Develop and Deploy C#/.Net Core applications for AWS LAMDA

Photo by James Harrison on Unsplash

In this section we will talk about develop and deploying .Net core applications to AWS Lamda

TOOLS

  1. Visual Studio 2019
  2. AWS Toolkit for Visual Studio

AWS providing very good tool kit for visual studio 2019 & 2015

New Project

There are two default packages added automatically .

Run & Debug:

By Default we can run Mock Lamda Test tool to trigger local code and can debug with different options provided by Test Tool

We can select different request types based on your request type

For Example:

API Gateway AWS proxy or Gateway Authorizer

AWS API Gateway:

As per official documentation

“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.”

If you are using API gateway for your APIs then there will be change in your function handler arguments.

AWS providing C# defined objects to read inputs triggered by API Gateway or S3.

Example:

APIGateway Request

API Gateway Response

apiProxyEvent will contains query string parameters,headers and body content passed via API gateway.

apigProxyEvent.Headers

apigProxyEvent.HttpMethod

apigProxyEvent.Body

Access Input Parameters

Once your done with integrating your lamda function with API Gateway,need to select integration type under Methods

Response:

Sample Response:

return new APIGatewayProxyResponse

{

StatusCode = (int)HttpStatusCode.OK,

Body = JsonConvert.SerializeObject(new { msg = “RESPONSEOBJECT”}),

Headers = new Dictionary<string, string> {

{ “Content-Type”, “application/json” },

{ “Access-Control-Allow-Headers”, “Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token” },

{ “Access-Control-Allow-Origin”, “*” },

{ “Access-Control-Allow-Methods”, “DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT’” }

}

};

Dependency Injection

We can configure startup.cs to implement dependency injection for lamda .net core application.

Packages Used

Deploy to AWS

If you are working .Net core applications or Azure functions you may familiar with deploy to Azure.

it will be similar experience with AWS lamda deployment with different settings view.

Select Project →Right Click

Aws Toolkit provide option to publish directly from Visual Studio 2019

Step 1: Please select valid AWS profile and Region

you can download and copy your credentials to following path

C:\Users\USerName\.aws

Step 2: Select Appropriate Region

Step 3:Select Aws Lamda function for deployment

Step 4: Select right Function under Type dropdown & Next

After initial settings we can configure Memory and Timeout limits directly in this wizard

We can also add variables for AWS Lamda functions

Tips

We can also set required parameters at gateway as mandatory and it will throw error in case of not passed as part of request.

Need to enable CORS to support cross origin requests

Configure Stage variables based on staging configuration and can be used for database settings/external API links based on environment

Help links

https://dzone.com/articles/under-the-hood-of-net-core-lambda-request

Just Developer….