AWS Developer Tools Blog
Deploy an Existing ASP.NET Core Web API to AWS Lambda
In the previous post, we talked about the new ASP.NET Core Web API blueprint for AWS Lambda, and the Amazon.Lambda.AspNetCoreServer NuGet package that made it possible to run the ASP.NET Core Web API through Lambda. But what if you already have an existing ASP.NET Core Web API that you want to try as a serverless application? You can do this by following these steps:
- Add the Amazon.Lambda.AspNetCoreServer NuGet package.
- Add a Lambda function and bootstrap the ASP.NET Core framework.
- Add the Amazon.Lambda.Tools NuGet package to enable the toolkit’s deployment features.
- Add a serverless.template file to define Amazon API Gateway.
- Deploy the project.
Let’s take a deeper look at each step.
Setting Up the Lambda Function
The first step is to add the Amazon.Lambda.AspNetCoreServer NuGet package that bridges the communication between Amazon API Gateway and the ASP.NET Core framework.
After you add the package, add a new class named LambdaFunction and have it extend from Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction. You have to implement the abstract method Init to bootstrap the ASP.NET Core framework.
public class LambdaFunction : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
protected override void Init(IWebHostBuilder builder)
{
builder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup()
.UseApiGateway();
}
}
Enable Tool Support in the AWS Toolkit for Visual Studio
In order for the AWS Toolkit for Visual Studio to recognize the project as a Lambda project, you have to add the Amazon.Lambda.Tools NuGet package. This package isn’t used as part of the runtime of the function and is added as a build tool.
{
"dependencies": {
...
"Amazon.Lambda.AspNetCoreServer": "0.8.4-preview1",
"Amazon.Lambda.Tools": {
"type": "build",
"version": "1.1.0-preview1"
}
},
...
}
To also enable the integration with the .NET Core CLI, list the Amazon.Lambda.Tools NuGet package in the tools section in the project.json file.
{
...
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
"Amazon.Lambda.Tools": "1.1.0-preview1"
},
...
}
Configuring Amazon API Gateway
At this point, you could right-click the project and deploy it to Lambda, but it wouldn’t be fronted by API Gateway exposing the function as an HTTP REST API. The easiest way to do that is to add a serverless.template file to the project and deploy the project as an AWS Serverless project.
Add a serverless.template file to the project by right-clicking the project and choosing Add, AWS Serverless Template.
The default serverless.template file contains one function definition configured to be exposed by API Gateway using proxy integration, so all requests will go to that function. This is exactly what you need for an ASP.NET Core Web API project. The only thing that needs to be updated is the handler field. The format for the handler field is <assembly-name>::<namespace>.LambdaFunction::FunctionHandlerAsync. The FunctionHandlerAsync method is inherited from the base class of our LambdaFunction class.
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Transform" : "AWS::Serverless-2016-10-31",
"Description" : "Starting template for an AWS Serverless Application.",
"Parameters" : {
},
"Resources" : {
"DefaultFunction" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "ExistingWebAPI::ExistingWebAPI.LambdaFunction::FunctionHandlerAsync",
"Runtime": "dotnetcore1.0",
"CodeUri": "",
"Description": "Default function",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/{proxy+}",
"Method": "ANY"
}
}
}
}
}
},
"Outputs" : {
}
}
Deploy
Now you can deploy the ASP.NET Core Web API to either AWS Elastic Beanstalk or Lambda. The deployment process works in the same way that we’ve shown in previous blog posts about AWS Serverless projects.
And that’s all you have to do to deploy an existing ASP.NET Core Web API project to Lambda.
Visit our .NET Core Lambda GitHub repository to let us know what you think of running ASP.NET Core applications as an AWS Serverless functions and issues you might have. This will help us take the Amazon.Lambda.AspNetCoreServer NuGet package out of preview status.