In EC2, the firewall is not inside the machine but in front of it, and it is called a security group. It is a list of rules of the kind "let anyone in on port 443" or "let only this IP in on port 22". Anything not on the list doesn't get in. That is why a freshly created machine can be perfectly well running and yet the website doesn't load: nobody has opened the door.
Understanding four ideas about security groups avoids half of EC2's problems and most security holes. This guide explains them and leaves you with a well-configured group for a web server.
Before you start: what each thing means
- Security group
- The EC2 firewall: a set of rules saying what traffic can reach the machine. A machine can have several and one group can serve several machines.
- Port
- The numbered "door" each type of traffic comes in through: 80 for the unencrypted web, 443 for the secure web, 22 for SSH, 3306 for MySQL, 3389 for Windows remote desktop.
- Inbound rule
- What is allowed to reach the machine. Anything without a rule is blocked.
- Outbound rule
- What the machine can send out. By default, everything; you almost never need to touch it.
- Source
- Where it is allowed from: a specific IP, a range, anyone (0.0.0.0/0) or another security group.
- CIDR
- The way of writing IP ranges. 203.0.113.5/32 is a single address; 0.0.0.0/0 is the whole internet.
- Stateful
- If you let a request in, the reply can go out without a separate rule. That is why you only have to think about inbound.
Step by step
See which group your machine has
EC2 console, Instances, select the machine, Security tab. You will see the assigned group or groups and, below, the inbound rules as they apply. If the website doesn't respond, the answer is here in 90 % of cases: 443 is missing, or the source is only your IP.
Understand the default group
Every network (VPC) has a group called default that allows all traffic between machines that share it and nothing from the internet. It is useful for your machines to talk to each other, but not for a public website. Don't edit it to open ports to the world: create your own groups with names that say what they are for.
Create a group for web servers
Security Groups menu, Create security group. Name
web-publica, a clear description, the default VPC. Inbound rules:Type Port Source What for HTTPS 443 0.0.0.0/0 and ::/0 The secure website, for everyone HTTP 80 0.0.0.0/0 and ::/0 To redirect to HTTPS and renew certificates SSH 22 My IP Administration, only from your connection The source dropdown has a My IP option, which fills in your current address. Add a description to each rule ("office", "Ana's home"): a year from now you will be glad to know whose IP each one was. Create.
Assign the group to the machine
Go back to the instance, Actions, Security, Change security groups. Add
web-publicaand remove the one it had if it is no longer needed. The change is immediate, no reboot. Test the website and SSH.For a database: open only to your machines
If you have a machine or an RDS with a database, don't open 3306 to the internet or to "My IP". Create a group
bd-internawith an inbound rule on port 3306 whose source is the security groupweb-publica(in the source dropdown you can choose a group instead of an IP). That way only the machines carrying that group can connect, whatever IP they have. It is the right way and the one almost nobody knows about.Review the ones you already have
In the security groups list, sort or filter for those with rules whose source is 0.0.0.0/0 on ports other than 80 and 443. A 22, a 3306 or a 3389 open to the whole internet is what attackers look for every day. Close them to your IP or to the appropriate group. And delete the groups not assigned to anything: they only confuse.
What can go wrong
I closed 22 to "My IP" and now I can't get in
Your IP has changed (home connections). Edit the rule from the console, which doesn't depend on the machine, with "My IP" again. Or use EC2 Instance Connect from the console, which gets in through the browser if the image supports it.
The website works by IP but not by domain
It is not the security group: it is DNS or the web server configuration. The security group knows nothing about names, only ports and IPs.
The machine can't reach the internet to update
Check the outbound rules: someone has restricted them. By default they allow everything. If the machine has no public IP, it also needs a NAT gateway, which is another story.
I have dozens of "launch-wizard-7" groups
The launch wizard creates them every time someone doesn't pick an existing one. Reassign the machines to your named groups and delete the wizard ones. Those in use can't be deleted: that tells you which ones remain.
In Simplio
In Simplio the firewall of every machine, Lightsail or EC2, is the same simple list: port, for whom (everyone, only my IP, a specific IP) and a description. Simplio marks administration ports open to the whole internet in red and warns you, and every change asks for the second factor and is kept in the activity log with who did it. Without knowing what a CIDR is.
The same task, in AWS and in Simplio
| In the AWS console | In Simplio | |
|---|---|---|
| Opening a port only to your IP | EC2 console, Security Groups, edit, find your IP, description: 5 steps | "Only my IP" rule in the machine's firewall: 1 click, Simplio knows your IP |
| Knowing which machines have something open to the world | Review group by group | Warning in the list: "port 22 open to the whole internet" |
| Lightsail and EC2 | Two consoles with two different models | The same simple firewall for both |