DEV Community

Cover image for First Lambda Function
Narravula Muni Sai Teja
Narravula Muni Sai Teja

Posted on

First Lambda Function

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

  1. 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:

Image description

  1. Click Create a function to start creating your first AWS Lambda function.

Image description

3.Select Author from scratch and enter the basic details

Image description

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

  1. Click Create function.
  2. 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();
};
Enter fullscreen mode Exit fullscreen mode
  1. Click Deploy at the top to save and deploy the Lambda function.

Image description

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)

  1. To begin creating a test event, click Test and Create new test event.
  2. 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"
          }
        }
      }
    }
  ]
}

Enter fullscreen mode Exit fullscreen mode
  1. Click on save To save the test.

Image description

  1. Click the test button to test the Lambda Function.

Image description

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.

Top comments (4)

Collapse
 
nevodavid profile image
Nevo David

Insane how easy this actually is once you break it down step by step - never thought running code with zero server setup could be this smooth.

Collapse
 
narravulamunisaiteja070 profile image
Narravula Muni Sai Teja

That's The Advantage we have with Serverless computing in AWS

Collapse
 
garre_sandeep_b1cde294045 profile image
Garre Sandeep

Good Content, thanks for sharing

Collapse
 
vamseekrishna_mahankali_5 profile image
Vamseekrishna Mahankali

Nice info regarding the person who don't know Lambda service this blog is very useful