using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Net.Http; using System.Text.Json; using Amazon.Lambda.Core; using Amazon.Lambda.APIGatewayEvents; // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace HelloWorld { public class Function { private static readonly HttpClient client = new HttpClient(); private static async Task GetCallingIP() { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client"); var msg = await client.GetStringAsync("http://checkip.amazonaws.com/").ConfigureAwait(continueOnCapturedContext:false); return msg.Replace("\n",""); } public async Task FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context) { var location = await GetCallingIP(); var body = new Dictionary { { "message", "hello world" }, { "location", location } }; return new APIGatewayProxyResponse { Body = JsonSerializer.Serialize(body), StatusCode = 200, Headers = new Dictionary { { "Content-Type", "application/json" } } }; } } }