AWS ‐ Best Practices - FullstackCodingGuy/Developer-Fundamentals GitHub Wiki
read
When designing secure, scalable cloud architectures, it's important to separate public-facing resources from private, internal resources. AWS uses Public Subnets and Private Subnets to achieve this.
Feature | Public Subnet | Private Subnet |
---|---|---|
Internet Access | Yes (via Internet Gateway) | No (needs NAT Gateway) |
Use Case | Hosting public resources (e.g., web servers, API gateways) | Hosting internal resources (e.g., databases, backend apps) |
Route Table | Direct route to Internet Gateway | No direct internet access (uses a NAT Gateway if needed) |
Security | Exposed to the internet | Internal network, not publicly accessible |
Connectivity | Public IPs allowed | Private IPs only |
1️⃣ Security
- Public Subnet: Hosts resources that need internet access (e.g., a web server).
- Private Subnet: Keeps sensitive resources isolated (e.g., databases, backend services).
2️⃣ Best Practice
- AWS recommends placing databases and internal apps in private subnets for security.
- Only expose what’s necessary via Load Balancers or API Gateways in a public subnet.
3️⃣ Outbound Internet Access (for Private Subnet)
- Private subnets cannot access the internet directly.
- To download packages (e.g., npm modules in Lambda), you need a NAT Gateway in a Public Subnet.
VPC (10.0.0.0/16)
│
├── Public Subnet (10.0.1.0/24)
│ ├── NAT Gateway (for outbound access)
│ ├── API Gateway / ALB (for external requests)
│
├── Private Subnet (10.0.2.0/24)
│ ├── Lambda Function
│ ├── DocumentDB
│ ├── RDS Database
│
└── Internet Gateway (for public access)
- User → API Gateway → Lambda (in Private Subnet) → DocumentDB
- Lambda (in Private Subnet) → NAT Gateway (in Public Subnet) → Internet (for updates)
🚀 NO!
- Lambda needs to be inside a Private Subnet to connect to DocumentDB.
- But if Lambda needs internet access (e.g., fetching dependencies), then a NAT Gateway in a Public Subnet is needed.
- Public Subnet → For internet-facing resources (APIs, ALB, NAT Gateway).
- Private Subnet → For secure backend resources (Databases, internal services).
- Use a NAT Gateway in a Public Subnet if private resources need outbound internet access.