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.

The AWS Toolkit for Visual Studio is an extension for Microsoft Visual Studio running on Microsoft Windows that makes it easier for developers to develop, debug, and deploy.NET applications using Amazon Web Services. With the AWS Toolkit for Visual Studio, you'll be able to get started faster and be more productive when building AWS applications. 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.

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

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.

Visual

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.

Aws Lambda Visual Studio 2019

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.

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.

Aws Lambda Visual Studio 2017

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.

Aws lambda visual studio 2017

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

So mostly for entertainment sake and to see how easy it would be, I wondered if I could create a Python-based lambda in AWS using GitHub and Visual Studio Online. So, let’s see how this goes and how long it takes …

Aws Lambda On Visual Studio

Pre-Requisites

For this little experiment, you will need:

  • A GitHub account
  • An Azure account
  • An AWS account

Set Up the Development Environment

Let’s create an empty repository in GitHub as we need somewhere to keep code! Go to https://github.com/new and create a new repository, I called mine PythonLambda. I made it public and with a README (so not empty to start).

Next hop over to Visual Studio Online and click get started. You will then need to log in using your Azure account. Then click Create Environment. Enter in a name (I chose the same as the GitHub project) and then paste the URL for the GitHub repo into the Git repository. Click create and wait for it to be available.

Next, connect to the environment. A window that looks remarkably like Visual Studio Code will appear; I chose to install support for Python. This took virtually no time whatsoever. Next hit Ctrl-' to open the terminal windows. Let’s check for python by running python --version:

On to installing the AWS CLI. Simply run the command pip3 install awscli --upgrade --user. Once this completes you can run aws --version:

So now we have a working development environment. Took about 3 minutes to set this up. We have a git repository, a copy of Visual Studio Code set up to write python code and the AWS CLI.

Logging into GitHub and AWS

Next, we can edit the README and check we can push back to GitHub as a new branch. Change the README file and save it. You can then go to Source Control tab (press Ctrl-Shift-G), commit the files and push to GitHub. It will pop up a window asking you to authorise microsoft-vs to access your GitHub account. After that, it will push the code to GitHub.

Next head to the AWS IAM console and log in. Ideally, you will create a new user but for the sake of simplicity, I will just create a new access key. Go to the Users link, find your user, go to Security Credentials and select Create Access Key. Back within the terminal in Visual Studio Online run aws configure and copy the Access Key ID and Secret Access Key into the prompts.

This step took me about 5 minutes. So, we are ready to start building the lambda in about 10 minutes.

Make a Python Lambda and Publish

The goal of this post is not really the Lambda more just to get it all set up. The code below is a really trivial function. Create a new file called lambda.py and add the new content:

This is a very simple function which will log whatever is passed to it and return the servers current date.

The bare minimum we need to do for a lambda to run is to create an IAM role and policy, and then we can publish a function. Run the following within the terminal:

This will create the Role and Policy for the lambda to use. Finally, lets publish the first version of the lambda. In the terminal, run:

Finally, we will set up a build task to publish this to AWS. Create a new folder called .vscode and add a new file called tasks.json. Add the following content:

Now if you hit Ctrl-Shift-B within the editor, it will zip and deploy the python code as it stands to AWS.

Wrapping Up

Visual

Visual Studio Online is amazing. You can get a development environment up and running in minutes. It works anywhere and can easily be hooked into whatever you like. While this post has only created a very basic set up, it is hopefully enough to show you how you could do everything from the comfort of your own browser and in very little time.

This post was also published on my own personal blog - jdunkerley.co.uk

Read more

Thinking of joining us?

If you enjoyed this blog post and are interested in working with smart Developers on challenging software projects, check out our current vacancies.