Aws Lambda On Visual Studio

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.

Open your Visual Studio and follow the steps to create new project. Click on File - New - Project. Now, the following screen is displayed wherein you select AWS Lambda for Visual C#. Select AWS Lambda Project (.NET Core). You can change the name if required, will keep here the default name. Click OK to continue. Use Visual Studio to build with the convenience of IntelliSense and deploy to AWS with AWS CloudFormation. Learn more » Project Templates The AWS Toolkit for Visual Studio includes project templates for AWS Lambda functions, AWS CloudFormation templates and sample projects showing how to use various AWS services.

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

Aws Lambda On Visual Studio
  • With Lambda and AWS API Gateway we are able to run a typical ASP.NET Web API application by replacing the web server as so: AWS Serverless Web API. The AWS Toolkit for Visual Studio provides a project template for Web API on Lambda. The whole Web API project can be deployed into S3. Details of this can be found here.
  • To create a Lambda project. Open Visual Studio, and on the File menu, choose New, Project. Do one of the following: For Visual Studio 2017, in the New Project dialog box, expand Installed, expand Visual C#,select AWS Lambda, choose the AWS Lambda Project (.NET Core - C#) template, and then choose OK.
Aws Lambda On Visual Studio

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

Prerequsites for this are :

  • AWS sdk should be installed.

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.

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.

Aws lambda visual studio code

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.

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 “binReleasenetcoreapp1.0publish” 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.

Aws Lambda On Visual Studio

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”.

Aws Lambda Visual Studio Code

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.

Aws Lambda Visual Studio Environment Variables

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.

Aws Lambda Template Visual Studio

My Learning Resource

Excel your system design interview