Shruti Tirpude
3 min readMar 17, 2024

--

Simplifying Local Testing of Serverless Applications with SAM CLI and Terraform

While working on AWS serverless applications using terraform as Infrastructure as code tool, we probably are all too familiar with the exasperation of trying to run your AWS serverless application locally for testing. Countless times, you’ve made changes, deployed it to AWS, and crossed your fingers, only to discover issues that could have been caught earlier with proper local testing. But with the powerful combination of AWS SAM CLI and HashiCorp Terraform, running serverless application locally is no longer a challenge. Link to the aws documentation: https://aws.amazon.com/blogs/compute/better-together-aws-sam-cli-and-hashicorp-terraform/

Let’s break it down into simple steps for running a nodeJs application in a lambda function locally and testing it :

Step 1: Setting the Stage

Install the following :

AWS CLI, SAM CLI, Terraform, Docker, Node

Step 2: Crafting Your Lambda Functions

Write the simple code for the lambda in handler file named function.js

exports.handler = async (event) => {
console.log("Event: ", event);
let responseMessage = "Hello, World!";

const response = {
statusCode: 200,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
message: responseMessage,
}),
};

return response;
};

Step 3: Terraforming Your Infrastructure

With your Lambda functions ready to go, it’s time to define your infrastructure using Terraform. This might seem daunting at first, but Terraform’s declarative syntax makes it a breeze to define AWS resources like Lambda functions, IAM roles, and API Gateway endpoints. The following is an example of a lambda function named hello_lambda.

resource "aws_lambda_function" "hello_lambda" {
function_name = "hello"

s3_bucket = aws_s3_bucket.lambda_bucket.id
s3_key = aws_s3_object.lambda_hello.key

runtime = "nodejs16.x"
handler = "function.handler"

source_code_hash = data.archive_file.lambda_hello.output_base64sha256

role = aws_iam_role.hello_lambda_exec.arn
}

Run the following command to initialize terraform

terraform init

Step 4: SAM CLI to the Rescue

With SAM CLI, we can test our serverless applications locally before deploying it to AWS. No more crossing your fingers and hoping for the best. With SAM CLI, we can invoke the Lambda functions locally, simulate API Gateway endpoints, and test app’s behavior in an environment that closely resembles AWS. The following command uses docker container to emulate a lambda function and build it.

DOCKER_HOST=unix://$HOME/.docker/run/docker.sock sam build — hook-name terraform — beta-features

Step 5: Invoke Lambda function locally using SAM Cli:

DOCKER_HOST=unix://$HOME/.docker/run/docker.sock sam local invoke aws_lambda_function.hello_lambda — beta-features

In the above screenshot, we get a statusCode 200 with the response message invoking the lambda function locally.

By harnessing the power of SAM CLI and Terraform, we can finally say goodbye to the frustration of not being able to run your AWS serverless application locally. With proper local testing and automation, you can catch bugs early, iterate faster, and deliver high-quality software. So go forth, code fearlessly, and remember: you’ve got this!

--

--

Shruti Tirpude

Software Engineer with an enthusiasm for AWS technologies like serverless.I enjoy solving real world complex problems through technology.