AWS SAM CLI: Local Testing and Debugging Support for Terraform Projects

Amazon Web Services (AWS) Serverless Application Model (SAM) CLI has made a significant update, and this time, Terraform users are in for a treat. AWS has just announced that the SAM CLI now includes support for local testing and debugging of infrastructure described with Terraform, bolstering development capabilities and efficiency. One can easily interpret infrastructure resource data from their Terraform projects, and execute Lambda functions and Amazon API Gateway endpoints locally within a Docker container.

Before we go any further, let’s take a closer look at what AWS SAM CLI and Terraform are.

What is AWS SAM CLI?

The AWS SAM CLI is a command-line interface that helps developers manage serverless applications built with the AWS Serverless Application Model (SAM). It offers a range of features like testing locally, packaging and deploying serverless applications, describing resources with code, and using AWS services seamlessly as per your application’s requirement.

What is Terraform?

Terraform is an Infrastructure as Code (IaC) tool that allows developers to manage and provision their infrastructure using high-level configuration syntax. The Infrastructure as Code paradigm is crucial in implementing modern, automated, and programmable infrastructure projects, and Terraform is one of the popular tools in this domain.

Until this update, the local testing and debugging features provided by the SAM CLI were available only for AWS CloudFormation templates. With the new functionality, developers working with Terraform would be able to test and debug their serverless applications locally—right in their development environment before pushing the code into the production environment.

This tutorial guide aims to walk you through this new feature and how it can be leveraged in your Terraform projects.


Setting Up AWS SAM CLI

Before you can begin debugging and testing in your local environment, you will need to set up AWS SAM CLI. Following are the basic steps:

  1. Install AWS SAM CLI: Depending on your Operating System, you can use different command-line methods to install AWS SAM CLI. Details on how to install it can be found in the official AWS SAM CLI User Guide.

  2. Configure your AWS credentials: Ensure you have the appropriate AWS credentials set up in your machine.

  3. Install Docker: Docker is essentially required for running steps in your local environment, much like the way it would in the AWS environment.


How to Use SAM CLI for Local Testing and Debugging on Terraform Projects

With the setup ready, you can now dive into testing and debugging your serverless applications defined in your Terraform projects.

1. Reading Infrastructure Resources

SAM CLI can interpret the infrastructure resource data from your Terraform project. This means you can start your Lambda function and API Gateway endpoints locally based on your existing Terraform setup.

To initiate your function or API endpoint, you can use the command sam local start-api for API-based applications, sam local start-lambda for Lambda functions, and sam local invoke to invoke the function.

2. Running Local Environments in a Docker Container

SAM CLI uses Docker to simulate an AWS Lambda-like executing environment. Using Docker, you can start your Lambda function and API Gateway endpoints locally. This would allow you to test your servers before deploying them to the production environment, reducing the risk of any potential issues.

3. Debugging

You can debug your local functions using various AWS toolkits on your IDE. This gives you the ability to step through the Lambda function code, inspect variables, stack traces, and access standard output logs.

• The sam local invoke command with the --debug flag can be used to initiate a debugging process for your Lambda functions.

• The sam local start-api or sam local start-lambda command coupled with the --debugger-path and --debug-args flags can be used when you want to debug with other AWS toolkits or debugging tools.

4. Generating Mock Test Events

The sam local generate-event command allows you to generate a set of predefined mock inputs (events) for various AWS services like S3, API Gateway, etc. You can redirect these mock events into a file and use it as an input to your testing Lambda functions.


Advantages of Using SAM CLI for Local Testing on Terraform Projects

  1. Time-Saving & Efficient: Local testing significantly reduces the time to test and debug code and boosts the speed of your development process.

  2. Reduced Costs: Testing in local environments doesn’t require any deployment to AWS Cloud, thus minimizing associated costs.

  3. Reliability: It allows you to simulate your Lambda function and API Gateway endpoints in your local machines just as they would perform in the production environment.

  4. Better Debugging: It provides the ability to step through your code and inspect variables, helping identify and resolve issues much quicker.


In conclusion, the inclusion of local testing and debugging support on Terraform projects by AWS SAM CLI is a welcome update. Developers can take advantage of this feature to speed up their development cycles, reduce costs and enhance their debugging procedures. The AWS Toolkit is also an essential resource for this feature, which helps in debugging throughout the Lambda function code. The CLI should continue to evolve and offer more support and features, making it an invaluable tool for local development.