Serverless API using AWS Lambda and CSharp

Develop and deploy Serverless API using AWS Lambada, Visual Studio and C#.

Serverless Applications

In today’s world of cloud computing where applications are becoming less and less dependent on physical platform or computer to run, serverless applications are becoming more and more popular because they are literally serverless. It means these applications are not more dependent on operating systems or hardware. Serverless applications are stateless and easily scalable enabling developer to focus on functionality and features of the application without worrying about resources and scalability.

Serverless applications are popular choice for the following scenarios

  • A service to be created to perform a very specific task upon request
  • A service to be created to perform a very specific task or job upon specific events
  • Data Processing or file processing tasks which does not require any interactions
  • Tasks which are trivial such as sending email

AWS Lambda and API Gateway

AWS Lambda is one of the most popular options available currently to develop and deploy server less application.

In integration with AWS API Gateway it is possible to create an API without hosting it on a physical servers such as IIS or Apache.

With the latest release AWS lambda now supports development with latest version of .NET which is .NET core and c#.

What’s in this article

I this article I am going to show how easy it is who create and deploy .NET serveless application fronted by API using Visual Studio.

With AWS toolkit it is now possible to create a Lambda function fronted by AWS API Gateway with a few clicks directly from the Visual Studio 2015 and later.

Before we start, following are the items which needs to be in place for the steps I explain to work smoothly.

  • A valid account in AWS with a valid user created in it.
  • Visual Studio 2015 with Update 3
  • Installed AWS toolkit for Visual Studio 2013 (Download)
  • Installed Microsoft .NET core tools (Preview 2) (Download)

Let’s dive into it

First step is to create a Lambda Serverless project in Visual Studio

Follow below mentioned steps.

  • Open Visual Studio with “Administrator” access.
  • Click File -> New -> Project…
  • Select “Aws Lambda” from the project template available in left pane.
  • Select “Aws Serverless Application (.NET Core)” from the middle pane.
  • Input appropriate Project Name, Solution Name and Location and click “OK”.

NewProject Dialog

  • Select “Empty Serverless Application” in “New AWS Serverless Application” dialog and click “Finish”

Serverless Application Template

  • A new project will be create with following structure in the Solution Explorer.

Project Structure

Following files are important to notice for this article.

  • Function.cs
    • The code in this file is like a controller code but it is actually a Lambda function. It’s the entry and exit point of the API.
    • By default it comes with one method “Get”. This method is a handler and it has two parameters APIGatewayProxyRequest and ILambdaContext.
    • Instance of APIGatewayProxyRequest is similar to WebRequest and it has all the properties which are needed to process a request such as body, headers, querystring parameters etc. Lambda function uses them to perform the action it is intended to perform.
    • Second parameter is provides useful information about the AWS lambda at run time. That is also handy for logging using Logger property of it.
    • The default “Get” method doesn’t do anything fancy. It just returns response with “Hello AWS Serverless”.
  • serverless.template
    • This file contains configuration information in the form of JSON.
    • The information in this is the settings applicable to each endpoint of the API such as which handler of lambda function class should be executed, with what AWS role and Policy, Memory size for Lambda, Timeout, URL template, http method for the API etc.

Let’s not make any changes in the default code and settings for now and deploy it to AWS Lambda.

Right click on the project “AWSServeless1” in solution explorer and click “Publish to AWS Lambda…” from the context menu.

It should pop-up “Publish AWS Serverless Application” window.

Publish Dialog

Click on square button for the field “Account profile to use:”, if you haven’t already configured an account profile already.

Configure Account Profile

It will open profile configuration window where you need to provide profile name, valid Access Key ID and Secret Access Key combination and click OK.

Configure Account Profile

If already configured it should display the configured profiles in the dropdown, select appropriate profile from it.

Select appropriate region from the “Region” dropdown list.

Leave “Configurations” and “Framework” values as it is.

Enter appropriate value for Stack Name. Stack name is to identify the API stack.

Create an S3 bucket, if you do not have any S3 bucket, by clicking on “New” button.

Provide a valid bucket name in “Create bucket” dialog and click OK.

This S3 bucket is the place where the deployment package will be uploaded during deployment.

Click “Publish”.

It should display publishing progress as following.

Publish In-Progress

Once the publishing is completed the dialog will be closed and it will switch to visual studio which will show the further deployment progress.

Once deployment is completed successfully it should show result as following.

Deployment Completed

Notice the value set to “AWS Serverless URL”. You can try to call the API directly from browser using that URL.

And if you do that you should be able to see the response from the API as following in the browser.

API result in browser

So that’s it. The vanilla version of Serverless API using AWS Lambda, AWS Api Gateway and C#.