Sub topic 3
Designing Web, Application, and Database Tiers
Assume you know IP addresses, CIDR notation, TCP ports, EC2, VPCs, subnets, and basic AWS CLI usage. A security group is a stateful, instance-level firewall. Use separate groups for each application tier so rules express which tier may call another.
caption: Security group references between application tiers
alt: Internet reaches the web tier, the web tier reaches the application tier, and the application tier reaches the database tier.
flowchart TD
Internet -->|HTTPS 443| Web[Web security group]
Web -->|TCP 8080| App[Application security group]
App -->|TCP 5432| DB[Database security group]A typical design allows public HTTPS to the web group. The application group allows port 8080 only from the web group. The database group allows PostgreSQL port 5432 only from the application group.
| Tier | Inbound source | Port |
|---|---|---|
| Web | `0.0.0.0/0` or a load balancer group | 443 |
| Application | Web security group | 8080 |
| Database | Application security group | 5432 |
Least Privilege and Administrative Access
Least privilege means allowing only the required source, protocol, and port. Avoid 0.0.0.0/0 for SSH or database ports. Permit SSH from a controlled administrator CIDR, use Systems Manager Session Manager where possible, and record rule descriptions.
Security group rules are allow-only. If no rule allows traffic, AWS denies it. Rules have no priority order, and rules from multiple attached groups are combined. Return traffic for an allowed connection is automatically permitted because security groups track connections.
Managing Rules with the AWS CLI and Terraform
Create and inspect rules with the CLI:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 8080 \
--source-group sg-0123456789abcdef0
aws ec2 describe-security-groups \
--group-ids sg-0aaaaaaaaaaaaaaaaFor repeatable environments, Terraform keeps the rule definition in version control:
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 = "Requests from web tier"
}Security Groups Versus Network ACLs
Security groups apply to elastic network interfaces and are stateful. Network ACLs apply to subnets, are stateless, and support both allow and deny rules evaluated by rule number.
Troubleshooting Blocked Connections
Check the destination group, attached network interface, route tables, subnet network ACL, and instance listener. Confirm that the source matches the rule. A security group reference means the source interface has that group attached; it does not mean traffic came from the group’s private CIDR.
> callout note > Test from the actual source tier. Testing from your laptop can produce a different result because its IP and security groups differ.
Quotas and Manageable Rule Sets
Security groups and rules have regional quotas. Keep groups focused, remove obsolete rules, and use prefix lists for managed sets of network ranges. Split large designs by tier instead of adding broad, duplicated rules.
Exercise
The application cannot connect to the database. Which rule should you inspect first?
Show answer
Inspect the database security group for an inbound TCP 5432 rule whose source is the application security group, then check routes, network ACLs, and the database listener.