Sub topic 10
Designing web, application and database tiers
This chapter assumes you know CIDR notation, ports, EC2 instances, and how a VPC and its subnets fit together. It also assumes you have read the earlier chapters on allow-only rules, stateful filtering and security group references.
Why tiers get their own groups
A three-tier application splits work into a web tier that faces users, an application tier that runs business logic, and a database tier that stores data. Give each tier its own security group. Each group then states exactly which traffic that tier accepts, and a problem in one tier does not automatically open the others.
The design rule is simple. Each tier accepts traffic only from the tier directly in front of it, and only on the port it needs.
The rule set
| Group | Inbound source | Port |
|---|---|---|
| web-sg | 0.0.0.0/0 | TCP 443 |
| app-sg | web-sg | TCP 8080 |
| db-sg | app-sg | TCP 5432 |
The web tier is the only tier open to the internet, and only on HTTPS. The application and database tiers reference the group of the tier in front of them instead of an IP range.
Why reference groups instead of CIDR ranges
When a rule names a security group as its source, it matches instances that have that group attached. It does not match by IP address. This helps in three ways:
- Instances can scale out or be replaced and get new private IPs. The rule keeps working with no edits.
- The rule says what it means: "the web tier may call the application tier".
- A host in the same subnet that lacks the web group cannot reach the application port, even though its IP is nearby.
Building it with the CLI
Create the groups first, then add the rules. The web rule is the HTTPS rule from the earlier examples. The two internal rules use --source-group.
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0Here sg-0aaaaaaaaaaaaaaaa is the application group and sg-0123456789abcdef0 is the web group. Repeat the pattern for the database, using the database group as --group-id, port 5432, and the application group as the source.
Outbound rules
By default a new group allows all outbound traffic. Return traffic for allowed inbound connections is permitted automatically because groups are stateful, so you do not need outbound rules for replies. You only need outbound rules for connections that a tier starts itself.
For a stricter design, remove the default outbound rule on the database group. The database then cannot start connections to anything, and it still answers the application tier. Check first that the database does not need to reach services such as patch repositories or DNS.
Common mistakes
- Opening the database to a CIDR range. A rule such as 10.0.0.0/16 on port 5432 lets every host in the VPC try to connect. Reference the application group instead.
- Attaching one shared group to every tier. If all tiers use one group that references itself, any instance can reach any other. This removes the separation.
- Allowing SSH from 0.0.0.0/0 on the web tier. Restrict administrative ports to a specific address or use a managed access method.
- Forgetting that the source must be in range. Group references work for instances in the same VPC or in a peered VPC. Check this before relying on a reference across VPCs.
Checking your work
List each group and confirm that no internal tier has a source of 0.0.0.0/0.
aws ec2 describe-security-groups \
--group-ids sg-0aaaaaaaaaaaaaaaa \
--query 'SecurityGroups[].IpPermissions'The output for the application group should show one entry for port 8080 with a UserIdGroupPairs value and an empty IpRanges list.
Exercise
Your database group currently allows TCP 5432 from 10.0.0.0/16, which is the whole VPC. The application tier uses app-sg. How do you tighten the rule, and what does it improve?
Show answer
Revoke the 10.0.0.0/16 rule and add a rule on the database group for TCP 5432 with app-sg as the source (--source-group). Only instances with app-sg attached can then connect, so other hosts in the VPC are blocked, and the rule keeps working when application instances change IP addresses.