Welcome back! In the previous lesson, you learned how AWS Lambda and API Gateway work together to create serverless APIs. Now, you are ready to take the next step: using the AWS Serverless Application Model (SAM) to define, test, and deploy your serverless applications.
AWS SAM is a framework that helps you manage serverless resources using simple configuration files. Instead of setting up each AWS service by hand, you can describe your entire application in a single file. This makes it easier to build, test, and deploy serverless APIs.
In this lesson, you will learn how to use AWS SAM to:
- Define your Lambda function and API endpoint in a
template.yamlfile - Test your function locally with sample data
- Deploy your application to AWS and verify it works
By the end of this lesson, you will be able to take a Python function and turn it into a working API using AWS SAM.
Before we dive in, let’s quickly remind ourselves how Lambda and API Gateway work together.
- Lambda Function: This is your code that runs in the cloud. It takes an
event(input), does some work, and returns a result. - API Gateway: This is the front door to your Lambda. It lets users send HTTP requests (like POST or GET) to your function.
In the last lesson, you saw a basic Lambda handler in Python:
The event parameter contains the data sent to your function (like a JSON payload from an API request). The context parameter has information about the runtime, but for now, you can focus on event.
With this in mind, let’s see how to use AWS SAM to manage and deploy this kind of function.
The heart of AWS SAM is the template.yaml file. This file describes your serverless application in a way that AWS understands.
Let’s build up a simple template.yaml step by step.
Every SAM template starts with a header that tells AWS what kind of template it is:
AWSTemplateFormatVersionandTransformare required for SAM templates.Descriptionis optional but helpful.
Next, you define your Lambda function as a resource. Here’s how you do it:
Resourcesis where you list everything your app needs.CalcFunctionis the name of your Lambda function.Type: AWS::Serverless::Functiontells SAM this is a Lambda.Propertiesdescribe the function:CodeUri: .means the code is in the current directory.Handler: main.handlerpoints to the Python file (main.py) and the function (handler).Runtime: python3.13sets the Python version.TimeoutandMemorySizecontrol how long and how much memory your function gets.
To make your function accessible over the web, you add an event trigger:
Eventslets you define triggers.- Here, we set up an HTTP API that listens for POST requests at
/calculate.
Finally, you can add outputs to make it easy to find your API endpoint after deployment:
This will print the API URL when you deploy.
Here's how the full template.yaml looks when you put it all together:
This template tells AWS to create a Lambda function, connect it to an API endpoint, and show you the URL after deployment.
Before you deploy your function to AWS, it’s a good idea to test it locally. This helps you catch errors early.
You can use a JSON file to simulate an API request. For example, create a file called event.json:
This file represents the data your API will receive.
Here’s a simple handler that reads the amount, calculates tax, and returns the result:
Let’s break this down:
event.get("body")gets the JSON body from the API request.json.loads(...)turns the JSON string into a Python dictionary.amount = float(body.get("amount", 0))reads the amount from the input.taxandtotalare calculated.- The function returns a response with the results in JSON format.
On your own machine, you can use the AWS SAM CLI to run the function locally:
This command runs your function with the sample event.
Expected Output:
This shows that your function is working as expected before you deploy it.
Once your function works locally, you are ready to deploy it to AWS.
First, you build your application so SAM can package it for deployment:
This command prepares your code and dependencies.
Next, you deploy your application to AWS:
--stack-namegives your deployment a name.--capabilities CAPABILITY_IAMallows SAM to create roles.--resolve-s3lets SAM manage storage for deployment.
After deployment, SAM will print the API URL. For example:
You can test your API using curl:
Expected Output:
This confirms your function is live and working in the cloud.
In this lesson, you learned how to use AWS SAM to define, test, and deploy a serverless API. You saw how to:
- Describe your Lambda function and API endpoint in a
template.yamlfile - Test your function locally with sample event data
- Deploy your application to AWS and verify it works
You are now ready to practice these steps yourself. In the next exercises, you will get hands-on experience defining, testing, and deploying your own serverless APIs using AWS SAM. Good luck, and have fun building!
