In this Part #2 of blog I will be creating a new .NET Core function in local machine and will upload to AWS using console interface. There are different ways you can develop and deploy but I will be talking in this blog only about the CLI way.
I am assuming you know how to create a Lambda function in AWS console already and if not, please read the Part #1 first.
Open the Command prompt and follow below steps:
Step 1: Install Lambda Templates for .NET Core
dotnet new -i Amazon.Lambda.Templates
Step 2: Create an empty function
dotnet new lambda.EmptyFunction –name MyTestLambdaFun
You should be able to see a folder structure like this is created.
under src\projectname you can see a Function.cs file which is our starting point.
Code will look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace MyTestLambdaFun
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
}
}
Step 3 – Modify the code to return HTTP 200 status code.
It is necessary for lambda functions to return 200 status code, or else the function will work in AWS console when you “Test”, but the API Gateway endpoint will not.
After adding the StatusCode property, the function will look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.Lambda.APIGatewayEvents;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace MyTestLambdaFun
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public APIGatewayProxyResponse FunctionHandler(ILambdaContext context)
{
return new APIGatewayProxyResponse
{
Body = "Hello Lambda, from Praveen",
StatusCode = 200
};
}
}
}
Do:
dotnet build
Now, if you are getting an error on APIGatewayProxyResponse, then you will have to install the dependency. Below command will help you:
dotnet add package Amazon.Lambda.APIGatewayEvents
(Refer to nuget page)
Try again dotnet build, and I am expecting to have the build succeeded.
Step 4 – Package the function
If you have not installed Lambda tools, then install it first:
dotnet tool install -g Amazon.Lambda.Tools
Then use the command:
dotnet lambda package
Notice the last line, you will get path to a zip file which you have to upload to AWS Lambda.
Step 5 – Upload the package to AWS console
Login to AWS Console and navigate to your lambda function. In this example I am going to use the same function we created in Part #1.
Scroll up and you should see a “Function Code” section like this:
Upload your ZIP file by clicking the upload button.
Next, you have to do one important task. Go to the source code folder again and open a file named “aws-lambda-tools-defaults.json”. You will have to copy the function-handler property value to the “Handler” textbox in “Function code” section.
Then click “Save” button.
Step 6 – Test the function
Click “Test”. You should see the test result like this: (Our function is not expecting an input parameter so you can create a dummy test event)
As a bonus, let us see how we can create a HTTP endpoint to this function.
Step 7 – Create API Gateway
Click “Trigger” button:
In the next screen, make these selections and click “Add”:
You will be redirected back to the dashboard. Scroll up and you will see a new section “API Gateway” with a new API endpoint URL. Use the link in browser to test.
Happy Lambda development!
You have noticed that I was rushing to finish the blog by not explaining in detail each step. I am leaving the rest to you as home work intentionally.
Refer to official .NET Core CLI documentation here.