Chapter 83 · Enabling Lambda VPC Internet Access
Subchapter 83.1
references/lambda-vpc-internet-access.mdMarkdown11 KBView on GitHub
This SOP guides you through enabling internet access for a Lambda function that currently exists in a VPC subnet without internet access. Lambda functions in VPC cannot receive public IP addresses, so the only way to provide internet access is through NAT Gateway infrastructure that routes traffic from private subnets to the internet.
Constraints for parameter acquisition:
Check for required tools and warn the user if any are missing.
Constraints:
Retrieve and analyze the current Lambda function configuration to understand its VPC setup.
Constraints:
# Get Lambda function configuration
aws lambda get-function --function-name <lambda_function_name>
# Check subnet details
aws ec2 describe-subnets --subnet-ids <subnet_id>
# Check route tables for the subnet
aws ec2 describe-route-tables --filters "Name=association.subnet-id,Values=<subnet_id>"Examine the VPC’s current networking setup to determine what infrastructure needs to be created.
Constraints:
# Check all route tables in VPC
aws ec2 describe-route-tables --filters "Name=vpc-id,Values=<vpc_id>"
# Check all subnets in VPC
aws ec2 describe-subnets --filters "Name=vpc-id,Values=<vpc_id>"
# Check for existing Internet Gateway
aws ec2 describe-internet-gateways --filters "Name=attachment.vpc-id,Values=<vpc_id>"
# Check for existing NAT Gateways
aws ec2 describe-nat-gateways --filter "Name=vpc-id,Values=<vpc_id>"Plan the NAT Gateway setup based on the VPC analysis.
Constraints:
Present the planned infrastructure changes and estimated costs to the user for explicit approval before creating any resources.
Constraints:
Create an Internet Gateway if one doesn’t exist and attach it to the VPC.
Constraints:
# Create Internet Gateway
aws ec2 create-internet-gateway --tag-specifications "ResourceType=internet-gateway,Tags=[{Key=Name,Value=<lambda_function_name>-igw}]"
# Attach Internet Gateway to VPC
aws ec2 attach-internet-gateway --internet-gateway-id <internet_gateway_id> --vpc-id <vpc_id>Create a public subnet for NAT Gateway placement if one doesn’t exist.
Constraints:
# Create public subnet
aws ec2 create-subnet --vpc-id <vpc_id> --cidr-block <public_subnet_cidr> --availability-zone <availability_zone> --tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=<lambda_function_name>-public-subnet}]"
# Create route table for public subnet
aws ec2 create-route-table --vpc-id <vpc_id> --tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=<lambda_function_name>-public-rt}]"
# Add route to Internet Gateway
aws ec2 create-route --route-table-id <public_route_table_id> --destination-cidr-block 0.0.0.0/0 --gateway-id <internet_gateway_id>
# Associate subnet with route table
aws ec2 associate-route-table --subnet-id <public_subnet_id> --route-table-id <public_route_table_id>Create the NAT Gateway and configure routing for Lambda internet access.
Constraints:
# Allocate Elastic IP
aws ec2 allocate-address --domain vpc --tag-specifications "ResourceType=elastic-ip,Tags=[{Key=Name,Value=<lambda_function_name>-nat-eip}]"
# Create NAT Gateway
aws ec2 create-nat-gateway --subnet-id <public_subnet_id> --allocation-id <elastic_ip_allocation_id>
# Create private route table
aws ec2 create-route-table --vpc-id <vpc_id> --tag-specifications "ResourceType=route-table,Tags=[{Key=Name,Value=<lambda_function_name>-private-rt}]"
# Check NAT Gateway status
aws ec2 describe-nat-gateways --nat-gateway-ids <nat_gateway_id>
# Add route to NAT Gateway
aws ec2 create-route --route-table-id <private_route_table_id> --destination-cidr-block 0.0.0.0/0 --nat-gateway-id <nat_gateway_id>
# Associate private subnet with private route table
aws ec2 associate-route-table --subnet-id <lambda_subnet_id> --route-table-id <private_route_table_id>Ensure security groups allow necessary outbound internet traffic.
Constraints:
# Check current security group rules
aws ec2 describe-security-groups --group-ids <security_group_id>
# Add HTTPS outbound rule if needed
aws ec2 authorize-security-group-egress --group-id <security_group_id> --protocol tcp --port 443 --cidr 0.0.0.0/0
# Add HTTP outbound rule if needed
aws ec2 authorize-security-group-egress --group-id <security_group_id> --protocol tcp --port 80 --cidr 0.0.0.0/0{
"IpPermissions": [
{
"IpProtocol": "tcp",
"FromPort": 443,
"ToPort": 443,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}]
},
{
"IpProtocol": "tcp",
"FromPort": 80,
"ToPort": 80,
"IpRanges": [{"CidrIp": "0.0.0.0/0"}]
}
]
}If the NAT Gateway is created but Lambda still cannot access the internet, check that the route table associated with Lambda’s subnets has a route to the NAT Gateway for 0.0.0.0/0 destination.
If Lambda function times out when trying to access the internet, verify that security group outbound rules allow the necessary ports and that the NAT Gateway or Internet Gateway is properly configured.
If network changes don’t resolve the issue immediately, you should remember that VPC networking changes can take a minute or two to propagate through AWS’s infrastructure. Wait up to 1-2 minutes after creating NAT Gateway and updating route tables before testing again.
If Lambda still cannot access the internet after NAT Gateway creation, verify that the Lambda function’s subnets are associated with the correct route table that has the 0.0.0.0/0 route pointing to the NAT Gateway.