AWS Lambda – Configuring Lambda Function


In the previous chapters, we have learnt how to create AWS Lambda function in AWS console. However, there are other parameters for creating a Lambda function. These include memory allocation, timeout etc.

In this chapter, let us understand in detail about the following configuration properties for AWS Lambda.

Memory Allocation

Login to AWS console and create or select the existing lambda function. Click the Configuration tab to get the details of the memory allocated. Look at the screenshot shown below −

Memory Allocation

Note that by default the memory allocated is 128MB. If you want to increase the memory you can click the slider.

The memory will get incremented to 64MB as you move the slider. Observe that the maximum memory available is 3008MB. Look at the screenshot shown below −

Maximum Memory

You can also use aws cli from command prompt to increase the memory limit. You will have to give the memory in increments of 64MB.

Now, let us increase the memory limit of AWS Lambda with name :myfirstlambdafunction.

The memory details of the function are shown in the screenshot given below −

Memory Details

The command used to change the memory using aws cli is as follows −

aws lambda update-function-configuration --function-name your function name --
region region where your function resides --memory-size memory amount --
profile admin user

The corresponding output of AWS Lambda function myfirstlambdafunction in AWS console is shown here. Observe that the memory is changed from 128MB to 256MB.

Memory Command

Maximum Execution Time

Timeout is the time allotted to AWS Lambda function to terminate if the timeout happens. AWS Lambda function will either run within the allocated time or terminate if it exceeds the timeout given. You need to evaluate the time required for the function to execute and accordingly select the time in Configuration tab in AWS console as shown below −

Maximum Execution Time

IAM Role

When creating AWS Lambda function, the role or the permission needs to be assigned. Incase you need AWS Lambda for S3 or dynamoDB, permission with regard to the services of lambda needs to be assigned. Based on the role assigned, AWS Lambda will decide the steps to be taken. For Example if you give full access of dynamodb, you can add, update and delete the rows from the dynamodb table.

Handler Name

This is the start of execution of the AWS Lambda function. Handler function has the details of the event triggered, context object and the callback which has to send back on success or error of AWS Lambda.

The format of the handler function in nodejs is shown here −

exports.handler = (event, context, callback) => {
   callback(null, "hello from lambda");
};

Lambda Function using Environment Variables

In this section, we will create a simple Lambda function using environment variables added in the configuration section. For this purpose, follow the steps given below and refer the respective screenshots −

Step 1

Go to AWS console and create a function in Lambda as shown.

Lambda Variables

Step 2

Now, add the environment variables as shown −

Lambda Environment

Step 3

Now, let us fetch the same in Lambda code as follows −

exports.handler = (event, context, callback) => {
   var hostName = process.env.host;   
   var userName = process.env.username;
   callback(null, "Environment Variables =>"+hostName+" and "+userName);
};

Step 4

To get the details from environment variables we need to use process.env as shown. Note that this syntax is for NodeJS runtime.

var hostName = process.env.host;   
var userName = process.env.username;

Step 5

The output for the Lambda function on execution will be as shown −

Lambda Function Execution
Advertisements