Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘cloud’

Getting Started with AWS Lambda and C#

Posted by Viral Sarvaiya on December 26, 2017


I know its very late to write about AWS Lambda but i just learned AWS and writing vary basic things on AWS lambda.

What is Lambda?
First of all we need to know what is AWS lambda.
Lambda is compute service provided by Amazon web service that let you run your code without managing servers. if you like to create small bunch of code or simple function that works for you into AWS Lambda.
You can run code for virtually any type of application or backend service – all with zero administration.
All we need to do is supply your code in one of the languages that AWS lambda supports. Currently AWS Lambda supports Node.js, Java, Python and C#.

Prerequisites to Create New Lambda Function using Visual Studio and C#.

To create AWS lambda function we have following Prerequisites
– Visual Studio 2015 SP1 or Visual Studio 2017 and .Net core for windows installed. ( Dotnet core – https://www.microsoft.com/net/download/windows)
– Toolkit for Visual Studio. – https://aws.amazon.com/visualstudio/
– Then we need to specify our credentials into AWS Explorer of Visual Studio.
○ After installing AWS toolkit it enables option of AWS Explorer into Visual studio -> View menu as below image.

It will open AWS Explorer.

○ Click to new Account Profile and it will open below popup.

Add your AWS account’s Access key ID, Secret Access Key or you can import CSV file exported from AWS while creating user. Then click to OK at last.

Create New Lambda function using Visual Studio.

Now we are creating new project for AWS lambda function.
– File Menu -> New -> Project, it will open new project selection popup.

Select Installed pane from left side and choose AWS Lambda and then choose project type as “AWS Lambda Project (.Net Core).
– After you select project type choose Blueprint, here we will choose Empty function.

It will open default template for Lambda function, Listed all files as displayed into Solution explorer.

And Function.cs file will have as below code default.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Amazon.Lambda.Core;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace AWSLambdaTest
{
public class Function
{

/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
return "Hello from Lambda!" + " - " + DateTime.UtcNow;
}
}
}

We can modify as per our need, we can also modify Class Name, Function Handler name.

Here I am updating Class Name = LambdaTestFunction
And Function name = LamndaDemoHandler

Body of function I am keeping as it is.

Publish to Lambda

– In solution explorer, right click to Lambda project and choose “Publish to AWS Lambda

– It will open “Upload Lambda function” window.

– We need to confirm Account profile to use and its region where we like to upload our Lambda function.
– If you are creating new Lambda function then write name into textbox or select existing Lambda function from same dropdown.
– Configuration will be “Release”
– Framework = “netcoreapp1.0”
– Assembly Name will be namespace of our Lambda project. Here for us it will AWSLambdaTest
– Type Name will be namespace.class name, here for us AWSLambdaTest.LambdaTestFunction
– Method Name will be handler name, here for us LamndaDemoHandler
– If you like to save settings then we can check checkbox
– Click to Next for selecting Role and other configuration.

– Require Role name, you can choose existing role which has access policy to upload Lambda function. Or create new Role to upload lambda function.
– If your Lambda function accesses resources on Amazon VPC then select VPC subnets.
– Set environment variables if your lambda function require. Keys will be automatically encrypted by default service key.
– Choose “Upload

– It will upload Lambda function and automatic close when upload completes.

– After function uploads it open Function page, use left tabs to test your lambda function, add event source, and view logs.
– You can add subnet into Configuration tab.
– Event sources tab is used if other source you have used, like Dynamo DB or etc.
– Logs will display Logs for Lambda function.
– From example Request dropdown you can choose your requests, here we are selecting “hello World” and typing string because our lambda function accepts string only.
– After completing all configuration click to “invoke” and it will call our lambda function and display result into Response box and you can check log into log box.

Now your lambda function is created and tested and ready for use.

Thanks.

Ref – http://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/lambda-index.html

Posted in .Net, asp.net feature, AWS, AWS Lambda, C#, Lambda Function, netcore | Tagged: , , , , , , , , , , , , | Leave a Comment »