Sub topic 1
What a security group does
A security group is a virtual firewall that controls which network traffic can reach the resources in a VPC and which traffic they can send out. This chapter covers the working model: what a security group filters, where it sits, and how it decides whether a packet gets through. Later chapters go deeper on rule syntax, sources, tiered design and troubleshooting.
Prerequisites
This chapter assumes you already know the following:
- Basic TCP/IP concepts: IP addresses, CIDR notation (such as
10.0.0.0/16), ports and protocols (TCP, UDP, ICMP). - What an Amazon EC2 instance is and how to launch one.
- What a VPC and a subnet are.
- How to run AWS CLI commands with configured credentials.
The core idea
Every EC2 instance in a VPC has at least one network interface. A security group is a named set of rules that AWS evaluates for the traffic going to and from that interface. If a rule allows the traffic, it passes. If no rule allows it, the traffic is dropped before it reaches the instance.
Three properties define how security groups behave:
- They contain only allow rules. You cannot write a rule that denies traffic. Anything not explicitly allowed is denied. This is called implicit deny.
- They are stateful. If a connection is allowed in one direction, the reply traffic is allowed automatically. You do not write a second rule for the response.
- They are attached to network interfaces, not to subnets. The filtering happens at the interface of each resource, so two instances in the same subnet can have different rules.
The rest of this chapter explains each property and the practical consequences.
What a security group contains
A security group has these attributes:
| Attribute | Meaning |
|---|---|
| Group ID | A unique identifier such as `sg-0123456789abcdef0`. Use this in CLI calls and in other groups' rules. |
| Name and description | Human-readable labels. The name must be unique within a VPC. The description is required when you create the group. |
| VPC | The VPC the group belongs to. A group can only be used by resources in that VPC. |
| Inbound rules | Allow traffic arriving at the resource. |
| Outbound rules | Allow traffic leaving the resource. |
Each rule specifies a protocol, a port range (for TCP and UDP) and either a source (inbound) or a destination (outbound). The source or destination can be an IP range in CIDR notation, a prefix list, or another security group. The next chapters cover rule anatomy and sources in detail.
Where security groups apply
A security group is associated with a network interface (an elastic network interface, or ENI). When you launch an EC2 instance, the console and CLI show the security group as a property of the instance, but AWS applies it to the instance's primary network interface. If the instance has additional interfaces, each can have its own groups.
Many AWS services also create network interfaces in your VPC, and security groups apply to them in the same way. Examples include Application Load Balancers, RDS databases, Lambda functions attached to a VPC, and interface VPC endpoints. The same filtering model applies to all of them.
Some facts about association:
- An interface can have several security groups at once. The default quota is 5 per interface, which you can raise within AWS limits.
- A security group can be attached to many interfaces.
- You can change the groups on an interface while the instance is running. The change takes effect without a restart.
- Edits to rules also take effect right away for all interfaces that use the group.
Because the group is tied to the VPC, you cannot attach a group from one VPC to an instance in another. Rules can reference groups in a peered VPC, which a later chapter covers.
A request path
The following diagram shows how a client request reaches an instance and where the security group sits in that path.
The filtering decision is made at the interface, outside the operating system. A packet that no rule allows never reaches the instance, so the instance's own logs show nothing. A client usually sees a connection attempt that hangs until it times out, rather than an immediate "connection refused" message. This difference is a useful clue when you troubleshoot, because a refusal normally means the packet arrived and nothing was listening on the port.
Allow-only rules and implicit deny
Because there are no deny rules, the way to block something is to not allow it. A new security group you create has no inbound rules, so it allows no inbound traffic at all. This is a safe starting point: you add only the access the workload needs.
Outbound traffic works differently by default. A security group created through the console or the CLI starts with one outbound rule that allows all traffic to all destinations (0.0.0.0/0). Instances can therefore reach out to the internet, to other instances and to AWS services unless you remove or narrow that rule. Many security-sensitive environments replace it with specific outbound rules.
Since you can only allow, you cannot say "allow everything except this address range" in one group. To get that result you list the allowed ranges explicitly. If you truly need explicit deny rules, for example to block a hostile IP address, you use network ACLs, which a later chapter compares with security groups.
Stateful filtering
A security group tracks connections. When an allowed request comes in, AWS records the connection and permits the response to leave, whatever the outbound rules say. The same holds in reverse: if an instance starts an allowed outbound connection, the reply is allowed back in even if no inbound rule matches.
For example, suppose a web server has this inbound rule:
- Allow TCP port 443 from
0.0.0.0/0.
A client opens a connection from a high, temporary port such as 51234 to port 443. The rule allows the request. The server's reply goes back to port 51234 on the client. You do not need an outbound rule for port 51234, and you could not sensibly write one, because clients choose different temporary ports. Statefulness removes that burden.
A few details are worth knowing:
- Tracking applies to the connection, so it follows the original direction. Traffic that the instance initiates is judged by outbound rules, and the replies come back automatically.
- Connection tracking has some exceptions. For instance, if a rule allows all traffic (all protocols and ports) for a given direction, traffic is not tracked for that rule. Response traffic is allowed by the rule itself in that case.
- Tracked connections have idle timeouts. After a long silence, an idle connection can be dropped from the table, and a later packet may not be recognised as a reply.
- Changing or removing a rule does not necessarily end connections that are already tracked. Existing flows can continue until they end or time out, while new connections follow the new rules.
How rules combine
Within one group, rules have no order. AWS does not read them top to bottom and stop at the first match. It considers all rules together, and traffic is allowed if any rule permits it. So you cannot "override" one rule with another, and rule position does not matter.
When an interface has several security groups, AWS merges all their rules into one effective set. If any rule in any attached group allows the traffic, it is allowed. Adding a group can only widen access. It can never narrow it.
Here is a small example. An instance has two groups attached:
| Group | Inbound rule |
|---|---|
| `web-sg` | TCP 443 from `0.0.0.0/0` |
| `admin-sg` | TCP 22 from `203.0.113.25/32` |
The instance accepts HTTPS from anywhere and SSH from one administrator address. Nothing else is allowed. Removing admin-sg from the instance would remove SSH access and leave HTTPS in place.
This additive behavior makes it practical to build small, single-purpose groups and combine them, such as one for "web server", one for "admin access" and one for "monitoring".
A first look at the CLI
The following commands create a group in a VPC and add one inbound rule that allows HTTPS from any IPv4 address. Replace the VPC ID and group ID with your own values. The create-security-group command prints the new group ID, which you use in the second command.
aws ec2 create-security-group \
--group-name web-sg \
--description "Web tier" \
--vpc-id vpc-0123456789abcdef0
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0After these commands, any instance that uses this group accepts TCP connections on port 443 from anywhere, and its replies are allowed automatically. Its outbound rules still permit all outgoing traffic, because the default outbound rule is in place.
A later chapter on managing rules covers listing, describing and revoking rules, and the same configuration in Terraform.
Common misunderstandings
- "A security group protects a subnet." It does not. It applies per network interface. Two instances in one subnet can have completely different access.
- "Rules are checked in order." They are not. All rules are evaluated together.
- "I need a rule for the reply traffic." You do not, because of stateful tracking.
- "I can deny a single address." You cannot in a security group. Use a network ACL for explicit denies.
- "A group from another VPC can be attached." It cannot. Groups are scoped to one VPC.
Summary
A security group is an allow-only, stateful firewall attached to network interfaces in a VPC. Anything not allowed is dropped. Allowed connections get their replies back automatically. Rules within and across attached groups have no order and add together. With this model in place, the next chapters look at rule structure, sources, and how to design groups for real architectures.
Exercise
An instance has two security groups attached. Group A allows inbound TCP 443 from 0.0.0.0/0. Group B has no inbound rules. A client connects to port 443 and the instance's application replies. Is the connection allowed, and do you need an outbound rule for the reply? Explain.
Show answer
The connection is allowed. Rules from all attached groups are merged, and any one rule that allows the traffic is enough, so Group A's rule permits it and Group B's empty rule set does not block it. You do not need an outbound rule for the reply, because security groups are stateful: the reply to an allowed inbound connection is permitted automatically.
Exercise
A client trying to reach port 8080 on your instance waits and then times out, instead of getting "connection refused". What does this suggest about the security group, and why?
Show answer
It suggests that no inbound rule allows TCP 8080 from the client, so the security group silently dropped the packets. A "connection refused" reply normally means the packet reached the instance and nothing was listening on the port. A timeout is what you see when packets are dropped without any response.