- Newest
- Most votes
- Most comments
First a small correction. Lambda functions never run in your VPC. They always run in the Lambda service VPC. When you configure if for VPC access, we create an ENI in your VPC and route all the Lambda's network traffic via that ENI.
Regardless of VPC configuration, the invocation of the Lambda function is always via the public Invoke API. This is also the way that API Gateway invokes your function, both for REST and WebSocket requests.
If your Lambda function is attached to a VPC and it needs to send messages via the WebSocket to the client, it needs to be able and communicate with API Gateway's public @connections URL. For that you need a VPC Endpoint for API Gateway or a NAT Gateway configured in the VPC with the appropriate routing and permissions.
I am not sure where you are getting the Forbidden error from, and not sure where is the client in your second use case. If your client is also running in the VPC, it also needs to have access to the public API's endpoint.
Thank you for providing a detailed description of your setup and the scenarios you've encountered. Let's analyze the situation and provide some insights on what might be causing the issue in your second scenario.
The behavior you're experiencing is likely related to the network configuration of your VPC and how Lambda functions interact with API Gateway when placed inside a VPC.
In the first scenario, where the request comes from your local machine through API Gateway to the Lambda function inside the VPC, it works because API Gateway can invoke Lambda functions regardless of their VPC configuration.
For the second scenario, where the Lambda function inside the VPC is trying to make a request to the API Gateway WebSocket API, the issue is likely related to network connectivity. Even though you've opened all ports and assigned admin roles, the Lambda function inside the VPC doesn't have direct internet access by default.
To resolve this, you need to ensure that your VPC is properly configured to allow outbound internet access for the Lambda function. This typically involves:
- Configuring a NAT Gateway in a public subnet of your VPC.
- Updating the route table associated with the private subnet where your Lambda function resides to route internet-bound traffic (0.0.0.0/0) through the NAT Gateway.
Additionally, you should verify that:
- The security group associated with your Lambda function allows outbound traffic to the API Gateway endpoint.
- The Network ACLs for both the subnet containing the Lambda function and the subnet containing the NAT Gateway allow the necessary outbound and inbound traffic.
If you've already set up a NAT Gateway as mentioned in your description, double-check its configuration and ensure that the route table for the private subnet is correctly pointing to the NAT Gateway for internet-bound traffic.
For the VPC Endpoint, make sure it's configured for API Gateway and that the endpoint policy allows access to your specific API.
The third scenario works because when the Lambda function is outside the VPC, it has direct internet access by default and can reach the API Gateway without additional networking configuration.
If you've verified all these settings and are still encountering issues, you may want to check CloudWatch Logs for any specific error messages from your Lambda function, and consider using VPC Flow Logs to analyze the network traffic and identify any potential blockages.
Sources
Networking and VPC configurations - AWS Lambda
Enable internet access for VPC-connected Lambda functions - AWS Lambda
Amazon API Gateway Features | API Management | Amazon Web Services
answered 2 years ago
Relevant content
asked 5 years ago

So, I already create VPC Endpoint to VPC that my Lambda is attached to. I also create NAT Gateway. Both I tried to configure on my Routing table. But none of them worked. I've read somewhere that websocket cannot use internal connection, and must use public connection. I've check that my lambda can connect to public network, even in VPC. Anything else I can check?