AWS Security Groups

Stateful filtering, inbound/outbound rules, port ranges, security group references as sources, and no-deny-rule model.

Overview

Overview AWS Security Groups are the primary layer of network access control for EC2 instances, RDS databases, Lambda functions in VPCs, ECS tasks, and most other AWS resources that have network interfaces (ENIs).

A security group is a stateful virtual firewall attached to an ENI. Stateful means that if you allow inbound traffic on port 443, the return traffic is automatically allowed without any outbound rule. The connection tracking is handled by the underlying hypervisor.

Security groups operate on an implicit-deny model: there are no explicit deny rules. All traffic is denied by default, and rules only add allow entries. You cannot write a security group rule that denies a specific IP. If you need explicit deny, use a Network ACL. By default, a new security group allows all outbound traffic.

The most powerful feature is the ability to use another security group as the source or destination of a rule. This means you can write rules like "allow port 3306 from anything in the web-tier security group" — as instances join or leave that group, the rule automatically applies without any IP management. This is the idiomatic AWS pattern for inter-tier communication.

Security groups are applied at the ENI level. A single ENI can have up to 5 security groups applied simultaneously. An instance can have multiple ENIs, each with its own set of security groups.

Key concepts

  • Stateful filtering
  • Implicit deny model
  • Security group as source/destination
  • ENI attachment
  • Default security group

Interview questions

  • Design security groups for a three-tier application: ALB, EC2 web tier, and RDS database.
  • What is the difference between security groups and NACLs? When do you need both?

Sign up free to read the full topic

More Networking topics

Start free on Cariara · Coding problems · System design studio