VPC Fundamentals for Developers
Exam scope: DVA-C02 tests VPC in the context of Lambda, RDS, ElastiCache, and ECS โ not deep network engineering. Focus on the developer scenarios.
VPC Building Blocksโ
VPC (10.0.0.0/16)
โโโ Public Subnet (10.0.1.0/24) โ Internet Gateway attached โ internet access
โ โโโ NAT Gateway
โ โโโ Load Balancer
โ
โโโ Private Subnet (10.0.10.0/24) โ No direct internet route
โโโ Lambda (in VPC)
โโโ EC2 App Servers
โโโ RDS / ElastiCache
Security Groups vs Network ACLsโ
| Feature | Security Groups | Network ACLs (NACLs) |
|---|---|---|
| Applies to | EC2 instances, ENIs | Subnets |
| State | Stateful (return traffic auto-allowed) | Stateless (must define both inbound + outbound) |
| Rules | Allow only (no explicit deny) | Allow and Deny |
| Evaluation | All rules evaluated | Rules evaluated in order (lowest number first) |
| Default behavior | Deny all in, allow all out | Allow all (default NACL) |
Stateful (Security Groups): If you allow inbound port 80, the response is automatically allowed out. Stateless (NACLs): You must explicitly allow BOTH inbound port 80 AND outbound ephemeral ports (1024โ65535).
NAT Gatewayโ
Allows private subnet resources (Lambda, EC2) to access the internet outbound only:
Private Lambda โ NAT Gateway (public subnet) โ Internet Gateway โ Internet
โ
Elastic IP attached
- NAT Gateway must be in a public subnet
- Costs money per hour + per GB processed
- Fully managed (vs NAT Instance which requires maintenance)
A Lambda function inside a VPC has no internet access by default. To call external APIs (Stripe, Twilio...), add a NAT Gateway.
Cheaper alternative: Use VPC Endpoints for AWS services (DynamoDB, S3, SQS) โ no NAT needed.
VPC Endpointsโ
Access AWS services privately โ traffic stays within AWS backbone, no internet required.
| Type | Services | Description |
|---|---|---|
| Gateway Endpoint | S3, DynamoDB | Free โ added to route table |
| Interface Endpoint (PrivateLink) | Most AWS services (SQS, SNS, Lambda, Secrets Manager...) | ENI in your subnet โ costs per hour + per GB |
# Without VPC Endpoint:
Lambda (private subnet) โ NAT Gateway โ Internet โ DynamoDB endpoint
($$$, public internet)
# With Gateway Endpoint for DynamoDB:
Lambda (private subnet) โ VPC Endpoint โ DynamoDB
(free, private, no NAT needed)
When to Use VPC Endpointsโ
- Lambda in VPC accessing DynamoDB/S3 โ Gateway Endpoint (free)
- Lambda in VPC accessing SQS, SNS, Secrets Manager โ Interface Endpoint
- Eliminate NAT Gateway costs for AWS service traffic
Lambda in VPCโ
// SAM โ Lambda in VPC
MyFunction:
Type: AWS::Serverless::Function
Properties:
VpcConfig:
SecurityGroupIds:
- !Ref LambdaSecurityGroup
SubnetIds:
- !Ref PrivateSubnet1
- !Ref PrivateSubnet2 # Multiple AZs for resilience
Lambda + VPC Cold Startsโ
Previously, Lambda VPC cold starts were slow (ENI creation took ~10s). Since 2020, AWS uses Hyperplane ENIs โ cold starts are similar to non-VPC functions.
Common Lambda in VPC Patternโ
Lambda (private subnet)
โโโ RDS / ElastiCache โ via Security Group (same VPC)
โโโ DynamoDB / S3 โ via Gateway VPC Endpoint (no internet)
โโโ SQS / Secrets Manager โ via Interface VPC Endpoint
โโโ External API (Stripe, etc.) โ via NAT Gateway
ECS in VPCโ
# Fargate task in VPC โ awsvpc network mode (required for Fargate)
TaskDefinition:
NetworkMode: awsvpc # Each task gets its own ENI and private IP
With awsvpc, you apply Security Groups directly to tasks โ not to the host.
๐งช Practice Questionsโ
Q1. A Lambda function in a VPC needs to call the DynamoDB API. No NAT Gateway is configured. What is the MOST cost-effective solution?
A) Add a NAT Gateway to allow internet access
B) Move Lambda outside the VPC
C) Add a Gateway VPC Endpoint for DynamoDB in the route table
D) Use DynamoDB Local
โ Answer & Explanation
C โ A Gateway VPC Endpoint for DynamoDB is free and routes traffic through the AWS backbone โ no internet required, no NAT costs. It's the recommended solution for Lambda accessing DynamoDB/S3 from within a VPC.
Q2. A Security Group allows inbound TCP port 443. A NACL also allows inbound TCP port 443 but has no outbound rule for ephemeral ports. What happens to HTTPS responses?
A) Responses flow normally โ Security Groups handle the return traffic
B) Responses are blocked โ NACLs are stateless and require an explicit outbound ephemeral port rule
C) The NACL is ignored because Security Group takes precedence
D) Only the first packet is blocked
โ Answer & Explanation
B โ NACLs are stateless โ they don't automatically allow return traffic. You must add an outbound rule for ephemeral ports (1024โ65535) to allow HTTPS responses. Security Groups are stateful, but both layers apply.
Q3. A Lambda function in a private VPC subnet needs to call an external HTTPS API (not an AWS service). What network component is required?
A) VPC Gateway Endpoint
B) Internet Gateway
C) NAT Gateway in a public subnet
D) VPC Peering
โ Answer & Explanation
C โ For external internet access from a private subnet, you need a NAT Gateway in a public subnet. VPC Endpoints only work for AWS services. Internet Gateway allows internet access but must be attached to a public subnet and requires a public IP.