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.
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 −
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 −
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 −
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.
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 −
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.
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"); };
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 −
Go to AWS console and create a function in Lambda as shown.
Now, add the environment variables as shown −
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); };
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;
The output for the Lambda function on execution will be as shown −