SkillSprout
Chapter 4 · Lesson 6
Intermediate
8 min read

Security Groups

Overview

A security group is a virtual, stateful firewall that controls inbound and outbound traffic at the level of an elastic network interface (ENI) — so for EC2 instances, RDS databases, load balancers, Lambda-in-VPC functions, endpoints and more. You write allow rules that say which protocol, port range and source/destination are permitted.

Because a security group is stateful, if you allow an inbound request the reply is automatically allowed out, and vice versa — you do not write a rule for return traffic. Anything not explicitly allowed is denied. A security group has no deny rules.

The most powerful feature is that a rule's source can be another security group. Instead of listing IP addresses, you say "the database accepts port 3306 from the app tier's security group". As instances come and go, the rule keeps working. This security group chaining is the standard pattern for multi-tier architectures.

Key Concepts

  • Security groups are stateful: return traffic for an allowed connection is automatically permitted regardless of rules.
  • Allow rules only — there is no deny; everything not allowed is implicitly denied.
  • Defaults: a new SG has no inbound rules and allows all outbound; the default SG allows inbound from itself.
  • SGs apply at the ENI level, are VPC-specific and are evaluated together (union) when multiple SGs are attached.
  • A rule source/destination can be a CIDR, prefix list, or another security group (same VPC or a peered VPC).
  • Changes take effect immediately; connections already being tracked may continue until they end or time out.
  • Defaults: 5 security groups per ENI (max 16), 60 inbound + 60 outbound rules per SG (adjustable; product with SGs per ENI is capped at 1,000).
  • Security groups are not filtered by AZ or subnet: one SG can span the whole VPC.
  • Connection tracking can be bypassed by allowing 0.0.0.0/0 on all ports in both directions (untracked flows).

Detailed Notes

How security groups evaluate traffic

  • Inbound rules are checked for traffic entering the ENI; outbound rules for traffic leaving.
  • With multiple security groups on one ENI, AWS builds the union of all rules — if any rule in any attached SG allows the traffic, it is allowed.
  • Rule order does not matter; there is no priority number.
  • Stateful tracking: the SG remembers an allowed flow, so replies are permitted even if no outbound (or inbound) rule would otherwise match.
PropertyBehaviour
Rule typeAllow only
StateStateful (connection tracked)
Default inboundDeny all
Default outboundAllow all (can be restricted)
Rule orderNot applicable — all rules evaluated
ScopeENI / instance, within one VPC
Rule sourcesCIDR (IPv4/IPv6), prefix list, another SG

Rule anatomy

A rule contains a protocol (TCP, UDP, ICMP, or all), a port range, a source (inbound) or destination (outbound), and an optional description.

json
{
  "GroupName": "app-tier-sg",
  "IpPermissions": [
    {
      "IpProtocol": "tcp",
      "FromPort": 8080,
      "ToPort": 8080,
      "UserIdGroupPairs": [{ "GroupId": "sg-0alb1234567890abc", "Description": "From ALB only" }]
    }
  ]
}

Security group referencing (chaining)

TierInbound rule
ALB SG443 from 0.0.0.0/0
App SG8080 from ALB SG
DB SG3306 from App SG

Advantages: no IP bookkeeping, works with Auto Scaling, and enforces the tier order. You can reference a security group in a peered VPC (same Region) and, with the same-Region constraint, in shared VPCs. You cannot reference an SG in a different Region.

Default security group and defaults

  • Every VPC has a default SG allowing all inbound from members of the same SG and all outbound. You can edit rules but cannot delete it.
  • A new SG: no inbound, all outbound.
  • If you do not specify an SG at launch, the instance gets the VPC's default SG.

Limits you should remember

LimitDefault
Security groups per VPC2,500
Inbound rules per SG60 (IPv4 and IPv6 counted separately)
Outbound rules per SG60
Security groups per network interface5 (up to 16)
Max rules per ENI (SGs x rules)1,000

Practical patterns

  • Bastion / SSH: restrict port 22 to the corporate CIDR — or better, remove SSH and use SSM Session Manager.
  • Load balancer front door: ALB SG allows 80/443 from the internet; instances accept traffic only from the ALB SG.
  • Lambda in VPC and VPC endpoints: interface endpoints are ENIs that use security groups (allow 443 from your workloads).
  • Managed prefix lists group CIDRs (for example on-premises ranges) and reuse them in many SGs.

Security group vs. NACL summary

Security groupNACL
LevelENI/instanceSubnet
StateStatefulStateless
RulesAllow onlyAllow and deny
EvaluationAll rulesIn number order, first match
AppliesOnly if attachedAutomatically to all subnet traffic

CLI examples

bash
aws ec2 create-security-group --group-name app-sg --description "App tier" --vpc-id vpc-0abc123
aws ec2 authorize-security-group-ingress --group-id sg-0app --protocol tcp --port 8080 --source-group sg-0alb
aws ec2 revoke-security-group-egress --group-id sg-0app --protocol -1 --cidr 0.0.0.0/0

Troubleshooting

  • Timeouts usually mean a missing allow rule or a NACL/route problem; "connection refused" means the packet arrived but nothing listens on the port.
  • Security group changes don't need a reboot.
  • Use VPC Flow Logs: an ACCEPT in the inbound direction followed by no outbound reply points to the OS or application layer.
  • AWS Config and Security Hub can flag SGs open to 0.0.0.0/0 on admin ports.

Real World Example

Scenario — an e-commerce platform locks down its three-tier application.

A Black-Friday-ready platform runs an ALB, an Auto Scaling group of web/app servers and an Aurora MySQL cluster. The security team's rule: no database may ever accept a connection from anything except the application servers.

The architects create three security groups:

  • alb-sg — inbound 443 from 0.0.0.0/0.
  • app-sg — inbound 8080 from alb-sg only; outbound restricted to 3306 (to db-sg) and 443 (to the VPC endpoint and NAT).
  • db-sg — inbound 3306 from app-sg only; no outbound rules needed beyond replies.

When Auto Scaling grows the fleet from 6 to 60 servers on sale day, nothing changes — every new instance carries app-sg, so it is automatically allowed to reach the database. An auditor later checks AWS Config: no SG allows 22 or 3389 from the internet, and administrators connect through SSM Session Manager, so no inbound admin ports exist at all.

Architecture Diagram

Security group chaining across three tiers
Architecture
AWS Cloud
Internet users
VPC
Public subnets
ALBalb-sg: 443 from 0.0.0.0/0
App subnets
App serversapp-sg: 8080 from alb-sg
Data subnets
Aurora MySQLdb-sg: 3306 from app-sg

Each tier only accepts traffic from the security group of the tier in front of it.

  1. 1Users connect to the ALB; alb-sg allows HTTPS from anywhere.
  2. 2The ALB forwards to app servers; app-sg allows port 8080 only from alb-sg.
  3. 3App servers connect to Aurora; db-sg allows port 3306 only from app-sg.
  4. 4Replies flow back automatically because security groups are stateful.

Best Practices

  • ✓Reference security groups, not IP addresses, for traffic between your own tiers.
  • ✓Follow least privilege: open only required ports and restrict the source; avoid 0.0.0.0/0 except for public web ports on load balancers.
  • ✓Never expose SSH (22) or RDP (3389) to the internet; use SSM Session Manager or EC2 Instance Connect Endpoint.
  • ✓Restrict outbound rules for sensitive tiers instead of leaving the default allow-all.
  • ✓Use descriptive names and rule descriptions, and tag SGs by application and environment.
  • ✓Use managed prefix lists to share common CIDR sets across many security groups.
  • ✓Audit continuously with AWS Config rules, Security Hub and IAM Access Analyzer-style reviews; remove unused SGs.
  • ✓Use separate SGs per role (alb, app, db, endpoints) so changes are scoped and auditable.

Common Interview Questions

Exam Tips

SAA-C03 focus

  • Stateful + allow-only + instance level = security group. Stateless + allow/deny + subnet level = NACL.
  • To block a specific IP address the answer is a NACL deny rule — security groups cannot deny.
  • Default SG behaviour: inbound denied, outbound allowed (for new SGs).
  • Rule sources can be another security group — the preferred, scalable pattern for tiered architectures and Auto Scaling.
  • SG rules are evaluated together; there is no rule order and the most permissive combination wins.
  • Security groups are VPC-scoped and can reference peered VPC SGs in the same Region only.
  • Timeouts -> likely SG/NACL/route; connection refused -> reached the host but nothing is listening.
  • Interface VPC endpoints, Lambda-in-VPC, RDS, ELB all use security groups because they use ENIs.

Finished this lesson?

0 of 64 lessons done · 0% of the course