AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. Lambda functions are pieces of code that perform specific tasks, such as processing data streams, responding to HTTP requests, or integrating with other AWS services.
With Lambda, you can execute code in response to triggers such as changes in data, user actions, or predefined schedules, allowing you to build scalable and responsive applications without the overhead of server management.
Lambda supports multiple programming languages, including Node.js, Python, Java, and Go, making it versatile for different development needs.
In this article
With AWS Lambda, users can upload code and Lambda handles the execution. Here’s a step-by-step breakdown of how it works:
AWS Lambda can be triggered by various AWS services and events. Some common triggers include:
Related content: Read our guide to lambda architecture
Here’s a step-by-step guide to creating and using AWS Lambda functions. These instructions are adapted from the official AWS documentation.
To create a Lambda function using the console, follow these steps:
2. Select the Author from scratch option.
3. For Basic information, enter a function name such as exampleLambdaFunction.
4. Select Python 3.12 or Node.js 20.x as the runtime.
5. Keep the default architecture setting of x86_64 and select Create function.
6. This creates a basic function that returns the message “Hello from Lambda!” and grants basic write permissions to CloudWatch Logs.
7. To modify the function code, go to the Code tab.
8. In the console’s built-in code editor, replace the default code with the following Node.js code:
export consthandler = async (event, context) => {
const number1 = event.number1;
const number2 = event.number2;
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw new Error("Both number1 and number2 should be numbers");
}
const sum = number1 + number2;
console.log(`The sum of ${number1} and ${number2} is ${sum}`);
console.log('CloudWatch log group: ', context.logGroupName);
let response = {
"sum": sum,
};
return {
statusCode: 200,
body: JSON.stringify(response),
};
};
This code takes a JSON object with two numbers, computes their sum, and returns it as a JSON string.
9. Select Deploy to update the function’s code.
10. Wait for the console to display a banner confirming the successful update.
To test your newly created Lambda function using the console:
2. Choose Create new event.
3. Enter an event name such as exampleTestEvent.
4. Replace the default values under Event JSON with the following:
{
"number1": 224,
"number2": 612
}
5. Select Save and then Test again to invoke the function.
6. The console will display the response and function logs in the Execution results tab. You should see something similar to:
Test Event Name
exampleTestEvent
Response
{
"statusCode": 200,
"body": "{\"sum\":836}"
}
Function Logs
START RequestId: 163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8 Version: $LATEST
2024-06-30T07:09:48.273Z 163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8 INFO The sum of 224 and 612 is 836
2024-06-30T07:09:48.279Z 163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8 INFO CloudWatch log group: /aws/lambda/exampleLambdaFunction
END RequestId: 163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8
REPORT RequestId: 163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8 Duration: 24.65 ms Billed Duration: 25 ms Memory Size: 128 MB Max Memory Used: 66 MB Init Duration: 141.15 ms
Request ID
163d1bf1-8ab7-4b12-8c7a-1c129ffc78e8
7. To view invocation records, open the CloudWatch console and navigate to the Log groups page.
8. Choose the relevant log group for the function (/aws/lambda/exampleLambdaFunction).
9. In the Log streams tab, select the log stream for the function’s invocation to view detailed logs.
Lumigo is a serverless monitoring platform that lets developers effortlessly find Lambda cold starts, understand their impact, and fix them.
Lumigo can help you:
Get a free account with Lumigo resolve Lambda issues in seconds