Blog - Trusted Team Extension Partner For Europe & USA

AWS Lambda integration testing with Python, Localstack, and Terraform

Written by Mahabubur Rahaman Melon | 15/04/2025

To start with unit testing for AWS Lambda integration testing with Python, Localstack, and Terraform we can just choose a testing framework and start writing our beautiful unit tests. For testing AWS services we also can use some mock tools like moto. But what about integration testing? A solution may be to deploy with the Continuous Deployment tool and run some test code against real AWS services. But there are some problems:

  • It takes more time to deploy every time

  • Running test code against AWS takes more time

  • Increases the AWS bills

So one good solution could be using a local infrastructure that resembles the same as AWS real infrastructure. So here comes our friend Localstack to solve the problem. And today we will take a look at how we can use Localstack for integration testing of AWS Lambda.
 

Prerequisite

  • Python for lambda and test code

  • Terraform, an IaC tool to deploy lambda at the Localstack

  • Localstack to use AWS infrastructure locally

  • Docker, latest Localstack version needs docker to run.

Install python version greater than on equal 3.8, Terraform version 1.10.2 and latest docker.
 

The Skeleton

Install terraform version 1.8.2 from here on your local machine.

1. Create project directory

 

 2. Create directory for lambda codes 

 

 3. Add handler.py file in lambda directory 

 

 4. Create directory for terraform 

 

 5. Add requirements file in project root directory 

 

6. Create directory for test code

 

 7. Create python file for test code 

 

 8. So the skeleton will look like this 

 

Add requirements

Let’s add some python requirements in requirements.txt file

 

 Make a python virtual environment named “venv” 

 

Activate virtual environment and install necessary packages from requirements.txt file 

 

Add Lambda Code

We will add a very simple code. Our lambda will just get the webpage https://example.com and return the page source as text.

 

Add Files to deploy Lambda

Let’s add a terraform variable for the lambda function name. So from our test code, we will provide the lambda function name and then test it to make the testing more dynamic. Add a file vars.tf in terraform dir and add following codes.

 

 Now add terraform code for lambda at `terraform/lambda.tf`  

 

Now we are going to add terraform/localstack.tf to which will tell terraform to use Localstack for deployment 

 

Deploy to Localstack with Terraform

Now time to test deployment at LocalStack

  1. Run command to start localstack in one terminal window

 

2. On another terminal run commands to test deployment. But this is only for testing and we are going to automate it in next steps. 

 

Automate Test Code

Now time to automate the lambda testing. Our strategy:

  • Deploy terraform to LocalStack from test code e.g python code
  • Execute lambda using boto3
  • Check if we get the webpage as text
  • Destroy infrastructure with terraform to LocalStack using test code
  1. Add terraform helper function at tests/terraform_helper.py which will responsible for creating infrastructure at test start and destroy after the test

 

Let’s add test code and we are going to use python’s unit test module to test our code 

 

 Finally, the project will look like this 

 

Time to test the Test

Now come to the moment of playing 🎻

As we implemented the tests with the unit test module, we can run the test using

 

 After a lot of logs we can see final output like this 

 

⚠️ WARNING

Localstack is growing very faster. So if it works today may break tomorrow if the required packages are not fixed. So please try to fix all the packages version in Pipfile. Sometimes it may require some python packages to start various services like DynamoDB or StepFuntions. Add those at Pipfile accordingly.

Final codes

All of the codes are in this GitHub repository:

https://github.com/melon-ruet/lambda-testing-localstack