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.

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 :

  • AWS sdk should be installed.

Getting Visual Studio Ready For AWS Development

Aws lambda visual studio 2019Aws lambda visual studio environment variables

Aws Lambda Visual Studio 2017

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.

In this guide, you’re going to learn how to get started with AWS Lambda and, more specifically, learn how to set up your first AWS Lambda Python! Stay tuned to learn how Lambda functions work and how to apply your newfound Lambda knowledge by using the Boto3 AWS Python SDK to create a Lambda function and start an EC2 instance.

Aws Lambda Template Visual Studio

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.

Deploy Your Lambda Microservice. Deploying your new microservice as a Lambda function is easy. The AWS Toolkit for Visual Studio will create the cloud components for you. To deploy your microservice, in the Solution Explorer, right click on your web service project, and select Publish to AWS Lambda. 20 hours ago  The AWS Toolkit for Visual Studio provides a number of quickstart blueprints for AWS Lambda projects. A new AWS Lambda project will be created. Note that the project includes a. One using the AWS Toolkit for Visual Studio and the second one is, using AWS Console. For the purpose of this demo, we will use AWS Toolkit. Right-click on the project in solution explorer and click on Publish to AWS Lambda. In this article, I have explained how to use the AWS Toolkit for Visual Studio Code to start developing code locally and then publish it to the AWS Lambda Functions once it is tested and verified locally.

The steps to get both of them are mentioned here.

Aws Lambda On Visual Studio

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.

Aws Lambda Visual Studio C#

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.

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.

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.

My Learning Resource

Excel your system design interview