Practice in your account · 20 min
Your first AWS Lambda function
In this lab you will create a Lambda function in Python, test it with an event, find its log in CloudWatch Logs and compare what you manage in Lambda with what you manage on an EC2 instance.
In this lab you will write an AWS Lambda function of a few lines, run it with a test event and look in CloudWatch Logs for what it printed. A function is a piece of code that does a single task, and Lambda is the AWS service that runs it for you without you keeping a computer on. To launch an EC2 instance you choose an operating system, an instance type and a network before running anything; in Lambda, by contrast, you only write the code.

Lambda runs a function when an event arrives, which is the notice that something happened. For example, an online store can have a function that makes a small version of each product photo: when someone uploads a photo to an S3 bucket, that event runs the function, and when it finishes, the function shuts down until the next photo. In this lab you will write the event yourself in the console, but the mechanism is the same: Lambda receives a JSON document, runs your function and shuts down.
Before you start
Do the zero spend budget lab first, so the email warns you if something starts charging. In the full account, also check the Region, because the function only shows up in the Region where you create it. For example, if you create the function in Ohio and then open the Lambda console with another Region selected, the list of functions looks empty. In the new experience, the function ends up in us-east-2.
cloud path tip: if you do this lab from the Explore AWS widget, building a web app with Lambda is one of the activities that give you extra credit. That activity uses a function URL, just like the
[lab05-lambda-url.yaml](/roadmaps/plantillas/lab05-lambda-url.yaml)template.
Steps in the console
In the console you will create the function, change its code, test it and then look for its log, which is the text the function writes while it runs. That log tells you what the function did when nobody was watching. For example, if the store's function fails with a photo, the log records which photo it was and what happened. Follow these steps:
- Open the Functions page of the Lambda console (https://console.aws.amazon.com/lambda/).
- Choose Create function and Author from scratch.
- In Basic information, enter
cloudpath-lab05-holaas the function name. - In Runtime, choose Python 3.13, the same version the template uses. The runtime is the language you write the function in, in this case Python. Leave the architecture set to x86_64, which is the type of processor the function runs on.
- Let Lambda create an execution role with basic permissions and choose Create function. A role is a set of permissions you give to a service, and this one is what allows the function to write its logs to CloudWatch Logs. For example, without that role the function would run, but it couldn't record what it did.
- On the Code tab, open
lambda_function.pyand replace all its content with this:
```python import json
def lambda_handler(event, context): nombre = event.get("nombre", "mundo") region = context.invoked_function_arn.split(":")[3] print(f"Saludo para {nombre} desde {region}") return {"mensaje": f"Hola, {nombre}", "region": region} ```
In this code, event is the JSON document the function receives and context carries data about the execution, such as the function's ARN, which is where the Region comes from. The function looks for a nombre in the event (if there isn't one, it uses "mundo"), writes a greeting to the log with print and responds with a message containing the name and the Region.
7. In the DEPLOY section, choose Deploy. Deploying saves your new code in Lambda; if you skip this step, the test runs the previous code.
8. In TEST EVENTS, choose Create new test event. Name: prueba. In Event JSON enter:
json
{ "nombre": "Ana" }
and choose Save.
9. Choose the run icon next to your event. On the OUTPUT tab you have to see Status: Succeeded, the response with your message and the log with the line "Saludo para Ana".
10. Open the CloudWatch console, the AWS service that stores logs, go to Log groups and choose /aws/lambda/cloudpath-lab05-hola. Choose the most recent log stream, which is where you will find what your function printed.
In a new experience account the steps are the same, although the execution role is created by the IAM role manager from an AWS template. That manager is turned on and can't be turned off until you activate the advanced features. Write down the name of the role that appears in Configuration, Permissions, because you will delete it at the end.
Lambda compared with EC2
A Lambda function and an EC2 instance differ in what you choose, in when they charge and in who manages the server, which is the computer where your code runs:
| An EC2 instance | This Lambda function | |
|---|---|---|
| What you chose | AMI, instance type, network, security group | Language and code |
| When it charges | Every second while running, whether you use it or not | Only when it runs (requests and duration) |
| Who patches the operating system | You | AWS |
| How you turn it off | You stop or terminate the instance | There is nothing to turn off |
These differences are the shared responsibility model applied to compute, which is how the work is split between what AWS takes care of and what you take care of. With EC2, the instance's operating system is your job, so you patch it; with Lambda, the server where the function runs is AWS's job, and you only take care of the code.
For example, if the store makes the small photos with an EC2 instance, it pays for every hour the instance is on even if nobody uploads photos, and it has to install the operating system updates. If it does the same with Lambda, it only pays for the times the function runs, and AWS takes care of updating the computer it runs on.
EC2 or Lambda?
0 of 6Tap the column of the service each characteristic belongs to.
Charges every second while running, whether you use it or not
Charges only when it runs, by requests and duration
You patch the operating system
There is nothing to turn off
You choose the AMI, instance type, network and security group
You only choose the language and write the code
CloudFormation option
CloudFormation is the AWS service that creates resources from a text file called a template. The lab05-lambda-url.yaml template creates a Python 3.13 function that responds with a JSON document containing the Region. Along with the function it creates its role, with the path /cloudpath-lab/ and only the basic execution policy, and its log group, with a retention of one day, which means the logs delete themselves after that day.
The template also creates a public function URL, with no authentication, with the two permissions Lambda has required for that since October 2025. A function URL is the function's own internet address, and when you open it in the browser the function runs and answers you. For example, if the store wanted a page that says "hello" to whoever opens it, that URL would be enough, with no server at all. To use the template, follow these steps:
- In the CloudFormation console, choose Create stack and With new resources (standard).
- Choose Upload a template file, upload
lab05-lambda-url.yamland choose Next. - Stack name:
cloudpath-lab05-lambda. Choose Next. - Check I acknowledge that AWS CloudFormation might create IAM resources, because the template creates the role. Choose Next and then Submit.
- In Outputs, open
Url. It has to respond{"lab": "lab05", "region": "us-east-2"}(or your Region).
Since the URL is public, anyone who has it can invoke the function, that is, make it run, and each invocation counts toward your free tier, so delete the stack when you finish. Also, according to the data in cfn-lint, the CloudFormation template checker, function URLs don't exist in Mexico (mx-central-1).
How to know you are done
In the console, the test has to say Succeeded, and the log group has to have your line. You can also invoke the function from CloudShell, a window inside the console where you type commands. For example, this command sends the function the name "CloudShell" instead of "Ana" and saves the response in a file:
aws lambda invoke \
--function-name cloudpath-lab05-hola \
--cli-binary-format raw-in-base64-out \
--payload '{"nombre": "CloudShell"}' \
respuesta.json && cat respuesta.json
The command has to print "StatusCode": 200, which means Lambda received the call and ran the function, and then {"mensaje": "Hola, CloudShell", "region": "..."}. The --cli-binary-format raw-in-base64-out option makes version 2 of the CLI send the JSON as is.
If you used the template, check its outputs and call the URL with these commands:
aws cloudformation describe-stacks --stack-name cloudpath-lab05-lambda \
--query "Stacks[0].Outputs" --output table
curl -s "<la Url de la salida>"
Delete what you created
A function that doesn't run doesn't charge, but its logs take up space in CloudWatch Logs and its role stays in IAM. For example, if you delete only the function, the log group and the role stay in your account even though they are no longer good for anything. That is why you have to delete all three, each one in its console:
- In Functions, select
cloudpath-lab05-hola, choose Actions and Delete, typeconfirmand choose Delete. - In CloudWatch, Log groups, select
/aws/lambda/cloudpath-lab05-hola, choose Actions and Delete log group(s), and confirm. - In IAM, Roles, select the function's execution role (if Lambda created it, it starts with
cloudpath-lab05-hola-role-), choose Delete, type its name and confirm.
If you used the template, delete the stack cloudpath-lab05-lambda, and the function, the URL, the role and the log group go with it.
True or false?
0 of 4A function that doesn't run charges you for every hour it exists.
If you delete the function from the console, its log group is deleted on its own.
If you created the function in the console, you delete the execution role yourself in IAM.
Deleting the template's stack takes the function, the URL, the role and the log group with it.
Next
- Create your first Lambda function: https://docs.aws.amazon.com/lambda/latest/dg/getting-started.html
- cloud path practice exams: /examenes?lang=en