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

Upgrade Lambda Serverless ASP.NET Core Web API to .NET Core 2.2 with custom runtime #1

Open
wants to merge 1 commit into
base: master
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
10 changes: 8 additions & 2 deletions AWSServerless1/AWSServerless1.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.4" />
<Content Include="bootstrap">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="3.0.3" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.0.0" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
</ItemGroup>
</Project>
24 changes: 23 additions & 1 deletion AWSServerless1/LocalEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.Json;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using System;
using System.Threading.Tasks;

namespace AWSServerless1
{
/// <summary>
/// The Main function can be used to run the ASP.NET Core application locally using the Kestrel webserver.
/// It is now also the main entry point for the custom runtime.
/// </summary>
public class LocalEntryPoint
{
public static void Main(string[] args)
private static readonly LambdaEntryPoint LambdaEntryPoint = new LambdaEntryPoint();
private static readonly Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>> Func = LambdaEntryPoint.FunctionHandlerAsync;

public static async Task Main(string[] args)
{
#if DEBUG
BuildWebHost(args).Run();
#else
// Wrap the FunctionHandler method in a form that LambdaBootstrap can work with.
using (var handlerWrapper = HandlerWrapper.GetHandlerWrapper(Func, new JsonSerializer()))

// Instantiate a LambdaBootstrap and run it.
// It will wait for invocations from AWS Lambda and call the handler function for each one.
using (var bootstrap = new LambdaBootstrap(handlerWrapper))
{
await bootstrap.RunAsync();
}
#endif
}

public static IWebHost BuildWebHost(string[] args) =>
Expand Down
2 changes: 1 addition & 1 deletion AWSServerless1/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
Expand Down
5 changes: 3 additions & 2 deletions AWSServerless1/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
"profile": "default",
"region": "ap-southeast-2",
"configuration": "Release",
"framework": "netcoreapp2.1",
"framework": "netcoreapp2.2",
"template": "serverless.template",
"s3-bucket": "aws-serverless-md",
"stack-name": "AWSServerless1"
"stack-name": "AWSServerless1",
"msbuild-parameters": "--self-contained true"
}
4 changes: 4 additions & 0 deletions AWSServerless1/bootstrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
# This is the script that the Lambda host calls to start the custom runtime.

/var/task/AWSServerless1
4 changes: 2 additions & 2 deletions AWSServerless1/serverless.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"AspNetCoreFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "AWSServerless1::AWSServerless1.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnetcore2.1",
"Handler": "not_required_for_custom_runtime",
"Runtime": "provided",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 30,
Expand Down
2 changes: 1 addition & 1 deletion AWSServerless1/web.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false">
<environmentVariables />
Expand Down