I am trying to set up a basic .NET Core 2.1 Lambda function that can access private VPC web APIs as well as public Internet web APIs. However, as soon as the function is associated with a VPC, it can no longer access the public Internet.
I tried the following (based on this YouTube tutorial):
- Created a new private subnet with CIDR block 172.18.10.0/24 .
- Created a new NAT gateway and associated with a public subnet that is already included in a route table with an Internet gateway at 0.0.0.0/0 and created a new EIP for the NAT gateway.
- Created a new route table, added Destination 0.0.0.0/0 to the NAT gateway and associated the route table with the newly created private subnet.
In the Lambda function:
- Added function to the VPC.
- Added function to the newly created private subnet.
- Added security group with the following: Inbound Port 0 - 65535, Outbound Ports All Destination 0.0.0.0/0 .
Here is the Lambda function I am testing:
using System.Net.Http;
using System.Net;
using System;
using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace MyFunction
{
public class Function
{
public void FunctionHandler(SQSEvent sqsEvent, ILambdaContext context)
{
using (HttpClient client = new HttpClient())
{
Console.WriteLine($"Sending HTTP request.");
HttpResponseMessage response = client.GetAsync($"http://dummy.restapiexample.com/api/v1/employee/18677").Result;
Console.WriteLine($"Response received.");
var content = response.Content.ReadAsStringAsync().Result;
Console.WriteLine($"Response content: {content}");
}
}
}
}
When triggering this function, the output is the following:
Sending HTTP request.
One or more errors occurred. (Resource temporarily unavailable): AggregateException
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
at MyFunction.Function.FunctionHandler(SQSEvent sqsEvent, ILambdaContext context)
in C:\__temp__\basic_lambda_func\Function.cs:line 20 at System.
When the Lambda function is not on the VPC it works fine. Any ideas what might be causing the issues on the VPC?