AWS
Imal Perera  

Creating Your First Lambda Function

Spread the love

In previous post I explained what is AWS Lambda and how you can trigger it and many more. so this post we are going to learn how to create a lambda function. 

First go to compute section in the aws console and select Lambda,  if this is the first time you come to this page you will see welcome page ( similar to welcome page ) and there will be a blue button saying “Get Started now” or else you will straight away go the page which listed all the existing lambda functions, and then you wanna click “Create Lambda function”

Then select the runtime ( I selected nodeJs 4.3 )  and then I filter templates with “blank” and used that template.  checkout the image below

Next window you will be asked to configure a trigger, but lets keep it as it is and first learn how to create the function. so i’m hitting next button, and in the window i’ll give the name and the description 

Write The Lambda Function

Lets explore node template and understand what it do. before we write our code

This is how the Lambda function signature looks like, events is the object we use to pass the data to the function and callback is the way we get the result back or the function which use to understand what happened inside.  we can use console.logs within the code and those will can be viewed later in the cloud watch logs.

So lets write our first lambda function which takes two parameters and added together.

"use strict"
console.log("Function Ready");
exports.handler = (event, context, callback) => {
    let first = event.first;
    let second = event.second;
    var value = first + second;
    callback(null, value);
};

Ok.. we are almost done, now scroll down, give a “Existing role” like “lambda_basic_execution” this will be enough for now but if you look into advanced you will see you can allocate memory for your function and many more.

That is it click on “next”, then review it and click on “Create function”

Test the function

Now it is time to test the the lambda function we wrote, click on test button in the top and you will get a popup asking to add the input data, ( see the below image) fill it and run the test. 

Congratulation you have created your first Lambda function and it is working… next lets see how we can create a scheduled task

Next : How to trigger a Lambda function

Leave A Comment