Build a Serverless Dotnet Core Web API with AWS Lambda and API Gateway

Sunil Kumar
.Net Programming
Published in
5 min readDec 1, 2020

Introduction

In this article, we are going to deploy the ASP.NET Core Web API in AWS Lambda and AWS API Gateway.

AWS Lambda

AWS Lambda lets you run code without managing servers. you pay only for the compute time you consume.

With Lambda, you can run code for virtually any type of application or backend service — all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability.

You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

API Gateway

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. APIs act as the “front door” for applications to access data, business logic, or functionality from your backend services.

Using API Gateway, you can create RESTful APIs and WebSocket APIs that enable real-time two-way communication applications. API Gateway supports containerized and serverless workloads, as well as web applications.

AWS Architecture:

Below is the AWS services we are going to use for the deployment of Web API.

Pre-requisites

To complete this learning path, you will need:
✓ An AWS Account
✓ An IAM user with access key credentials
✓ Visual Studio Code or Visual Studio 2019+ for Windows

If you don’t have an account visit https://aws.amazon.com and click Sign Up.
You must have a set of valid AWS credentials, consisting of an access key and a secret key, which are used to sign programmatic requests to AWS. You can obtain a set of account credentials when you create your account, although we recommend you do not use these credentials and instead create an IAM user and use those credentials.

Installing the AWS CLI:

Install the `AWS CLI` for Windows, Mac, or Linux: https://aws.amazon.com/cli/

Once installed, you can configure the CLI by running the `aws configure` command in a terminal or command-line window.

When prompted, enter your `AWS Access Key ID` and press Enter.

Enter your `AWS Secret Access Key` when prompted and then press Enter.

For the `default region name` you should enter your chosen region code (e.g. eu-west-1)

Finally, for the `default output` format you can just press Enter.

Installing the AWS Lambda Tools:

`dotnet tool install -g Amazon.Lambda.Tools`
`dotnet tool install — global Amazon.Lambda.TestTool-3.1`

Read the below articles for creating the Postgres RDS instance and managing the connection string using Systems Manager

Creating the AWS Lambda Web Api Project using the command line

`dotnet new — help` will display the installed AWS project templates

To create a serverless project run the below command
`dotnet new serverless.AspNetCoreWebAPI -n CleanArchitectureApp`

Adding Lambda Support to existing ASP.NET Core Web API Project

Below are the steps to add AWS Lambda support for existing projects

Step 1: In csproj file add the below tag
`<AWSProjectType>Lambda</AWSProjectType>`

Include the below AWS package references to the projects


<PackageReference Include=”AWSSDK.Extensions.NETCore.Setup” Version=”3.3.101" />
<PackageReference Include=”Amazon.Lambda.AspNetCoreServer” Version=”5.2.0" />

Step 2: Create a c# file with name “LambdaEntryPoint.cs” and inherit the appropriate class based on your requirement. Below are some details
- API Gateway REST API -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
- API Gateway HTTP API payload version 1.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
- API Gateway HTTP API payload version 2.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction
- Application Load Balancer -> Amazon.Lambda.AspNetCoreServer.ApplicationLoadBalancerFunction

Call the Startup.cs in the override method.


protected override void Init(IWebHostBuilder builder)
{
builder
.UseStartup<Startup>();
}

Step 3: Create a json file “aws-lambda-tools-defaults.json” to read the default lambda configuration.

Step 4: create a serverless declarative template “serverless.template” and this will be used by the AWS Cloud Formation for creating the required resources during the Lambda deployment.

Specify the dotnet core runtime, lambda handler, memory and policies

“Resources”: {
“AspNetCoreFunction”: {
“Type”: “AWS::Serverless::Function”,
“Properties”: {
“Handler”: “CleanArchitectureApp.WebApi::CleanArchitectureApp.WebApi.LambdaEntryPoint::FunctionHandlerAsync”,
“Runtime”: “dotnetcore3.1”,
“CodeUri”: “”,
“MemorySize”: 256,
“Timeout”: 30,
“Role”: null,
“Policies”: [
“AWSLambdaFullAccess”, “AmazonSSMFullAccess”,”AWSLambdaVPCAccessExecutionRole”
],

Below is the full implementation

Deploying the WebApi to AWS Lambda

Open the command line on Windows and change the repository working directory to:

`cd D:\GitHub_Projects\CleanArchitectureApp\CleanArchitectureApp.WebApi`

Enter the below command and hit enter
`dotnet lambda deploy-serverless — template serverless.template`

Cloud Formation Stack Creation In Progress

Lambda Function Deployed

Lambda function permissions for connecting to RDS instance

API Gateway Resource Creation

Hit the API gateway endpoint
https://xxxx.execute-api.ap-south-1.amazonaws.com/Prod/swagger

Cloud Watch Logs:

Summary

You can implement the Warmer function for faster ColdStart(startup) of Lambda Function.

Happy Coding!

--

--

.Net Programming
.Net Programming

Published in .Net Programming

Explore the .Net with the latest in C# from basic to advanced, including .Net versions 9, 8, 6, 5, Core 3.1, .Net Framework, ASP.NET Core, MVC, design patterns, OOPS, and SOLID principles. Get top tutorials, best practices, and hands-on code examples on GitHub.

Sunil Kumar
Sunil Kumar

Written by Sunil Kumar

Architect and Full Stack Developer in Dotnet Core, C#, WebApi’s, Integrations and Learner on Angular and React FrontEnd