Skip to content
This repository was archived by the owner on Jul 16, 2022. It is now read-only.

Implement Lambda warmer #2

Open
wants to merge 1 commit into
base: netcore-22-runtime-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions AWSServerless1/APIGatewayProxyFunction.cs
Original file line number Diff line number Diff line change
@@ -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<APIGatewayProxyResponse> 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);
}
}
}
1 change: 1 addition & 0 deletions AWSServerless1/AWSServerless1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="3.0.3" />
<PackageReference Include="AWSSDK.Lambda" Version="3.3.101" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.0.0" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
</ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion AWSServerless1/serverless.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
"Path": "/",
"Method": "ANY"
}
}
},
"APIWarmingSchedule": {
"Type": "Schedule",
"Properties": {
"Schedule": "rate(5 minutes)",
"Input": "{ \"Resource\": \"WarmingLambda\", \"Body\": \"5\" }"
}
}
}
}
}
Expand Down