DEV Community

Cover image for Steps to Transform ASP.NET Core API into AWS Lambda Functions
Naimul Karim
Naimul Karim

Posted on

Steps to Transform ASP.NET Core API into AWS Lambda Functions

The beauty of hosting an ASP.NET Core API behind a Lambda function is that you can write an ASP.NET Core API using the skills you already have and AWS's logic will provide a bridge to run each controller same as existing API Controllers.

The below steps will guide you to transform a ASP.Net CoreAPI to a Serverless Application.

*1. Install the AWS Toolkit extension for Visual Studio *

  1. Create the new project, selecting the template ‘AWS Serverless Application (.NET Core)

The below tag will be added to in csproj fileLambda

And the AWS nuget package reference will also be added.Amazon.Lambda.AspNetCoreServer(5.3.0)

3. Configuring Amazon API Gateway

serverless.template: An AWS CloudFormation Serverless Application Model template file for declaring Serverless functions and other AWS resources. It contains one function definition configured to be exposed by API Gateway using proxy integration, so all requests will go to that function. The only thing that needs to be updated is the handler field. The format for the handler field is

::.LambdaFunction::FunctionHandlerAsync

The FunctionHandlerAsync method is inherited from the base class of our LambdaFunction class.

*The Lambda Entry Point *

Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction

The code in this file bootstraps the ASP.NET Core hosting framework. The Lambda function is defined in the base class. When the app is run locally, it started with the LocalEntryPoint class. When this is run within the Lambda service in the cloud, this LambdaEntryPoint is what will be used.

Creating the API and service

  1. Copy the codes from existing API and service classes.

Run Locally

  1. Now build and run the project from IIS and see if it works

  1. Check the controller Post and Get responses from postman.

All good locally.

Deploying the WeApi to AWS Lambda

  1. Create an AWS Profile from AWS Explorer in View

  1. Right click your project and select “Publish To AWS Lambda”.

  2. Put a Stack Name and create a S3 Bucket.

Publishing to AWS is in progress.

S3 Bucket is created.

Lambda Function is deployed.

From Cloud Watch Monitor is showing the statistics.

  1. Get the AWS Serverless URL from Visual Studio .

  1. Check the API from postman.

The API is running and returning responses.

You are done !

Top comments (0)