AWS Builder Center
Your First AWS Lambda Function: A Python Walkthrough

Your First AWS Lambda Function: A Python Walkthrough

Create a small Python greeting function, send it a JSON input, inspect the result, and find its logs. Each step explains what is happening.

Give your first function one small job

This walkthrough creates a function that accepts a name and returns a greeting.
The task is deliberately small. It lets you learn how input reaches a Lambda handler, how the function returns output, and where its log messages go.
You need an AWS account with permission to create a Lambda function and its execution role. Check the pricing or credits applicable to your account; Lambda usage and CloudWatch Logs can incur charges.

What Lambda does in this example

AWS Lambda runs your code on managed infrastructure. For this walkthrough, you use a Lambda Event Function, the event-driven function type.
An invocation is one run of the function. A test event supplies the input for a run. The handler is the Python function that Lambda calls to process that input.
You will invoke the function directly from the console, so you do not need a website or public API endpoint.

Step 1: create the function

  1. Open AWS Lambda in the AWS Management Console.
  1. Open Event Functions and choose Create Event Function.
  1. Select Author from scratch.
  1. Name it  beginner-greeting .
  1. Select Python 3.14, or another supported Python runtime offered in your console.
  1. Keep the default architecture and let the console create a new execution role with basic Lambda permissions.
  1. Create the function.
The execution role provides the function's AWS permissions. The basic role used here supports writing logs; this example does not read S3 objects or access a database.

Step 2: add the Python code

On the Code tab, open  lambda_function.py  and replace the starter code with:
def lambda_handler(event, context):
name = event.get("name", "Builder")

if not isinstance(name, str) or not name.strip():
return {"error": "Provide name as a non-empty string."}

message = f"Hello, {name.strip()}! Your Lambda function works."
print("Greeting created successfully.")

return {"message": message}
Choose Deploy to make the updated code available for invocation.
The handler setting should be  lambda_function.lambda_handler : the file's name without  .py , followed by the function's name. Keeping the provided names avoids changing that setting.

Step 3: understand the code

The  event  parameter receives the invocation input. For a JSON object in this exercise, the Python runtime provides a dictionary.
 event.get("name", "Builder")  reads a name or uses  Builder  when the key is absent.
The condition checks that the name is a string containing some text. For invalid input, the function returns a simple error message. Otherwise, it builds a greeting and returns a dictionary containing that greeting.
 context  contains information about the invocation. This example accepts it but does not need to use it.
The  print  statement produces a log entry. Logs help explain what happened during execution; they are separate from the value returned to the caller.

Step 4: create the test input

Open the Test tab and create a test event named  greet-arjun . Use this JSON:
{
"name": "Arjun"
}
Choose Private for the event's sharing setting and save it. This is a small input document, not additional Python code.
Select Test to invoke the function, then expand the execution result details.
Expected returned value:
{
"message": "Hello, Arjun! Your Lambda function works."
}
The console also shows execution information around the returned value. The greeting above is your function's response, not the entire console display.

Step 5: change the input and predict the result

Try these inputs one at a time:
Input
Expected returned content
 {"name": "Sam"} 
A greeting addressed to Sam
 {} 
A greeting addressed to Builder
 {"name": " "} 
The non-empty-string error
 {"name": 42} 
The non-empty-string error
The code remains the same while the input changes. That is a useful way to see the relationship between a handler and its event.
The error cases return ordinary application data. They do not raise a Python exception, so Lambda can report a successful invocation even when the returned object contains the application's  error  field.

Step 6: find the log

After running a valid test, open Monitor and follow View CloudWatch logs. The default log group for this example is  /aws/lambda/beginner-greeting .
Open the relevant log stream and look for:
Greeting created successfully.
Logs can take a little time to appear. The execution role must also permit the required CloudWatch Logs operations.

Step 7: clean up

When you have finished experimenting, delete only the resources created for this exercise:
  1. Delete the  beginner-greeting  function from the Lambda console.
  1. Delete its exercise log group from CloudWatch Logs if you no longer need the records.
  1. Delete the new execution role only after confirming no other function or application uses it.
Deleting a function does not automatically remove its execution role and log group.
You have now supplied input, executed Python code, inspected a returned value, and found an execution log. Those are useful foundations for a larger event-driven application.
Discussion: What small task would you turn into your next Lambda function?

Further reading

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article