AWS Security Group
Control inbound and outbound traffic to AWS resources by attaching security groups and defining least-privilege rules.
What Is an AWS Security Group?
An AWS security group acts as a virtual firewall for an AWS resource that has a network interface, such as an EC2 instance, Elastic Load Balancing load balancer, RDS database, or Lambda function configured for a VPC.
Security groups control traffic associated with the resource’s elastic network interfaces. They do not filter traffic at the subnet boundary. A resource can be associated with one or more security groups, and every network interface can have multiple security groups.
A security group belongs to one VPC. When an instance is launched with a security group, AWS applies that security group to the instance’s network interfaces.
Security Group Characteristics
Security groups have several important properties:
- Stateful: If inbound traffic is allowed, the corresponding response traffic is allowed automatically. An outbound rule is not required for the response.
- Allow rules only: Security groups do not support explicit deny rules. Traffic is denied unless a rule allows it.
- All rules are evaluated: AWS does not use the first-match behavior of a traditional firewall. If any applicable rule allows traffic, the traffic is allowed.
- Inbound rules are separate from outbound rules: The source of an inbound rule and the destination of an outbound rule must be defined explicitly.
- Changes apply immediately: Rule changes affect new and existing connections as soon as AWS processes them.
- Resource and network-interface association: The same security group can be reused by multiple resources.
Default Security Group Behavior
A default security group in a VPC has an inbound rule that allows traffic from other resources associated with the same security group. It also has outbound rules that allow all traffic.
Custom security groups have no inbound rules when created. They have an outbound rule that allows all traffic if you keep the default outbound rule, but you can remove it and add specific destinations.
Common behavior:
| Security group type | Inbound traffic | Outbound traffic |
|---|---|---|
| Default group | Allows traffic from the same default security group | Allows all traffic |
| Custom group | Denied until an inbound allow rule is added | Allows all traffic until the default outbound rule is removed |
Do not use the default security group for every workload. Create a separate security group for each tier or access pattern.
Inbound and Outbound Rules
Each rule specifies:
- A protocol, such as TCP or UDP
- A port or port range
- A source for inbound rules
- A destination for outbound rules
- An optional description
The source or destination can be an IPv4 CIDR block, an IPv6 CIDR block, a prefix list, or another security group.
For example:
| Type | Protocol | Port | Source | Purpose |
|---|---|---|---|---|
| Inbound | TCP | 22 | 203.0.113.10/32 |
SSH from one administrator address |
| Inbound | TCP | 443 | 0.0.0.0/0 |
Public HTTPS traffic |
| Inbound | TCP | 5432 | sg-app-0123456789abcdef0 |
PostgreSQL from application instances |
| Outbound | TCP | 443 | 0.0.0.0/0 |
HTTPS internet access |
Prefer security groups as sources instead of private CIDR ranges when a set of resources should communicate. This allows resources to change addresses without requiring a rule update.
Example: Three-Tier Architecture
Consider an application with a load balancer, application servers, and a database:
1 | Internet |
The recommended rules are:
web-sg: allow inbound TCP 443 from0.0.0.0/0and::/0.app-sg: allow inbound TCP 8080 fromweb-sg.db-sg: allow inbound TCP 5432 fromapp-sg.
Use separate security groups for each tier. The database should not accept traffic directly from the internet or from a broad VPC CIDR block.
Security Groups and Network ACLs
Security groups and network ACLs provide two layers of network filtering:
| Feature | Security group | Network ACL |
|---|---|---|
| Level | Network interface or resource | Subnet |
| Rule type | Allow only | Allow and deny |
| State | Stateful | Stateless |
| Rule evaluation | Any matching allow rule permits traffic | Rules are evaluated in numerical order |
| Typical use | Application-level resource access | Subnet boundary and network-wide restrictions |
A network ACL applies to traffic entering or leaving a subnet. It is stateless, so a matching inbound and outbound rule is required. Security groups remain stateful even if the subnet ACL is used.
For most EC2 application designs, security groups are the primary control. Network ACLs can add a coarse subnet-level deny or allow policy, but the two controls must be considered together.
Security Groups and Route Tables
| Feature | Security group | Route table |
|---|---|---|
| Purpose | Controls whether traffic is allowed | Determines where traffic is sent |
| Scope | Network interface or resource | Subnet |
| Rules | Stateful inbound and outbound allow rules | Destination CIDR and next-hop target |
| Effect | Permits or blocks traffic | Provides or prevents a network path |
A route table and a security group serve different purposes. For example, a 0.0.0.0/0 route to an internet gateway provides internet routing, but the resource still needs a public IP address, appropriate network ACL rules, and a security group that allows the traffic.
Creating a Security Group with the AWS CLI
Create a security group and add rules:
1 | # Create a security group for an application |
When using the AWS CLI, --source-group identifies another security group. Use --cidr or IpRanges for a CIDR source or destination. Some AWS resources accept a security group as an attached network interface setting or as a parameter in their own API.
Referencing Security Groups
A rule can reference a security group instead of a fixed IP address. AWS permits the traffic when the network interface matches the referenced security group.
This is useful for:
- Allowing application instances to reach a database security group.
- Allowing a load balancer security group to reach application instances.
- Allowing resources in approved AWS services to reach an application.
- Simplifying changes when instances are replaced or scale across private addresses.
Rules that reference security groups do not need to include private IP addresses or subnet CIDRs. Confirm Region, account, and VPC peering or transit configuration requirements before using cross-account or cross-network security group references.
Common Security Mistakes
Avoid these configurations:
- Opening SSH or RDP to
0.0.0.0/0. - Using a database security group source that is a broad VPC CIDR.
- Leaving unused inbound rules after a migration or access change.
- Attaching a single security group to every resource tier.
- Replacing least-privilege outbound controls with an overly broad rule everywhere.
- Assuming a security group is an explicit deny list.
- Checking only the instance’s rules while forgetting the destination security group.
Security groups must allow traffic in both relevant directions. Because security groups are stateful, a response is allowed automatically for a permitted connection, but the original inbound or outbound request must have an applicable allow rule.
Least-Privilege Recommendations
- Give every workload its own security group or a small set of purpose-specific groups.
- Use individual addresses or CIDR ranges for administrative access.
- Use
sg-*references for workload-to-workload communication. - Open only the application protocol and required ports.
- Restrict outbound traffic when the environment or compliance policy requires it.
- Add descriptions to rules so their owner and purpose are clear.
- Review rules regularly and remove obsolete groups and rules.
- Use AWS Organizations, AWS Config, and IAM to enforce approved network configurations.
- Combine security groups with route tables, network ACLs, identity controls, encryption, and host-based protections.
Troubleshooting Connectivity
When a connection fails, verify the path in the following order:
- Confirm the source resource can resolve and route to the destination address.
- Confirm the destination subnet route table and network ACL allow the traffic.
- Confirm the destination security group allows the destination port and protocol from the source.
- Confirm the source security group allows outbound traffic for the destination port and protocol.
- Confirm the destination process is listening on the expected port.
- Confirm DNS resolves to a reachable private or public address.
- Check VPC endpoints, NAT gateways, internet gateways, or network appliances when the path crosses network boundaries.
- Review VPC Flow Logs for accepted or rejected traffic.
Useful CLI checks include:
1 | # Show the rules for one security group |
If Flow Logs show REJECT on the source or destination network interface, inspect the corresponding security group and network ACL. Remember that security group changes are not always the cause: route, DNS, service binding, or application errors can produce the same connection timeout.
Security Group Checklist
Before deploying a workload:
- Create purpose-specific security groups.
- Remove unnecessary default inbound and outbound rules.
- Use narrow IPv4 or IPv6 source and destination ranges.
- Reference security groups for approved workload-to-workload communication.
- Restrict administrative ports to approved addresses or management networks.
- Document every rule with a description.
- Review network ACL interactions.
- Enable monitoring and VPC Flow Logs where appropriate.
- Test inbound, outbound, and cross-tier connectivity.
- Periodically audit for unused rules and groups.