Skip to main content
In this article, we walk you through an advanced example of using the AWS SAM CLI to build, deploy, and test a serverless application. We will guide you from initializing a new SAM project to deploying a fully functional API integrated with DynamoDB, using multiple Lambda functions.

Project Initialization

To begin, open your terminal and execute the following command to initialize your SAM project:
During initialization, the CLI prompts a series of questions to configure your project. AWS offers several Quick Start templates, such as the simple Hello World example, data processing workflows, serverless APIs, DynamoDB integrations, and machine learning samples. For this demo, choose the simple “Hello World” template.
The image shows a Visual Studio Code interface with a terminal open, displaying a list of AWS Quick Start application templates to choose from. The templates include options like "Hello World Example," "Data processing," and "Machine Learning."
Next, select your desired runtime. Even though Python is typically the default, this example uses Node.js (e.g., Node.js 20). Choose the zip package type and plain JavaScript.
The image shows a Visual Studio Code interface with a terminal open, displaying options for selecting runtimes, package types, and starter templates for a project setup.
The CLI then asks if you want to enable additional features such as X-Ray tracing or CloudWatch monitoring. For simplicity, answer “no” for these options and provide a project name, for example, “SAM API.” After confirmation, SAM creates the project folder structure along with essential files. You might see an output similar to:

Project Structure and Configuration

The generated project includes a template.yaml file that defines your SAM configuration. Key sections include a project description, global function settings (like the default function timeout), and resource definitions. Below is an excerpt of the template showcasing the Hello World function:
The API event for the Hello World function is defined as shown below:
Other files generated include a README, a .gitignore, sample tests, and directories for Lambda function code and test events. For instance, here is a Node.js Lambda function snippet:
A sample event JSON file for local testing looks like:
Similarly, an event simulating an API Gateway POST request is provided below:
Once your application setup is complete, deploy it using:

Evolving the Example: Building an API for an E-Commerce Application

In this section, we extend the project to create an API for an e-commerce (or inventory) application. The API will allow users to add new products and retrieve a list of existing products from a DynamoDB table. Start by cleaning up any unnecessary files (like tests, .npmignore, and package.json) and reorganize your project structure so that all Lambda functions reside within a dedicated source folder. Next, add several Lambda functions to the project:
  • Create Products – Adds a new product to the DynamoDB table.
  • Get All Products – Retrieves the complete list of products.
  • Get Product by ID – Retrieves a specific product by its name (used as the primary key).

Create Products Lambda Function

Below is the Lambda function code snippet for creating products:
The corresponding SAM resource definition in template.yaml is provided below:
The policy template (DynamoDBCrudPolicy) automatically grants the necessary CRUD permissions on the specified DynamoDB table to the Lambda function.

Deploying the API

After configuring your Lambda function and API event, deploy the stack using the guided deployment command:
Follow the interactive prompts to configure deployment parameters such as stack name, region, and IAM role confirmations. After deployment, CloudFormation creates the DynamoDB table, API Gateway, Lambda functions, and assigns the required permissions.
The image shows an AWS CloudFormation dashboard displaying details of a stack named "sam-api," including its status as "CREATE_COMPLETE" and various metadata like stack ID and creation time.
Verify the deployed resources in your AWS Console:
  • DynamoDB: Check the products table.
  • Lambda: Confirm the createProduct Lambda function.
  • API Gateway: Ensure that the API properly triggers your Lambda functions.
The image shows an AWS Lambda console page displaying details of a function named "aws-sam-HelloWorld-EAYDQV69Ok4P," including options to add triggers and destinations, and a sidebar with a tutorial on creating a simple web app.
The image shows an AWS API Gateway interface with a resource named "/products" and a POST method. The right panel displays resource details and indicates no methods are defined.
The image shows the AWS API Gateway console, specifically the "Stages" section for an API named "sam-api," displaying details for the "Prod" stage.

Testing the API

Use tools like Postman or curl to test your API endpoints. For instance, to create a new product, send a POST request with a JSON payload containing the required “name” field along with additional properties such as price. Example POST payload for creating a phone:
After adding multiple products (e.g., phone, TV, book), inspect the DynamoDB table to verify the entries. For the Get All Products endpoint, configure a Lambda function in your template as follows:
Similarly, set up the Get Product by ID endpoint (using the product name as a path parameter) with the following configuration:
Deploy your updated template with:
Test the endpoints by sending:
  • A GET request to /products to retrieve all products.
  • A GET request to /products/{name} (for example, /products/book) to retrieve a single product.
Example response for /products:
Example response for /products/book:

Outputting API Information

To simplify testing and troubleshooting, add an Outputs section to your template.yaml to display useful details such as the Lambda ARN and the API URL:
After running sam deploy, you will see output similar to:

Cleaning Up

Once testing is complete and you no longer need the deployed resources, clean up by deleting the CloudFormation stack:
This command removes the stack along with all associated resources.
This concludes our SAM advanced demo. In this article, we covered how to initialize a SAM project using a Quick Start template, build and configure multiple Lambda functions integrated with DynamoDB and API Gateway, and output essential deployment information for testing. Happy coding!

Watch Video

Practice Lab