/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.XRay.Recorder.Handlers.AwsSdk;
// 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 {{ cookiecutter.project_name }};
public class Function
{
private static HttpClient? _httpClient;
///
/// Default constructor that Lambda will invoke.
///
public Function()
{
_httpClient = new HttpClient();
AWSSDKHandler.RegisterXRayForAllServices();
}
///
/// Test constructor
///
public Function(HttpClient httpClient)
{
_httpClient = httpClient;
}
///
/// Lambda Handler
///
/// API Gateway Proxy event
/// AWS Lambda context
/// API Gateway Proxy response
public async Task FunctionHandler(APIGatewayProxyRequest apigwProxyEvent, ILambdaContext context)
{
var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;
Console.WriteLine("Getting ip address from external service");
var watch = Stopwatch.StartNew();
var location = await GetCallingIp().ConfigureAwait(false);
watch.Stop();
var lookupRecord = new LookupRecord(requestContextRequestId,
"Hello AWS Lambda Powertools for .NET", location, (int)watch.ElapsedMilliseconds);
return new APIGatewayProxyResponse
{
Body = JsonSerializer.Serialize(lookupRecord),
StatusCode = 200,
Headers = new Dictionary { { "Content-Type", "application/json" } }
};
}
///
/// Calls location api to return IP address
///
/// IP address string
private static async Task GetCallingIp()
{
if (_httpClient == null) return "0.0.0.0";
_httpClient.DefaultRequestHeaders.Accept.Clear();
_httpClient.DefaultRequestHeaders.Add("User-Agent", "AWS Lambda .Net Client");
try
{
Console.WriteLine("Calling Check IP API");
var response = await _httpClient.GetStringAsync("https://checkip.amazonaws.com/").ConfigureAwait(false);
var ip = response.Replace("\n", "");
Console.WriteLine($"API response returned {ip}");
return ip;
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
}
///
/// LookupRecord class represents the data structure of location lookup
///
[Serializable]
public class LookupRecord
{
public LookupRecord()
{
}
///
/// Create new LookupRecord
///
/// Id of the lookup
/// Greeting phrase
/// IP address
/// Lookup execution time
public LookupRecord(string? lookupId, string? greeting, string? ipAddress, int requestExecutionTime)
{
LookupId = lookupId;
Greeting = greeting;
IpAddress = ipAddress;
RequestExecutionTime = requestExecutionTime;
}
public string? LookupId { get; set; }
public string? Greeting { get; set; }
public string? IpAddress { get; set; }
public int RequestExecutionTime { get; set; }
}