Introduction

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.yaml file
  • 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.

Quick Recap: Lambda Handlers and API Gateway

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.

Defining Your Application with `template.yaml`

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.

1. Start with the Template Header

Every SAM template starts with a header that tells AWS what kind of template it is:

  • AWSTemplateFormatVersion and Transform are required for SAM templates.
  • Description is optional but helpful.
2. Define the Lambda Function

Next, you define your Lambda function as a resource. Here’s how you do it:

  • Resources is where you list everything your app needs.
  • CalcFunction is the name of your Lambda function.
  • Type: AWS::Serverless::Function tells SAM this is a Lambda.
  • Properties describe the function:
    • CodeUri: . means the code is in the current directory.
    • Handler: main.handler points to the Python file (main.py) and the function (handler).
    • Runtime: python3.13 sets the Python version.
    • Timeout and MemorySize control how long and how much memory your function gets.
3. Connect API Gateway to Lambda

To make your function accessible over the web, you add an event trigger:

  • Events lets you define triggers.
  • Here, we set up an HTTP API that listens for POST requests at /calculate.
4. Add Outputs

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.

5. Complete Template Example

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.

Testing Locally with Sample Event Data

Before you deploy your function to AWS, it’s a good idea to test it locally. This helps you catch errors early.

1. Create a Sample Event

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.

2. Write the Lambda Handler

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.
  • tax and total are calculated.
  • The function returns a response with the results in JSON format.
3. Test the Function

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.

Deploying with SAM: Build, Deploy, and Verify

Once your function works locally, you are ready to deploy it to AWS.

1. Build the Application

First, you build your application so SAM can package it for deployment:

This command prepares your code and dependencies.

2. Deploy the Application

Next, you deploy your application to AWS:

  • --stack-name gives your deployment a name.
  • --capabilities CAPABILITY_IAM allows SAM to create roles.
  • --resolve-s3 lets SAM manage storage for deployment.
3. Find Your API Endpoint

After deployment, SAM will print the API URL. For example:

4. Test the Deployed API

You can test your API using curl:

Expected Output:

This confirms your function is live and working in the cloud.

Summary and What’s Next

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.yaml file
  • 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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal