Stop 05 · Compute With Amazon EC2Topics · 6/9

Security Groups for EC2

Part of the AWS Roadmap

In short

Most EC2 security incidents start with one over-open rule, usually SSH or a database port open to the whole internet. Security groups are your first firewall, and they are simple once you know their three rules.

A security group is a virtual firewall attached to an instance's network interface. It only has allow rules, it is stateful so return traffic is automatically allowed, and by default it allows all outbound traffic and no inbound traffic.

The jaadu as a society gate guard checks a clipboard guest list while waving one visitor through and holding up a hand to another.

Think of it like this

Housing society guard with a guest list

The gate guard lets in only visitors on the list. Residents can go out freely, and when a delivery you ordered comes back, the guard lets it in because you started the conversation. That is a stateful, allow-only security group.

Watch it happen

Inbound vs outbound in a new security group

Inbound

  • Nothing allowed until you add a rule
  • Example: TCP 80 and 443 from 0.0.0.0/0 for a website
  • Example: TCP 22 from 203.0.113.10/32 (your IP only)
  • Can reference another security group as the source

Outbound

  • All traffic allowed by default
  • Can be tightened, for example only 443 to the internet
  • Return traffic for inbound requests is always allowed
  • Rules are evaluated together, no order or priority

Security groups only allow, never deny, and they are stateful. Keep inbound as narrow as possible and use group-to-group references inside your app.

The big ideas

What you need to know

Stateful

If a request is allowed in, the reply is allowed out automatically. You don't write rules for responses.

Allow rules only

To block one bad IP you need a network ACL or AWS WAF, not a security group.

Reference other groups

Allow the database port only from the app servers' security group. New app servers get access automatically.

Attached to interfaces

A security group belongs to a VPC and attaches to network interfaces. One instance can have several groups.

See it in code

Walk through the code

bash
1VPC_ID=vpc-0123456789abcdef02MY_IP=$(curl -s https://checkip.amazonaws.com)3 4WEB_SG=$(aws ec2 create-security-group --group-name web-sg \5  --description "Web servers" --vpc-id "$VPC_ID" \6  --query GroupId --output text)7 8aws ec2 authorize-security-group-ingress --group-id "$WEB_SG" \9  --protocol tcp --port 80 --cidr 0.0.0.0/010aws ec2 authorize-security-group-ingress --group-id "$WEB_SG" \11  --protocol tcp --port 22 --cidr "$MY_IP/32"12 13# The database group accepts MySQL only from web servers14aws ec2 authorize-security-group-ingress --group-id sg-0db0000000000000a \15  --protocol tcp --port 3306 --source-group "$WEB_SG"

1/4checkip.amazonaws.com returns your current public IP.

A web security group open to HTTP, SSH from your IP only, and a database group that trusts only the web group.

Save yourself the pain

Common mistakes

Oops

Opening port 3306 or 5432 to 0.0.0.0/0 to fix a connection error.

Do this instead

Allow the database port only from the app's security group.

Oops

Trying to add a deny rule for an attacker's IP.

Do this instead

Security groups have no deny rules. Use a network ACL or AWS WAF to block specific addresses.

Your mission

Lock down a web server

~25 min

Quick check

A web server's security group allows inbound TCP 443. Do you need an outbound rule for the HTTPS responses?

Go deeper

  1. 1Allow SSH only from your own IP address, not from 0.0.0.0/0.
  2. 2Allow ports 80 and 443 from anywhere for a public web server and test it.
  3. 3Reference one security group from another, for example letting only the web tier reach the database.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge