Create your First AWS Lambda Function


Lambda is a pure serverless offering from AWS. AWS is one of the many Cloud Compute services offered by the provider. At the basic level, you need to mention the runtime and deploy the function. Each function runtime comes with a boilerplate code to start with. You can test the lambda function by configuring the test event object based on your needs. You pay for what you use, and at all other times, your code remains inactive.
Create a Lambda
Go to the resource search bar and type lambda. Select the service named Lambda.
On the functions page, click on the Create function button.
Give the function a name and select the runtime. Do not change the architecture unless you are sure you need the arm64 architecture. Now, click on the Create function button in the bottom-right corner.
Understanding Event Object
Lambda was specially made to react to different events. So, a lambda function is always triggered by another event. These events can be generated by other client applications or AWS services. Different triggers create different event objects. So, understanding the event is crucial for writing the Lambda function code. We can print the event object to understand what information we need.
import json
def lambda_handler(event, context):
print(event)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
You need to deploy the code after changes are made to it.
Once the code is successfully deployed, you will receive a notification.
Now, go to the Test section. Here you will invoke the lambda function with different test events.
Let’s assume that the Lambda function will receive an HTTP request and return a dummy message. You can use the below custom object as a test event.
{
"version": "2.0",
"routeKey": "$default",
"rawPath": "/",
"rawQueryString": "",
"headers": {
"content-length": "31",
"x-amzn-tls-version": "TLSv1.3",
"x-forwarded-proto": "https",
"postman-token": "73c9c0bf-c453-41e4-84ae-2271c44d151b",
"x-forwarded-port": "443",
"x-forwarded-for": "2405:201:802e:2019:141e:885a:537f:5acd",
"accept": "*/*",
"x-amzn-tls-cipher-suite": "TLS_AES_128_GCM_SHA256",
"x-amzn-trace-id": "Root=1-68135b1f-4710793e52d43b1427175efd",
"host": "y3lsrtnhle4zmtlrum7ngb6sou0cebwr.lambda-url.us-east-1.on.aws",
"content-type": "application/json",
"accept-encoding": "gzip, deflate, br",
"user-agent": "PostmanRuntime/7.43.3"
},
"requestContext": {
"accountId": "anonymous",
"apiId": "y3lsrtnhle4zmtlrum7ngb6sou0cebwr",
"domainName": "y3lsrtnhle4zmtlrum7ngb6sou0cebwr.lambda-url.us-east-1.on.aws",
"domainPrefix": "y3lsrtnhle4zmtlrum7ngb6sou0cebwr",
"http": {
"method": "POST",
"path": "/",
"protocol": "HTTP/1.1",
"sourceIp": "2405:201:802e:2019:141e:885a:537f:5acd",
"userAgent": "PostmanRuntime/7.43.3"
},
"requestId": "574efcee-be84-4e8a-b1de-9652c534715c",
"routeKey": "$default",
"stage": "$default",
"time": "01/May/2025:11:29:35 +0000",
"timeEpoch": 1746098975629
},
"body": "{\r\n \"name\": \"Ritwik Math\"\r\n}",
"isBase64Encoded": false
}
Save the test event first.
Now, click on test.
Open the Motinor page. Click on the View CloudWatch logs button. It will open all the log streams for your Lambda.
Open the log stream that matches the time of the event. You will see the same object printed.
Sometimes, AWS does not create different log streams for events fired in close proximity. Go through the log streams that are close to the time of event you are looking for. Or you can use Log Insigts to search for data you want.
Code Changes
Based on our Event object, we can make some changes in the code. For example, if the method is GET, we code will reply Get request received. For POST code will send the body as the message. Any other method is unsupported.
import json
def lambda_handler(event, context):
body = "Unsuported method"
status_code = 405
if event["requestContext"]["http"]["method"] == "POST":
status_code = 201
body = json.loads(event["body"])
if event["requestContext"]["http"]["method"] == "GET":
status_code = 200
body = "Get request received"
return {
'statusCode': status_code,
'body': json.dumps(body)
}
You can use the above code as a reference or write your own logic. Then deploy the code.
Go to the Test page again. Select the pre-saved test event.
Test with POST, GET, and PUT methods by updating the method key value within requestContext→http.
Click on the Test button and check the response from Lambda.
Function URL
Now, your code is ready to be used by others. Go to the lambda configuration.
Select Function URL and click on the Create function URL button.
Select Auth Type as None. Click on Save.
In the Function overview section, the Function URL will be visible.
Test Lambda with Postman
Copy the URL and open Postman. Test lambda against GET, POST, and PUT methods. Check both the status code and response body.
GET Method
POST Method
PUT Method
Subscribe to my newsletter
Read articles from Ritwik Math directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ritwik Math
Ritwik Math
As a seasoned senior web developer with a wealth of experience in Python, Javascript, web development, MySQL, MongoDB, and React, I am passionate about crafting exceptional digital experiences that delight users and drive business success.