Skip to main content

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โ€‹

FeatureSecurity GroupsNetwork ACLs (NACLs)
Applies toEC2 instances, ENIsSubnets
StateStateful (return traffic auto-allowed)Stateless (must define both inbound + outbound)
RulesAllow only (no explicit deny)Allow and Deny
EvaluationAll rules evaluatedRules evaluated in order (lowest number first)
Default behaviorDeny all in, allow all outAllow all (default NACL)
Exam hint

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)
Lambda in VPC needs NAT for internet

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.

TypeServicesDescription
Gateway EndpointS3, DynamoDBFree โ€” 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.


๐Ÿ”— Resourcesโ€‹