diff --git a/AWSServerless1/.gitignore b/AWSServerless1/.gitignore new file mode 100644 index 0000000..6837a8f --- /dev/null +++ b/AWSServerless1/.gitignore @@ -0,0 +1 @@ +!/nuget-cache/Amazon.Lambda.AspNetCoreServer.4.0.0-preview2.nupkg \ No newline at end of file diff --git a/AWSServerless1/AWSServerless1.csproj b/AWSServerless1/AWSServerless1.csproj index 162eff4..5e74aa0 100644 --- a/AWSServerless1/AWSServerless1.csproj +++ b/AWSServerless1/AWSServerless1.csproj @@ -1,12 +1,17 @@  - - netcoreapp2.1 - true - Lambda - - - - - - + + netcoreapp3.0 + true + Lambda + + + + Always + + + + + + + \ No newline at end of file diff --git a/AWSServerless1/LocalEntryPoint.cs b/AWSServerless1/LocalEntryPoint.cs deleted file mode 100644 index a881025..0000000 --- a/AWSServerless1/LocalEntryPoint.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; - -namespace AWSServerless1 -{ - /// - /// The Main function can be used to run the ASP.NET Core application locally using the Kestrel webserver. - /// - public class LocalEntryPoint - { - public static void Main(string[] args) - { - BuildWebHost(args).Run(); - } - - public static IWebHost BuildWebHost(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup() - .Build(); - } -} diff --git a/AWSServerless1/Program.cs b/AWSServerless1/Program.cs new file mode 100644 index 0000000..1298890 --- /dev/null +++ b/AWSServerless1/Program.cs @@ -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 +{ + /// + /// 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 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>)(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(); + }); + } +} diff --git a/AWSServerless1/Startup.cs b/AWSServerless1/Startup.cs index 15578ed..fb9d70a 100644 --- a/AWSServerless1/Startup.cs +++ b/AWSServerless1/Startup.cs @@ -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 { @@ -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()) { @@ -33,8 +33,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) app.UseHsts(); } + app.UseRouting(); app.UseHttpsRedirection(); - app.UseMvc(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + }); } } } diff --git a/AWSServerless1/aws-lambda-tools-defaults.json b/AWSServerless1/aws-lambda-tools-defaults.json index e68c1c7..b17724a 100644 --- a/AWSServerless1/aws-lambda-tools-defaults.json +++ b/AWSServerless1/aws-lambda-tools-defaults.json @@ -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" } \ 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..35099c2 100644 --- a/AWSServerless1/web.config +++ b/AWSServerless1/web.config @@ -2,7 +2,7 @@ - + diff --git a/buildspec.yml b/buildspec.yml new file mode 100644 index 0000000..8fa2400 --- /dev/null +++ b/buildspec.yml @@ -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 \ No newline at end of file