Sub topic 10
Designing web, application and database tiers
A tiered design splits a workload into layers, and each layer gets its own security group. The load balancer or web servers face the internet. The application servers accept traffic only from the web layer. The database accepts traffic only from the application layer. This chapter shows how to express that with security group references.
This chapter assumes you know CIDR notation, ports, EC2 instances, and the basics of a VPC and subnets. It also assumes you have read the earlier chapters on stateful, allow-only rules and on referencing one security group from another.
Why tiers get separate groups
A security group attaches to network interfaces, not to subnets. That lets you put instances with different roles in the same subnet and still give them different rules. You don't need a separate subnet for each tier to get separation, although separate subnets are still common for routing reasons (public versus private).
Giving each tier its own group has three benefits:
- Each group's rules describe one role, so they are easy to read and review.
- Scaling a tier needs no rule changes. A new instance gets the tier's group and inherits its access.
- A compromised web server cannot reach the database directly, because the database group never lists the web group as a source.
The target design
The design uses three groups:
| Group | Inbound rule | Source |
|---|---|---|
| `web-sg` | TCP 443 | `0.0.0.0/0` |
| `app-sg` | TCP 8080 | `web-sg` |
| `db-sg` | TCP 5432 | `app-sg` |
The database in this example is PostgreSQL, which uses port 5432. Substitute 3306 for MySQL or 1433 for SQL Server.
Because security groups are stateful, you do not add rules for responses. When the web tier calls the application tier and the connection is allowed, the reply is allowed automatically.
Building the tiers with the CLI
First create the three groups. Each command prints the new group ID. The IDs below are placeholders, so replace them with the values returned in your account.
aws ec2 create-security-group \
--group-name web-sg \
--description "Web tier" \
--vpc-id vpc-0123456789abcdef0
aws ec2 create-security-group \
--group-name app-sg \
--description "Application tier" \
--vpc-id vpc-0123456789abcdef0
aws ec2 create-security-group \
--group-name db-sg \
--description "Database tier" \
--vpc-id vpc-0123456789abcdef0Next add the web tier's public rule. HTTPS from anywhere is the one rule in this design that uses an open CIDR range.
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0Now allow the application tier to accept traffic from the web tier. The --source-group option names the web group, so the rule matches any interface that has web-sg attached, whatever its IP address.
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0The database rule follows the same pattern. Here sg-0bbbbbbbbbbbbbbbb stands for db-sg and sg-0aaaaaaaaaaaaaaaa for app-sg.
aws ec2 authorize-security-group-ingress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--protocol tcp \
--port 5432 \
--source-group sg-0aaaaaaaaaaaaaaaaNotice that no rule anywhere lists an IP address for the application or database tier. Instances can be replaced, scaled out, or moved to another Availability Zone without editing any rule.
What a reference matches
A rule that references a group matches traffic from the private IP addresses of network interfaces that have that group attached. It does not match the group's own rules, and it does not match public or Elastic IP addresses. Two consequences follow:
- Traffic must arrive from a private address. If the web tier reaches the application tier through its public IP, the source is not matched by the reference.
- The reference works across VPCs only when the VPCs are peered or connected through a supported mechanism, and the referenced group is in the peer VPC. Otherwise use CIDR ranges.
A reference also does not make the groups related. Adding web-sg as a source in app-sg doesn't give web-sg any of app-sg's rules. The relationship is one-way.
Outbound rules in a tiered design
New security groups created through the CLI or console allow all outbound traffic by default. That is acceptable for many web tiers but is looser than the design needs for the database. You can tighten outbound rules for a tier that has a small, known set of destinations.
A common pattern is to leave the web and application groups with the default outbound rule, because they need to reach package repositories, AWS APIs, and external services. For the database tier, which usually initiates no connections, remove the default outbound rule:
aws ec2 revoke-security-group-egress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--ip-permissions 'IpProtocol=-1,IpRanges=[{CidrIp=0.0.0.0/0}]'Be careful before you do this. A database host that must reach an AWS service, a patch mirror, or a monitoring endpoint will fail silently after the rule is removed. Managed services such as Amazon RDS handle their own patching and do not need outbound rules in your group for that purpose. If you run the database on EC2 yourself, plan the outbound destinations first.
Because rules are stateful, removing outbound access does not break replies to inbound connections. The application tier can still query the database and receive results.
Putting a load balancer in front
In most real designs the internet does not reach web servers directly. An Application Load Balancer sits in public subnets and forwards traffic to targets. The load balancer has its own security group, which takes over the public rule:
| Group | Inbound rule | Source |
|---|---|---|
| `alb-sg` | TCP 443 | `0.0.0.0/0` |
| `web-sg` | TCP 8080 | `alb-sg` |
| `app-sg` | TCP 9000 | `web-sg` |
| `db-sg` | TCP 5432 | `app-sg` |
With this change no instance in the design accepts traffic from the internet. Only the load balancer does. The same reference pattern applies at every hop.
If your instances run health checks from the load balancer, the check traffic originates from the load balancer's interfaces. The same reference covers it, provided the health check port is in the rule. A mismatch between the target port and the health check port is a common reason for targets reported as unhealthy.
Declaring the tiers in Terraform
Infrastructure as code keeps the tiers reproducible. Use one rule resource per rule so that each rule can change independently, and use referenced_security_group_id for references.
variable "vpc_id" {
type = string
}
resource "aws_security_group" "web" {
name = "web-sg"
description = "Web tier"
vpc_id = var.vpc_id
}
resource "aws_security_group" "app" {
name = "app-sg"
description = "Application tier"
vpc_id = var.vpc_id
}
resource "aws_security_group" "db" {
name = "db-sg"
description = "Database tier"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_ingress_rule" "web_https" {
security_group_id = aws_security_group.web.id
cidr_ipv4 = "0.0.0.0/0"
ip_protocol = "tcp"
from_port = 443
to_port = 443
description = "HTTPS from anywhere"
}
resource "aws_vpc_security_group_ingress_rule" "app_from_web" {
security_group_id = aws_security_group.app.id
referenced_security_group_id = aws_security_group.web.id
ip_protocol = "tcp"
from_port = 8080
to_port = 8080
description = "App port from web tier"
}
resource "aws_vpc_security_group_ingress_rule" "db_from_app" {
security_group_id = aws_security_group.db.id
referenced_security_group_id = aws_security_group.app.id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
description = "PostgreSQL from app tier"
}Terraform resolves the dependencies from the references, so it creates the groups first and the rules after. The descriptions are worth writing. They show up in the console and in describe-security-groups, and they tell a reviewer why the rule exists.
Common mistakes
- Using CIDR ranges between tiers. A rule such as "5432 from
10.0.2.0/24" lets every host in that subnet reach the database, including ones that are not part of the application tier. A reference limits access to interfaces with the right group. - Referencing a group from the wrong direction. The group that receives the traffic holds the rule, and the sender's group is the source. Putting the rule on the sender's group (as an inbound rule) has no effect on the receiving side.
- Opening the database to
0.0.0.0/0to debug. This is easy to forget. Test from an instance in the application tier, or use a temporary rule restricted to one/32address and remove it afterward. - Attaching the wrong group. An instance needs the right group attached, and an instance may carry several. Rules from all attached groups combine, so a leftover broad group (such as the default group) can undo your tight design.
- Forgetting that references need private addresses. Calls between tiers must use private DNS names or private IPs.
Verifying the design
After applying the rules, inspect each group and confirm the sources are groups, not ranges:
aws ec2 describe-security-groups \
--group-ids sg-0bbbbbbbbbbbbbbbb \
--query 'SecurityGroups[0].IpPermissions'The output for db-sg should show one permission with port 5432 and a UserIdGroupPairs entry pointing to app-sg, and an empty IpRanges list. Then test from an application instance with a TCP connection check to the database host on port 5432. A test from a web instance to the same address should time out. A timeout, instead of a "connection refused" error, is the typical sign of a security group drop, because the packet is discarded rather than rejected by the host.
Exercise
A database group db-sg has one inbound rule: TCP 5432 from app-sg. An application instance has app-sg attached and connects to the database's public IP address, and the connection times out. The database's private address would work. Why does the connection fail, and what should you change?
Show answer
A rule that references a security group matches traffic from the private IP addresses of interfaces that have that group attached. A connection to the public address does not arrive from a matched private source, so the rule does not allow it. Change the application to connect using the database's private DNS name or private IP address. Do not widen the rule to 0.0.0.0/0.
Exercise
You want to add an Application Load Balancer in front of the web tier, so that no web instance accepts traffic from the internet. Which rules change, and what is the new source of the web tier's inbound rule?
Show answer
Create an alb-sg group with an inbound rule for TCP 443 from 0.0.0.0/0 and attach it to the load balancer. Remove the 0.0.0.0/0 rule from web-sg and add an inbound rule for the target port that uses alb-sg as its source. The application and database rules do not change.