Skip to content
Certification Guide

Lab · Task 1.2 · about 30 minutes

Stateless Network ACLs and Stateful Security Groups

Services in this lab

  • Amazon VPCin scope
  • Amazon EC2in scope
  • AWS CLIin scope

Lesson for this lab: Secure Workloads and Applications

Goal

Build a public subnet with one web server, then watch a stateless network ACL drop the replies, and see that a numbered deny rule only wins when it is numbered before the allow.

Before you start

From the exam's point of view, this lab proves three lines from the lesson. A security group is stateful, so replies to allowed traffic flow back on their own. A network ACL is stateless, so every direction needs its own rule. A network ACL checks rules in ascending number and stops at the first match.

  • Use a sandbox AWS account, not production, and set an AWS Budgets alert on it.
  • Step 2 opens port 80 to the whole internet, because the web server is public for the length of the lab. It serves one static test page and holds no data. Clean-up removes the rule along with the instance.
  • Sign in as an administrator through IAM Identity Center, as in the 1.1 lab. Do not create IAM user access keys or a key pair. The instance in this lab has no SSH port, so it needs no key.
  • Pick one Region and use it throughout, for example eu-west-1. Below, <region> means that Region.
  • Choose one Availability Zone in that Region, for example eu-west-1a, and write it down as <az>.
  • Open two terminal windows. Terminal 1 runs the AWS CLI. Terminal 2 runs curl from your own machine, outside AWS.
  • Write the IDs that each command returns into a notes file, because later steps refer to them as <vpc-id>, <subnet-id>, and so on.

Resources this lab creates, all deleted in Clean up:

ResourceName or placeholder
VPC<vpc-id>, CIDR 10.20.0.0/16
Public subnet<subnet-id>, CIDR 10.20.1.0/24
Internet gateway<igw-id>
Route table<rtb-id>, with one default route and one subnet association
Security grouplab-web-sg (<sg-id>)
EC2 instance<instance-id>, runs a small web server on port 80
Custom network ACL<nacl-id>, associated with the subnet
Local filesuserdata.sh

Steps

0 of 8 steps done

  1. In terminal 1:

    aws ec2 create-vpc --cidr-block 10.20.0.0/16 --query Vpc.VpcId --output text
    aws ec2 create-subnet --vpc-id <vpc-id> --cidr-block 10.20.1.0/24 \
      --availability-zone <az> --query Subnet.SubnetId --output text
    aws ec2 modify-subnet-attribute --subnet-id <subnet-id> --map-public-ip-on-launch
    aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text
    aws ec2 attach-internet-gateway --internet-gateway-id <igw-id> --vpc-id <vpc-id>
    aws ec2 create-route-table --vpc-id <vpc-id> --query RouteTable.RouteTableId --output text
    aws ec2 create-route --route-table-id <rtb-id> \
      --destination-cidr-block 0.0.0.0/0 --gateway-id <igw-id>
    aws ec2 associate-route-table --route-table-id <rtb-id> --subnet-id <subnet-id>
    

    Console: VPC → Create VPC → VPC and more, with one public subnet, one Availability Zone and an internet gateway. Then open the subnet's route table and confirm 0.0.0.0/0 points at the internet gateway. That route is what makes the subnet public. A subnet with no such route is private, whatever its name.

  2. The security group is the instance-level filter. It allows inbound port 80 and nothing else. There is no SSH rule, so the instance has no inbound administrative port at all.

    aws ec2 create-security-group --group-name lab-web-sg \
      --description "Lab web server, HTTP only" --vpc-id <vpc-id> \
      --query GroupId --output text
    aws ec2 authorize-security-group-ingress --group-id <sg-id> \
      --protocol tcp --port 80 --cidr 0.0.0.0/0
    

    Console: VPC → Security groups → Create security group, add one inbound rule for HTTP from Anywhere-IPv4. Leave the default outbound rule in place.

  3. Save this as userdata.sh. It writes a page and serves it on port 80 with Python's built-in web server:

    #!/bin/bash
    mkdir -p /srv/www
    echo "secure workloads lab" > /srv/www/index.html
    cd /srv/www && nohup python3 -m http.server 80 >/var/log/http.log 2>&1 &
    

    In terminal 1, resolve the latest Amazon Linux 2023 AMI from its public Systems Manager parameter and launch the instance into the public subnet:

    aws ec2 run-instances \
      --image-id resolve:ssm:/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
      --instance-type t3.micro --count 1 \
      --subnet-id <subnet-id> --security-group-ids <sg-id> \
      --user-data file://userdata.sh \
      --query 'Instances[0].InstanceId' --output text
    aws ec2 wait instance-running --instance-ids <instance-id>
    aws ec2 describe-instances --instance-ids <instance-id> \
      --query 'Reservations[0].Instances[0].PublicIpAddress' --output text
    

    Write the returned address down as <public-ip>. Then, in terminal 2, confirm the server answers:

    curl --max-time 10 http://<public-ip>/
    

    The output is secure workloads lab. Every packet in this exchange passed the default network ACL, which allows everything, and the security group, which allows port 80.

  4. A custom network ACL denies everything until you add rules. This one allows inbound port 80 from anywhere and has no outbound rule at all, which is the mistake you are about to see.

    In terminal 1, create the ACL and its inbound rule:

    aws ec2 create-network-acl --vpc-id <vpc-id> \
      --query NetworkAcl.NetworkAclId --output text
    aws ec2 create-network-acl-entry --network-acl-id <nacl-id> \
      --ingress --rule-number 100 --protocol 6 --port-range From=80,To=80 \
      --cidr-block 0.0.0.0/0 --rule-action allow
    

    Then find the subnet's current association and point it at the new ACL:

    aws ec2 describe-network-acls \
      --filters Name=association.subnet-id,Values=<subnet-id> \
      --query 'NetworkAcls[0].Associations[?SubnetId==`<subnet-id>`].NetworkAclAssociationId' \
      --output text
    aws ec2 replace-network-acl-association \
      --association-id <assoc-id> --network-acl-id <nacl-id> \
      --query NewAssociationId --output text
    

    The last command prints the subnet's new association ID. Write it down as <assoc-id>, because clean-up needs it to hand the subnet back to the default ACL.

    Console: VPC → Network ACLs → Create network ACL, then add an inbound rule 100 for HTTP from 0.0.0.0/0, and under Subnet associations select your subnet.

  5. EXPECTED FAILURE. In terminal 2, repeat the request from the launch step:

    curl --max-time 10 http://<public-ip>/
    

    The request hangs and then fails with a timeout, and curl prints nothing. The inbound request was allowed, so the server received it, but its reply leaves the subnet on an ephemeral port toward your machine. Your custom network ACL has no outbound rule for that traffic, so the default * rule denies it. The security group still allows port 80. It is not the cause, and it would be fine if its outbound rules were changed. Check it yourself in the next step.

  6. Network ACLs are stateless, so return traffic needs its own rule. The client picks an ephemeral source port, and the range depends on the client's operating system. This lab opens 1024–65535, which AWS suggests to cover clients you can't predict.

    In terminal 1:

    aws ec2 create-network-acl-entry --network-acl-id <nacl-id> \
      --egress --rule-number 100 --protocol 6 --port-range From=1024,To=65535 \
      --cidr-block 0.0.0.0/0 --rule-action allow
    

    In terminal 2, run curl --max-time 10 http://<public-ip>/ again. It returns secure workloads lab. Nothing changed in the security group. Only the subnet's network ACL gained the outbound rule that the stateless filter needed.

  7. Your machine's public address is the one you want to block. Get it from terminal 2:

    curl -s https://checkip.amazonaws.com
    

    Write it down as <your-ip>. In terminal 1, add an inbound deny rule for that address, numbered 200, after the allow at rule 100:

    aws ec2 create-network-acl-entry --network-acl-id <nacl-id> \
      --ingress --rule-number 200 --protocol 6 --port-range From=80,To=80 \
      --cidr-block <your-ip>/32 --rule-action deny
    

    In terminal 2, run curl --max-time 10 http://<public-ip>/ again. It still returns secure workloads lab. This is the trap. Rule 100 allows every IPv4 address on port 80 and matches your request first, so evaluation stops there and rule 200 never runs.

  8. EXPECTED FAILURE. In terminal 1, add a deny rule for the same address with the lower number 90:

    aws ec2 create-network-acl-entry --network-acl-id <nacl-id> \
      --ingress --rule-number 90 --protocol 6 --port-range From=80,To=80 \
      --cidr-block <your-ip>/32 --rule-action deny
    

    In terminal 2, run curl --max-time 10 http://<public-ip>/. It now times out, while the security group still allows port 80 and rule 100 still allows everyone else. Rule 90 matches first, so the deny applies. Ask a teammate on another network to run the same curl, and they should still get the page. That shows the block is per address, not per instance.

Verify

Run each check and compare it with the expected result.

CheckTerminalExpected result
aws ec2 describe-network-acls --network-acl-ids <nacl-id> --query 'NetworkAcls[0].Entries[].[RuleNumber,Egress,RuleAction,CidrBlock]' --output table1Rules 90 and 200 deny your address, rule 100 allows 0.0.0.0/0 inbound, and rule 100 allows 1024-65535 outbound
aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=<subnet-id> --query 'NetworkAcls[0].NetworkAclId' --output text1Prints <nacl-id>, the custom ACL
aws ec2 describe-security-groups --group-ids <sg-id> --query 'SecurityGroups[0].IpPermissions[].[FromPort,IpRanges[0].CidrIp]' --output text1Prints 80 0.0.0.0/0, and nothing about the ACL changed
curl --max-time 10 http://<public-ip>/ from your address, after step 62Returns secure workloads lab
The same curl after the last step2Times out, because rule 90 denies your address first

Clean up

Delete everything in this order. The custom ACL and the route table must be unlinked from the subnet before they can be deleted, and the subnet must go before the VPC.

  1. Terminal 1: terminate the instance and wait until it stops.

    aws ec2 terminate-instances --instance-ids <instance-id>
    aws ec2 wait instance-terminated --instance-ids <instance-id>
    
  2. Give the subnet back the VPC's default network ACL and detach the route table. Look up the default ACL first. The route table lookup prints the association ID, which you write down as <rtb-assoc-id>.

    DEFAULT_ACL=$(aws ec2 describe-network-acls \
      --filters Name=vpc-id,Values=<vpc-id> Name=default,Values=true \
      --query 'NetworkAcls[0].NetworkAclId' --output text)
    aws ec2 replace-network-acl-association --association-id <assoc-id> --network-acl-id $DEFAULT_ACL
    aws ec2 describe-route-tables --route-table-ids <rtb-id> \
      --query 'RouteTables[0].Associations[?SubnetId==`<subnet-id>`].RouteTableAssociationId' --output text
    aws ec2 disassociate-route-table --association-id <rtb-assoc-id>
    
  3. Delete the custom network ACL, the security group, the route table, and the subnet, in that order.

    aws ec2 delete-network-acl --network-acl-id <nacl-id>
    aws ec2 delete-security-group --group-id <sg-id>
    aws ec2 delete-route-table --route-table-id <rtb-id>
    aws ec2 delete-subnet --subnet-id <subnet-id>
    
  4. Detach and delete the internet gateway, then delete the VPC.

    aws ec2 detach-internet-gateway --internet-gateway-id <igw-id> --vpc-id <vpc-id>
    aws ec2 delete-internet-gateway --internet-gateway-id <igw-id>
    aws ec2 delete-vpc --vpc-id <vpc-id>
    
  5. Both terminals: delete the local file userdata.sh and close terminal 2.

  6. Check that nothing remains:

    aws ec2 describe-vpcs --vpc-ids <vpc-id>
    aws ec2 describe-instances --instance-ids <instance-id> \
      --query 'Reservations[].Instances[].State.Name' --output text
    

    The first command fails because the VPC no longer exists, and the second prints terminated. Console: VPC → Your VPCs no longer lists 10.20.0.0/16, and EC2 → Instances shows no running instance with the lab-web-sg group. The lab created no other resources, so nothing from this lab should remain.

Sources

Last reviewed