diff --git a/AWSServerless1/APIGatewayProxyFunction.cs b/AWSServerless1/APIGatewayProxyFunction.cs new file mode 100644 index 0000000..51fa727 --- /dev/null +++ b/AWSServerless1/APIGatewayProxyFunction.cs @@ -0,0 +1,43 @@ +using Amazon.Lambda; +using Amazon.Lambda.APIGatewayEvents; +using Amazon.Lambda.Core; +using Newtonsoft.Json; +using System; +using System.Threading.Tasks; + +namespace AWSServerless1 +{ + public abstract class APIGatewayProxyFunction : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction + { + public override async Task FunctionHandlerAsync(APIGatewayProxyRequest request, + ILambdaContext lambdaContext) + { + Console.WriteLine("In overridden FunctionHandlerAsync..."); + + if (request.Resource == "WarmingLambda") + { + int.TryParse(request.Body, out var concurrencyCount); + + if (concurrencyCount > 1) + { + Console.WriteLine($"Warming instance {concurrencyCount}."); + var client = new AmazonLambdaClient(); + await client.InvokeAsync(new Amazon.Lambda.Model.InvokeRequest + { + FunctionName = lambdaContext.FunctionName, + InvocationType = InvocationType.RequestResponse, + Payload = JsonConvert.SerializeObject(new APIGatewayProxyRequest + { + Resource = request.Resource, + Body = (concurrencyCount - 1).ToString() + }) + }); + } + + return new APIGatewayProxyResponse { }; + } + + return await base.FunctionHandlerAsync(request, lambdaContext); + } + } +} diff --git a/AWSServerless1/AWSServerless1.csproj b/AWSServerless1/AWSServerless1.csproj index 931a48b..9d09d69 100644 --- a/AWSServerless1/AWSServerless1.csproj +++ b/AWSServerless1/AWSServerless1.csproj @@ -12,6 +12,7 @@ + diff --git a/AWSServerless1/serverless.template b/AWSServerless1/serverless.template index a9fdfe7..6f6e127 100644 --- a/AWSServerless1/serverless.template +++ b/AWSServerless1/serverless.template @@ -29,7 +29,14 @@ "Path": "/", "Method": "ANY" } - } + }, + "APIWarmingSchedule": { + "Type": "Schedule", + "Properties": { + "Schedule": "rate(5 minutes)", + "Input": "{ \"Resource\": \"WarmingLambda\", \"Body\": \"5\" }" + } + } } } }