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 3.0 with custom runtime #4

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
1 change: 1 addition & 0 deletions AWSServerless1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!/nuget-cache/Amazon.Lambda.AspNetCoreServer.4.0.0-preview2.nupkg
25 changes: 15 additions & 10 deletions AWSServerless1/AWSServerless1.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.4" />
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="3.0.3" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
<ItemGroup>
<Content Include="bootstrap">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer" Version="4.0.0" />
<PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.0.0" />
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.3.100.1" />
</ItemGroup>
</Project>
21 changes: 0 additions & 21 deletions AWSServerless1/LocalEntryPoint.cs

This file was deleted.

41 changes: 41 additions & 0 deletions AWSServerless1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.Serialization.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.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 Program
{
public static async Task Main(string[] args)
{
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_NAME")))
{
CreateHostBuilder(args).Build().Run();
}
else
{
var lambdaEntry = new LambdaEntryPoint();
var functionHandler = (Func<APIGatewayProxyRequest, ILambdaContext, Task<APIGatewayProxyResponse>>)(lambdaEntry.FunctionHandlerAsync);
using var handlerWrapper = HandlerWrapper.GetHandlerWrapper(functionHandler, new JsonSerializer());
using var bootstrap = new LambdaBootstrap(handlerWrapper);
await bootstrap.RunAsync();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
19 changes: 12 additions & 7 deletions AWSServerless1/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace AWSServerless1
{
Expand All @@ -13,16 +13,16 @@ public Startup(IConfiguration configuration)
Configuration = configuration;
}

public static IConfiguration Configuration { get; private set; }
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container
// 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.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
Expand All @@ -33,8 +33,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseHsts();
}

app.UseRouting();
app.UseHttpsRedirection();
app.UseMvc();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
27 changes: 14 additions & 13 deletions AWSServerless1/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"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.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "ap-southeast-2",
"configuration": "Release",
"framework": "netcoreapp2.1",
"template": "serverless.template",
"s3-bucket": "aws-serverless-md",
"stack-name": "AWSServerless1"
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"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.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "ap-southeast-2",
"configuration": "Release",
"framework": "netcoreapp3.0",
"template": "serverless.template",
"s3-bucket": "aws-serverless-md",
"stack-name": "AWSServerless1",
"msbuild-parameters": "--self-contained true /p:PublishReadyToRun=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="AspNetCoreModuleV3" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="false">
<environmentVariables />
Expand Down
18 changes: 18 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 0.2

env:
variables:
AWS_DEFAULT_REGION: ap-southeast-2

phases:
install:
runtime-versions:
nodejs: 8
commands:
- rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
- yum install dotnet-sdk-3.0 -y
- dotnet tool install -g Amazon.Lambda.Tools
build:
commands:
- cd ./AWSServerless1
- dotnet lambda deploy-serverless