
Developing an API using .NET Core 3.1 AWS Lambda function
In this post we will be learning about Developing, Logging and monitoring an API using AWS Services likes AWS Lambda, API Gateway, Cloudwatch through visual studio in .Net Core 3.1
AWS Lambda support for .NET Core 3.1
AWS now support .net Core 3.1 which brings many new runtime features to Lambda including C# 8.0 and F# 4.7 support, new JSON serializer, and a new ReadyToRun feature for ahead-of-time compilation. There are also new versions of the .NET Lambda tooling and libraries.
Creating a Lambda Project
To get started writing Lambda functions in Visual Studio, you first need to have these installed.
- Visual Studio
- AWS SDK for .net
- AWS Toolkit for Visual studio
- .net Core 3.1
Get started by using the Visual Studio New Project wizard. Under the Visual C# templates, there is a new category called AWS Lambda. After you install AWS Toolkit for Visual studio. You can choose between two types of project, AWS Lambda Project and AWS Serverless Application, and you also have an option to add a test project. To begin, choose AWS Lambda Project with Tests (.NET Core), name the project HelloWorld, and then choose OK.


Select Empty function to begin with scratch or can choose any blueprint to get started.

By Default it will create a simple function that takes a string as an input and converts it into upper case.

FunctionHandler is the method Lambda will call after it constructs the instance. By default you can see it takes string as an input with the ILambdaContext which is an interface for the lambda context.
An important field to understand is the function-handler. This indicates to Lambda the method to call in our code in response to our function being invoked. The format of this field is <assembly-name>::<full-type-name>::<method-name>. Be sure to include the namespace with the type name.
Another file that was created with the blueprint is aws-lambda-tools-defaults.json. This file contains default values that the blueprint has set to help prepopulate some of the fields in the deployment wizard. It is also helpful in setting command line options with our integration with the new .NET Core CLI.
To start with We can add our business logic with Function.cs and can add references, packages and class files in the project.
Logging into Cloudwatch
Lambda function comes with a CloudWatch Logs log group, with a log stream for each instance of your function. The runtime sends details about each invocation to the log stream, and relays logs and other output from your function’s code.
To output logs from your function code, you can use methods on the Console class, or any logging library that writes to stdout or stderr. The following example uses the LambdaLogger class from the Amazon.Lambda.Core library.
LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables()));


Debug the function
AWS .NET Core Mock Lambda Test Tool makes it easy to debug .NET Core Lambda functions. If you are using the AWS Toolkit for Visual Studio, the toolkit automatically installs or updates the test tool, and configures your launchSettings.json file. With the toolkit, you can use F5 debugging when the project opens.

Deploying the Function
To get started deploying the function, right-click the Lambda project and then choose
Publish to AWS Lambda. This starts the deployment wizard. Notice that many of the fields are already set. These values come from the aws-lambda-tools-defaults.json file described earlier. We do need to enter a function name. For this example, let’s name it HelloWorld, and then choose Next.


We can associate role and Add environment variable here then upload.

We can verify the lambda on AWS Management console.

Creating API on API Gateway.
Go to API Gateway then click on Create API AWS provide option to create HTTP , Websocket and Rest API. We will select rest.



Create Post method and associate it with lambda Services as integration type and Lambda Name HelloWorld


Go to Actions and deploy API


Test our API using Postman
Voila!! API is ready to consume by any application like Mobile , Web using Rest call. We can test our API using Postman

Nice content. It will help us to have understanding of AWS API development.
Simple and very helpful for beginners.
God Job👍
Very descriptive content to start developing APIs using aws.