Skip to content
Visual explainer 3h to learn 4 steps

Security groups, NACLs & VPC endpoints

Lock down traffic and reach S3 and DynamoDB privately.

By Invicly Tech, , in AWS Solutions Architect – Associate. About 12 minutes to read.

Topic 10 of 34

In short

Security groups are stateful firewalls on network interfaces that only allow traffic. Network ACLs are stateless filters on subnets that can allow or deny. VPC endpoints let resources reach AWS services privately without an internet gateway or NAT.

Explained simply

A VPC has two kinds of built-in firewall. Security groups protect each network interface of an instance, load balancer or database. Network ACLs (NACLs) protect a whole subnet. Traffic usually has to pass both, so they work like a guard at the building gate and a lock on each office door.

Security groups are stateful: they remember connections. If they let a request in, the reply goes out automatically. They only have allow rules, and anything not allowed is blocked. NACLs are stateless: each direction is checked separately, so you must allow the reply too. NACLs can also say deny.

Your servers often need AWS services like S3 or DynamoDB. Without help, that traffic travels out through a NAT gateway to the service's public address. VPC endpoints give a private path instead, so the traffic never leaves the AWS network and you skip NAT charges.

There are two kinds of endpoint. Gateway endpoints are free route table entries for S3 and DynamoDB. Interface endpoints are private network cards in your subnets that reach almost any AWS service, or even another company's service, through AWS PrivateLink.

Press Play on a diagram to walk through it one step at a time.

CompareSecurity group vs NACL
Security group
  • Instance or ENI level
  • Stateful: replies allowed
  • Allow rules only
  • All rules evaluated
  • Can reference other SGs
Network ACL
  • Subnet level
  • Stateless: allow return traffic
  • Allow and deny rules
  • Lowest rule number wins
  • Good for blocking IP ranges
CompareVPC endpoint types
Gateway endpoint
  • S3 and DynamoDB only
  • Route table target
  • No hourly charge
  • Same-Region access
Interface endpoint
  • Most AWS services
  • ENI with private IP
  • Powered by PrivateLink
  • Hourly and per-GB charge
LayersChecks on the way into an instance
  1. 1
    Route table

    Is there a path to this subnet?

  2. 2
    Network ACL

    Subnet rules, numbered, first match wins.

  3. 3
    Security group

    ENI rules, all allows evaluated together.

  4. 4
    OS firewall

    iptables or Windows Firewall, if used.

  5. 5
    Application

    Must be listening on the port.

In depth

Security groups in practice

Security groups attach to network interfaces (ENIs), not to subnets. Each rule has a protocol, a port range, and a source or destination, which can be a CIDR, a managed prefix list, or another security group. A new security group allows no inbound traffic and all outbound traffic. The default security group also allows inbound traffic from itself.

Referencing a security group instead of IPs is the most useful habit. "Allow port 5432 from the app security group" keeps working as instances come and go, and it even works across peered VPCs in the same Region.

All rules are evaluated together; there is no order and no deny. Changes take effect immediately. By default an ENI can have up to five security groups and each group up to 60 inbound and 60 outbound rules; both quotas can be raised.

FlowChained security groups in a three-tier app
  1. Step 1
    Internet

    Users on port 443.

  2. Step 2
    ALB SG

    Allows 443 from 0.0.0.0/0.

  3. Step 3
    App SG

    Allows 8080 from the ALB SG only.

  4. Step 4
    DB SG

    Allows 5432 from the app SG only.

NACLs: stateless and ordered

Each subnet has exactly one network ACL. The default NACL allows all traffic in and out. A custom NACL you create denies everything until you add rules. Rules are numbered from 1 to 32766 and checked from lowest to highest; the first match wins and the rest are ignored. A final rule marked * denies anything that matched nothing.

Because NACLs are stateless, replies need their own rule. A client connecting to your web server on port 443 uses a random high source port for the reply. Linux clients use 32768 to 60999, Windows 49152 to 65535, and NAT gateways and load balancers use 1024 to 65535. Allowing 1024 to 65535 for return traffic covers them all.

Use NACLs for coarse subnet-wide rules, especially deny rules for known bad IP ranges. Leave fine-grained, per-application rules to security groups.

SequenceWhy stateless NACLs need two rules
Client 198.51.100.7
Subnet NACL
Web server
  1. 1SYN to port 443 from port 50123
    Client 198.51.100.7 to Subnet NACL: SYN to port 443 from port 50123
  2. 2Inbound rule 100: allow 443
    Subnet NACL to Web server: Inbound rule 100: allow 443
  3. 3Reply to port 50123
    Web server to Subnet NACL: Reply to port 50123
  4. 4Outbound rule: allow 1024-65535
    Subnet NACL to Client 198.51.100.7: Outbound rule: allow 1024-65535

Gateway endpoints for S3 and DynamoDB

A gateway endpoint adds a route to your chosen route tables. The destination is an AWS managed prefix list (pl-...) that represents all S3 or DynamoDB addresses in that Region, and the target is the endpoint. Instances need no code changes; the same public service names now resolve and route privately. Gateway endpoints are free, which is why they are the classic fix for high NAT costs.

Limits matter for the exam. Gateway endpoints only reach services in the same Region. They cannot be used from on-premises networks over VPN or Direct Connect, or from other VPCs through peering or Transit Gateway.

An endpoint policy can restrict which buckets or tables are reachable through it, which helps stop data being copied to an attacker's bucket. On the bucket side, a policy condition on aws:SourceVpce or aws:SourceVpc can require that requests come through your endpoint or VPC. Conditions on aws:SourceIp do not work for endpoint traffic, because it uses private addresses.

  • Free, route-table based
  • S3 and DynamoDB only, same Region
  • Not usable from on-premises or peered VPCs
  • Endpoint policy limits which buckets are reachable

Interface endpoints and PrivateLink

An interface endpoint places an ENI with a private IP in each subnet you choose, usually one per AZ. With private DNS enabled, the service's normal name, such as ssm.us-east-1.amazonaws.com, resolves to those private IPs inside the VPC. This needs the VPC's DNS support and DNS hostnames attributes turned on. The endpoint has a security group that must allow HTTPS from your clients. You pay per endpoint per AZ per hour, plus per GB.

Unlike gateway endpoints, interface endpoints are reachable from on-premises over VPN or Direct Connect and from connected VPCs. S3 offers both kinds; use the interface type when on-premises systems need private S3 access.

The same technology, AWS PrivateLink, lets you publish your own service. Put it behind a Network Load Balancer, create an endpoint service, and consumers create interface endpoints to it. Traffic flows one way only, and overlapping CIDRs between provider and consumer do not matter.

FlowPublishing a service with PrivateLink
  1. 1
    Provider NLB

    Service runs behind an NLB.

  2. 2
    Endpoint service

    Provider allows chosen accounts.

  3. 3
    Consumer endpoint

    Interface endpoint in consumer VPC.

  4. 4
    Accept

    Provider approves the connection.

  5. 5
    Private calls

    Consumer uses the endpoint DNS name.

Choosing the right control

Match the control to the job. Security groups are the default for allowing traffic to each workload. NACLs are for subnet-wide guardrails and blocking specific IP ranges, since only they can deny. AWS WAF filters HTTP requests on CloudFront, ALB and API Gateway. AWS Network Firewall inspects all traffic in and out of a VPC with stateful rules, including domain name filtering, which suits "only allow outbound traffic to these domains" requirements.

Endpoints are a security control too. Gateway or interface endpoints with endpoint policies keep AWS API traffic private and limit which resources can be reached, a key part of a data perimeter.

Verify rather than assume. Flow logs show what is actually accepted and rejected, and Reachability Analyzer shows which rule would block a path before you deploy.

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Compare security groups and NACLs~45m
  2. 2
    Write stateful and stateless rules~45m
  3. 3
    Add gateway endpoints for S3 and DynamoDB~45m
  4. 4
    Use interface endpoints with PrivateLink~45m

Key points

  • Reference a security group as the source, e.g. DB allows port 3306 from the app SG.
  • Use a NACL deny rule to block a specific malicious IP range.
  • Stateless NACLs need outbound rules for ephemeral ports 1024 to 65535.
  • Gateway endpoints for S3 also remove NAT data-processing charges.
  • PrivateLink can expose your own service privately to other VPCs.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
# Allow MySQL only from the app tier security group
aws ec2 authorize-security-group-ingress \
  --group-id sg-0db1111111111111 \
  --protocol tcp --port 3306 \
  --source-group sg-0app222222222222

# Private S3 access from a VPC
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc \
  --service-name com.amazonaws.us-east-1.s3 \
  --route-table-ids rtb-0priv

Three-tier security groups from the CLI

Step 1: create three security groups in the VPC. Step 2: the ALB group allows 443 from anywhere. Step 3: the app group allows 8080 only from the ALB group, so even another instance in the same subnet cannot hit the app directly. Step 4: the database group allows 5432 only from the app group. Auto Scaling can add and remove app instances freely, and no IP addresses ever appear in the rules.

bash
ALB_SG=$(aws ec2 create-security-group --vpc-id $VPC --group-name alb \
  --description "ALB" --query GroupId --output text)
APP_SG=$(aws ec2 create-security-group --vpc-id $VPC --group-name app \
  --description "App" --query GroupId --output text)
DB_SG=$(aws ec2 create-security-group --vpc-id $VPC --group-name db \
  --description "DB" --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id $ALB_SG \
  --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id $APP_SG \
  --protocol tcp --port 8080 --source-group $ALB_SG
aws ec2 authorize-security-group-ingress --group-id $DB_SG \
  --protocol tcp --port 5432 --source-group $APP_SG

Blocking an attacking range with a NACL

Logs show abuse from 198.51.100.0/24 against a public subnet. Security groups cannot deny, so use the subnet's NACL. Step 1: list the NACL's rules and note that the allow rule for 443 is number 100. Step 2: add a deny rule with a lower number, 90, so it is checked first. Step 3: confirm with flow logs that traffic from that range now shows REJECT. If you had added the deny as rule 110, the allow at 100 would match first and the deny would never apply.

bash
aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$PUB_A \
  --query "NetworkAcls[0].[NetworkAclId,Entries]"
aws ec2 create-network-acl-entry --network-acl-id acl-0abc1234 \
  --ingress --rule-number 90 --protocol -1 \
  --rule-action deny --cidr-block 198.51.100.0/24

Quick reference

  • Security group: ENI level, stateful, allow only, all rules evaluated together
  • NACL: subnet level, stateless, allow and deny, lowest rule number wins
  • New custom NACLs deny everything; the default NACL allows everything
  • NACL return traffic: allow ephemeral ports 1024 to 65535
  • Reference security groups as sources instead of IP addresses
  • Gateway endpoint: free, S3 and DynamoDB, same Region, not from on-premises
  • Interface endpoint: ENI per AZ, private DNS, hourly cost, reachable over VPN and DX
  • Use aws:SourceVpce, not aws:SourceIp, to restrict buckets to endpoint traffic

Common mistakes

  • Trying to add a deny rule to a security group; only NACLs can deny.
  • Forgetting NACL outbound ephemeral ports, so replies are silently dropped.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A NACL has rule 100 allowing TCP 443 from 0.0.0.0/0 and rule 120 denying 203.0.113.0/24. Is HTTPS from 203.0.113.9 allowed?

Question 2

2On-premises servers reach AWS over Direct Connect and need private access to S3. Which endpoint type works?

Question 3

3Inbound HTTPS works through a custom NACL, but clients never get a response. What is missing?

Question 4

4A SaaS provider wants to offer its API privately to customers whose VPC CIDRs overlap with its own. What should it use?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Add an S3 gateway endpoint to your private route table and confirm the new route.

  2. Exercise 2medium

    Write a bucket policy that denies all access to the bucket private-data unless the request comes through endpoint vpce-0abc1234.

  3. Exercise 3hard

    Make Session Manager work for instances in a private subnet with no NAT gateway.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Can a security group block a specific IP address?
No. Security groups only have allow rules. Use a NACL deny rule, AWS WAF for HTTP traffic, or Network Firewall.
Do I need both security groups and NACLs?
Security groups are required and do most of the work. Many teams leave NACLs at the default allow-all and only add NACL rules for subnet-wide denies or compliance.
Are VPC endpoints free?
Gateway endpoints for S3 and DynamoDB are free. Interface endpoints are charged per hour per AZ and per GB processed.
Does changing a security group need a restart?
No. Rule changes apply immediately to every interface that uses the group.

Go deeper

ShareXLinkedInWhatsApp