- Newest
- Most votes
- Most comments
When you deploy a Lambda function in a VPC, it only receives private IP addresses and cannot directly access the internet, even if the subnet has an Internet Gateway (IGW) attached. This is why you're experiencing timeouts when trying to use an IGW.
To answer your questions:
-
No, you cannot replace the NAT Gateway with an IGW for Lambda functions in a VPC. Lambda functions in a VPC can only use private IP addresses and cannot be directly attached to an IGW, which requires public IP addresses. This is why your requests time out when using an IGW but succeed with a NAT Gateway.
-
There are a few more cost-efficient alternatives to NAT Gateways:
-
IPv6 approach: If Cognito supports IPv6 (you'd need to check if it has AAAA DNS records), you could configure your VPC with dual-stack subnets and use an Egress-Only Internet Gateway (EIGW) instead of a NAT Gateway. This doesn't incur the fixed hourly costs or data processing fees of a NAT Gateway.
-
Interface VPC Endpoints (AWS PrivateLink): While there isn't a direct VPC endpoint for Cognito, you could restructure your application to handle Cognito interactions from components that aren't in the VPC.
-
Lambda router: You could have a Lambda function outside the VPC that handles Cognito interactions and communicates with your VPC-based Lambda functions.
The most common and straightforward solution is to use a NAT Gateway, but it does come with fixed hourly costs and data processing fees. If your traffic volume is low, the IPv6 approach with an Egress-Only Internet Gateway might be the most cost-effective alternative if Cognito supports IPv6.
Sources
Connecting Lambda inside a VPC to resources outside a VPC | AWS re:Post
NAT Gateway for outbound request | AWS re:Post
The IPv6 solution worked for me.
Relevant content
- asked 3 years ago
