Hello, Lambda! Creating A Simple AWS Lambda Application in C#

aws Mar 13, 2019

Serverless apps are one of those modern technology paradigms that have been around for a while, but I've only had the chance to play with recently.

For a bit of background, I have to write an application for work that will take a list of IDs and process each one by calling an API. This application will only be run once, so I thought it could be a use case for a serverless.

Before I jumped to write code for my app, I wanted to get a taste of serverless by writing a simple application. And it was very easy with AWS Lambda and the AWS Toolkit for Visual Studio.

Prerequisites

  1. First, register for a free AWS account. The free tier which will allow you 1 million free requests per month on Lambda.
  2. Install the AWS Toolkit for Visual Studio. The Toolkit has project templates which will bootstrap our simple Lambda project.
  3. AWS Lambda runs C# with .NET Core, so of course you will need the .NET Core SDK.

How to Create a Simple Lambda Application in C#

Our Lambda app will be a simple "Hello World" app. The function will take a string input, and the output will be "Hello" followed by a space, and the string that was passed in.

Go to File > New Project and select AWS Lambda from the left sidebar. Then select AWS Lambda Project with Tests (.NET Core). Name your project "HelloLambda" and click OK.

Choose Empty Function in the Select Blueprint dialog.

You will now have the files necessary for your simple Lambda project.

Open Function.cs and edit the code in the FunctionHandler method to:


public string FunctionHandler(string input, ILambdaContext context) 
{ 
	return SayHello(input); 
}

Then below this method, write the SayHello method:


public string SayHello(string input)
{
	return $"Hello {input}!";
}

The project template that we used also generated a test project. This is useful to test if our Lambda function is working as expected. Open up FunctionTest.cs in the HelloLambda.Tests project and edit the code to the following:


[Fact]
public void TestSayHello()
{
	// Invoke the lambda function and confirm the greeting is correct.
	var function = new Function();
	var context = new TestLambdaContext();
	var greeting = function.FunctionHandler("World", context);

	Assert.Equal("Hello World!", greeting);
}

Run this test to ensure that our function is working correctly.

Deploying the AWS Lambda Application

Our function is working, but this is an AWS Lambda application so we need to deploy it to AWS for it to be useful. Fortunately, the AWS Toolkit for Visual Studio makes the whole process a breeze!

You will need to setup your AWS credentials if you haven't done so yet.

Right-click the Lambda project and choose Publish to AWS Lambda from the context menu.

This opens up a dialog box. Choose the account profile with your AWS account credentials and enter a meaningful function name.

Next, select an IAM role for the Lambda function. The Lambda function will run as this role, so if you need access to other resources, choose a role with the appropriate permissions. Since this is a simple function, I've opted to create a new role based on the AWS Lambda defaults (AWSLambdaRole). Click the Upload button to proceed.

Our function will now be uploaded to AWS servers.

Once the deployment is finished, it will open up the Lambda Function view where you can test the function (now on the cloud!) Enter a sample input, click Invoke, and verify that the function responds correctly.

The Log output shows the compute duration and the memory used by the Lambda function. This is useful to benchmark how much resource your function will use in execution. If you have an inefficient Lambda function, you may find yourself paying dearly!

That's it for writing a simple AWS Lambda function! Check out the AWS Lambda documentation for more information, and read up on the best practices in writing Lambda functions.

Let me know how you go in the comments!

unsplash-logoPablo Gentile

Kristina Alberto

Software engineer from Sydney. I work at Domain Group.