AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You just upload your function, and it automatically runs in response to events.
Run code without servers — just write your function, upload it, and it runs automatically in response to events (like API calls, file uploads, DB changes).
Key Uses:
- API backends
- File/image processing
- Automation tasks
- Real-time data handling
You only pay when it runs — no need to manage servers.
It's Time to Create Your First Lambda Function
- In the AWS Management Console search bar, enter Lambda, and click the Lambda result under Services: You will see a page load that welcomes you to the AWS Lambda service:
- Click Create a function to start creating your first AWS Lambda function.
3.Select Author from scratch and enter the basic details
Name: Hello Lambda
Runtime: Node.js 22.x
Permissions: Click Change default execution role
Execution Role: Select Use an existing role
Existing role: Select the role beginning with Your lambda role
Note: Create role with Lambda Full access
- Click Create function.
- Scroll down to the Code source section and replace the contents of the index.mjs with the following code:
console.log("Loading function");
export const handler = async (event, context) => {
console.log(JSON.stringify(event, null, 2));
const message = JSON.parse(event.Records[0].Sns.Message);
if (message.cook_secs < message.req_secs) {
const messageText = message.pre
? `User ended ${message.pre} preset early`
: "Hello RCB.. Ee Sala Cup Namdu";
context.succeed(messageText);
}
context.succeed();
};
- Click Deploy at the top to save and deploy the Lambda function.
Let's test the Lambda Code:
The code you used in the previous lab step reads an incoming SNS message and parses the JSON body to be analyzed (line 4), then checks if the microwave was stopped before the requested number of seconds (line 5), and finally if a preset was used or not (lines 6-8)
- To begin creating a test event, click Test and Create new test event.
- In the form, enter the following values: Event name: Event JSON: Enter the following JSON into the event editor at the bottom of the form:
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:EXAMPLE",
"Sns": {
"Type": "Notification",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"TopicArn": "arn:aws:sns:EXAMPLE",
"Subject": "TestInvoke",
"Message": "{\"gid\":\"foo1234\",\"cook_secs\":90,\"req_secs\":150,\"cmds\":[2,3,0,\"start\"]}",
"Timestamp": "1970-01-01T00:00:00.000Z",
"SignatureVersion": "1",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"UnsubscribeUrl": "EXAMPLE",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
}
}
}
]
}
- Click on save To save the test.
- Click the test button to test the Lambda Function.
Boom..... You successfully Completed the creation and testing your first Lambda Function.
Follow me on LinkedIn:
https://www.linkedin.com/in/munisaiteja-narravula/
Learn the Serverless Concepts to reduce costs in Your Account.