diff --git a/AWSServerless1/AWSServerless1.csproj b/AWSServerless1/AWSServerless1.csproj index 162eff4..931a48b 100644 --- a/AWSServerless1/AWSServerless1.csproj +++ b/AWSServerless1/AWSServerless1.csproj @@ -1,12 +1,18 @@  - netcoreapp2.1 + netcoreapp2.2 true Lambda - + + Always + + + + + \ No newline at end of file diff --git a/AWSServerless1/LocalEntryPoint.cs b/AWSServerless1/LocalEntryPoint.cs index a881025..98667cd 100644 --- a/AWSServerless1/LocalEntryPoint.cs +++ b/AWSServerless1/LocalEntryPoint.cs @@ -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 { /// /// 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. /// public class LocalEntryPoint { - public static void Main(string[] args) + private static readonly LambdaEntryPoint LambdaEntryPoint = new LambdaEntryPoint(); + private static readonly Func> 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) => diff --git a/AWSServerless1/Startup.cs b/AWSServerless1/Startup.cs index 15578ed..7fd6072 100644 --- a/AWSServerless1/Startup.cs +++ b/AWSServerless1/Startup.cs @@ -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 diff --git a/AWSServerless1/aws-lambda-tools-defaults.json b/AWSServerless1/aws-lambda-tools-defaults.json index e68c1c7..50cd4a2 100644 --- a/AWSServerless1/aws-lambda-tools-defaults.json +++ b/AWSServerless1/aws-lambda-tools-defaults.json @@ -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" } \ No newline at end of file diff --git a/AWSServerless1/bootstrap b/AWSServerless1/bootstrap new file mode 100644 index 0000000..1f18f12 --- /dev/null +++ b/AWSServerless1/bootstrap @@ -0,0 +1,4 @@ +#!/bin/sh +# This is the script that the Lambda host calls to start the custom runtime. + +/var/task/AWSServerless1 \ No newline at end of file diff --git a/AWSServerless1/serverless.template b/AWSServerless1/serverless.template index 30bd0b0..a9fdfe7 100644 --- a/AWSServerless1/serverless.template +++ b/AWSServerless1/serverless.template @@ -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, diff --git a/AWSServerless1/web.config b/AWSServerless1/web.config index bcc15ec..2157aef 100644 --- a/AWSServerless1/web.config +++ b/AWSServerless1/web.config @@ -2,7 +2,7 @@ - +