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

Commit 5fab067

Browse files
Upgrade Lambda Serverless ASP.NET Core Web API to .NET Core 3.0 with custom runtime
1 parent 40e4451 commit 5fab067

12 files changed

+115
-54
lines changed

Diff for: AWSServerless1/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!/nuget-cache/Amazon.Lambda.AspNetCoreServer.4.0.0-preview2.nupkg

Diff for: AWSServerless1/AWSServerless1.csproj

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
2-
<PropertyGroup>
3-
<TargetFramework>netcoreapp2.1</TargetFramework>
4-
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
5-
<AWSProjectType>Lambda</AWSProjectType>
6-
</PropertyGroup>
7-
<ItemGroup>
8-
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.4" />
9-
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="3.0.3" />
10-
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
11-
</ItemGroup>
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
5+
<AWSProjectType>Lambda</AWSProjectType>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Content Include="bootstrap">
9+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
10+
</Content>
11+
</ItemGroup>
12+
<ItemGroup>
13+
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="4.0.0-preview2" />
14+
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.0.0" />
15+
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
16+
</ItemGroup>
1217
</Project>

Diff for: AWSServerless1/LocalEntryPoint.cs

-21
This file was deleted.

Diff for: AWSServerless1/Program.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Amazon.Lambda.APIGatewayEvents;
2+
using Amazon.Lambda.Core;
3+
using Amazon.Lambda.RuntimeSupport;
4+
using Amazon.Lambda.Serialization.Json;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Hosting;
7+
using System;
8+
using System.Threading.Tasks;
9+
10+
namespace AWSServerless1
11+
{
12+
/// <summary>
13+
/// The Main function can be used to run the ASP.NET Core application locally using the Kestrel webserver.
14+
/// It is now also the main entry point for the custom runtime.
15+
/// </summary>
16+
public class Program
17+
{
18+
public static async Task Main(string[] args)
19+
{
20+
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME")))
21+
{
22+
CreateHostBuilder(args).Build().Run();
23+
}
24+
else
25+
{
26+
var lambdaEntry = new LambdaEntryPoint();
27+
var functionHandler = (Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>>)(lambdaEntry.FunctionHandlerAsync);
28+
using var handlerWrapper = HandlerWrapper.GetHandlerWrapper(functionHandler, new JsonSerializer());
29+
using var bootstrap = new LambdaBootstrap(handlerWrapper);
30+
await bootstrap.RunAsync();
31+
}
32+
}
33+
34+
public static IHostBuilder CreateHostBuilder(string[] args) =>
35+
Host.CreateDefaultBuilder(args)
36+
.ConfigureWebHostDefaults(webBuilder =>
37+
{
38+
webBuilder.UseStartup<Startup>();
39+
});
40+
}
41+
}

Diff for: AWSServerless1/Startup.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3-
using Microsoft.AspNetCore.Mvc;
43
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Hosting;
66

77
namespace AWSServerless1
88
{
@@ -13,16 +13,16 @@ public Startup(IConfiguration configuration)
1313
Configuration = configuration;
1414
}
1515

16-
public static IConfiguration Configuration { get; private set; }
16+
public IConfiguration Configuration { get; }
1717

18-
// This method gets called by the runtime. Use this method to add services to the container
18+
// This method gets called by the runtime. Use this method to add services to the container.
1919
public void ConfigureServices(IServiceCollection services)
2020
{
21-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
21+
services.AddControllers();
2222
}
2323

24-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
25-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
24+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
25+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2626
{
2727
if (env.IsDevelopment())
2828
{
@@ -33,8 +33,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3333
app.UseHsts();
3434
}
3535

36+
app.UseRouting();
3637
app.UseHttpsRedirection();
37-
app.UseMvc();
38+
39+
app.UseEndpoints(endpoints =>
40+
{
41+
endpoints.MapControllers();
42+
});
3843
}
3944
}
4045
}

Diff for: AWSServerless1/aws-lambda-tools-defaults.json

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
2-
"Information": [
3-
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
4-
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
5-
"dotnet lambda help",
6-
"All the command line options for the Lambda command can be specified in this file."
7-
],
8-
"profile": "default",
9-
"region": "ap-southeast-2",
10-
"configuration": "Release",
11-
"framework": "netcoreapp2.1",
12-
"template": "serverless.template",
13-
"s3-bucket": "aws-serverless-md",
14-
"stack-name": "AWSServerless1"
2+
"Information": [
3+
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
4+
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
5+
"dotnet lambda help",
6+
"All the command line options for the Lambda command can be specified in this file."
7+
],
8+
"profile": "default",
9+
"region": "ap-southeast-2",
10+
"configuration": "Release",
11+
"framework": "netcoreapp3.0",
12+
"template": "serverless.template",
13+
"s3-bucket": "aws-serverless-md",
14+
"stack-name": "AWSServerless1",
15+
"msbuild-parameters": "--self-contained true /p:PublishReadyToRun=true"
1516
}

Diff for: AWSServerless1/bootstrap

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
# This is the script that the Lambda host calls to start the custom runtime.
3+
4+
/var/task/AWSServerless1
Binary file not shown.

Diff for: AWSServerless1/nuget.config

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<add key="local" value="./nuget-cache" />
5+
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
6+
</packageSources>
7+
</configuration>

Diff for: AWSServerless1/serverless.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"AspNetCoreFunction" : {
99
"Type" : "AWS::Serverless::Function",
1010
"Properties": {
11-
"Handler": "AWSServerless1::AWSServerless1.LambdaEntryPoint::FunctionHandlerAsync",
12-
"Runtime": "dotnetcore2.1",
11+
"Handler": "not_required_for_custom_runtime",
12+
"Runtime": "provided",
1313
"CodeUri": "",
1414
"MemorySize": 256,
1515
"Timeout": 30,

Diff for: AWSServerless1/web.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<configuration>
33
<system.webServer>
44
<handlers>
5-
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
5+
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV3" resourceType="Unspecified" />
66
</handlers>
77
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false">
88
<environmentVariables />

Diff for: buildspec.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
AWS_DEFAULT_REGION: ap-southeast-2
6+
7+
phases:
8+
install:
9+
runtime-versions:
10+
nodejs: 8
11+
commands:
12+
- rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
13+
- yum install dotnet-sdk-3.0 -y
14+
- dotnet tool install -g Amazon.Lambda.Tools
15+
build:
16+
commands:
17+
- cd ./AWSServerless1
18+
- dotnet lambda deploy-serverless

0 commit comments

Comments
 (0)