Publishing and Running .NET Core function in AWS lambda

By | March 17, 2017

Hello, In my last article I have discussed about the serverless programming. and how aws lambda ca n help us to publish the .NET core (C#) code in cloud and run the function. In this article I will show you how can publish the function in the aws lambda and access it to perform some operation at client.

Steps to Publish Code in AWS Lambda.

There are two ways by which we can publish the code in AWS lambda. First by publising with the help of Visual Studio and AWS .NET SDK, or secondly directly uploading the files in AWS

Lets see the first method with the Help of visual studio.

Prerequsites for this are :

 

Getting Visual Studio Ready For AWS Development

Once you install the AWS SDK, you can see the AWS Explorer under the view as shown in the figure below.

AWS Explorer

AWS Explorer

 

Click on the AWS explorer and you will see a new panel in the visual studio as shown in the figure below. Click on the create profile button as shown in the figure below.

 

And a new pop up  window will appear on the screen. In this screen you have to enter the Access Key ID and Secret Access Key for the aws account which you should have created by now.

The steps to get both of them are mentioned here.

 

Once you enter the access key ID and secret access key, save the settings and you are ready to use the AWS service with your visual studio,

Creating a AWS Lambda Project in Visual Studio.

In visual studio go to File > New > Project. and under Installed templates you can see AWS Lambda as shown in the below figure. Select AWS Lambda Project option from the templates. I have named my new project as HelloLambda.

 

Once we click ok we will get the option to select from the various options for type of function we want to create in lambda.

I will create an “Empty function”. and Click Finish.

And we have our newly created Lambda project. In the project structure we have a file Name Function.cs  where we will be writing our first lambda.

In the Function.cs I have created a new method named HelloWorld as shown below.

Notice there are two parameters for the method. First is the string parameter named input which we will be passing while calling function. The second is the ILambdaContext type parameter which will be used by aws runtime to track and log the function steps.

Once we are done with the development of our function we can publish the code by right clicking on the project and Selecting “Publish to AWS Lambda..” as shown below.

Once you click Publish to AWS Lambda you will get a new window where you need to enter the lambda function settings as shown in the figure below.

Here we need the add the FunctionName which is the name of the function by which it will be stored in the AWS Lambda.

The Assembly name is the name of the project or assembly in which we have created our C# method.

Similarly Type name is the class name of the C# method.

And Method Name is the name of the C# method which will be called while we call the aws lambda.

All these settings are necessary for the AWS lamda to look out for the method which we will be calling while making a call to the aws lambda.

In the next screen we have to provide other settings like Role Name, Memory and Timeout as shown in the figure below.

aws lambda

Once you will enter all the settings click “Upload”. and our lambda function will be created in the AWS console as shown in the figure below.

Publishing Directly from the AWS Console

Apart from publishing directly from the visual studio we can publish the lambda function from aws console. In this section I will show how to achieve the same.

Go to the aws console as shown in the below figure. and navigate to the lambda service. Once you are on the lambda service page. You can create a lambda by clicking on the Create a Lambda Function.

 

In the next screen select “Blank Function” from the blueprint.

In the configure triggers page we don’t have to select any triggers. and just Click Next.

In the next screen we will get the Configure Function. Here we have to provide the Name of the function. And we have to upload the zipped assemblies from the “\bin\Release\netcoreapp1.0\publish” folder of the project. Here we have to take care of directly zipping the files and not inside a folder.

I have called the function as “HelloWorldFromConsole”. I have uploaded the zipped file.

In the same page we need to provide the handler for the function. This handler will help the amazon lambda to locate the function to be called.

The format of the handler should be “Assembly::namespace.class::method”.

In my case the handler is like “LambdaFunction::LambdaFunction.Fucntion::HelloWorld”.

Here on the same page you can provide the configuration key and value pairs which can be used by the lambda function. Once you are done you can click “Next” and “Create Function”. This way we are able to create a function from the AWS console.

Conclusion:

In this article I have shown you two ways to create a lambda function in aws. You can use this function to be called from other application which can be scaled up and down automatically by amazon.