Test Lambda before deployment on CI using LocalStack

In my previous posts, I covered how to write tests for Lambdas, run them on LocalStack and addressed the issue I encountered while working on LocalStack. Now, in this post, I will show you how to set up a GitHub Action job to run tests on LocalStack before deploying Lambdas to production. At Qxf2, we place high importance on CI/CD, and it is crucial for us to test Lambdas before deploying them into production. LocalStack proves to be helpful by allowing us to deploy Lambdas locally for testing purposes.

CICD Workflow
CICD Workflow

Nowadays, we are using Github Action for CI/CD. Look at the steps below for setup.
Steps to setup with Github Action:
1. Create a .yml file under .github/workflow directory
2. Write a workflow to run Lambda tests on LocalStack and then deploy to production
3. Use act tool to test your workflow locally
4. Push your code and merge it into master/main branch


1. Create a .yml file under .github/workflow directory:

Please skip this step if you already have a YAML file for GitHub Actions. If you haven’t set up GitHub Actions yet, please create a directory named .github/workflows and create a file named url-filter-lambda.yml


2. Write a workflow to run Lambda tests on LocalStack and then deploy to production:

Now we need to write a workflow to start LocalStack and run the test that deploys the Lambda and tests it. Please refer our previous blog for the test and docker-compose file used to start LocalStack. Look at the workflow below:

name: Test with LocalStack
 
on:
  push:
    branches:
      - main # Adjust the branch name if needed
 
jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
 
      - name: Set up Docker Compose
        run: |
          sudo apt-get update
          sudo apt-get install -y docker-compose
 
      - name: Start LocalStack
        run: |
          docker-compose -f docker-compose.yml up -d
 
      - name: Wait for LocalStack to be ready
        run: |
          timeout 300 bash -c 'until echo > /dev/tcp/localhost/4566; do sleep 1; done'
 
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.8" # Replace version with your desired Python version 
 
      - name: Install dependencies
        run: pip install -r requirements.txt 
 
      - name: Run tests
        run: pytest -s -v test_lambda.py # Replace with the appropriate command to run your tests
 
      - name: Stop LocalStack
        run: docker-compose -f docker-compose.yml down
 
  production:
    name: Deploy to production
    needs: test
    runs-on: ubuntu-latest
 
    steps:
    .......
    .......
    .......

Refer to our GitHub Action script of the URL filter Lambda for your reference here. Our workflow tests the Lambda on LocalStack before deploying it to the Staging and the Production.


3. Use act tool to test your workflow locally:

To test a workflow locally before pushing the changes, we need to use ‘act’ tool. Refer to the nektos/act README and install the ‘act’ tool. Once you’re set with the ‘act’ tool, run the script using the ‘act’ command.

Command to run the workflow:

 act -W <Workflow yml file path --env-file env_staging.env

In the above command, you can use the –env-file option if you want to pass environment variable values, or you can skip it. For more commands, please refer to the nektos/act readme file.


4. Push your code and merge it into master/main branch:

Once you can successfully run the workflow using ‘act,’ you can push your changes and merge them with the master/main branch. After the merge, GitHub Actions trigger the workflow for every push to the branch mentioned under the workflow. It runs the test on LocalStack, and if the test runs successfully, it proceeds to the next jobs.


I hope this blog helps you get set up with GitHub Actions for your Lambda deployment.


Hire testers from Qxf2!

Hiring Qxf2 brings technical expertise that extends beyond traditional test automation. Our skilled testers possess diverse technical skills and practical knowledge. We excel in crafting and executing comprehensive test strategies, enabling seamless integration of testing into the development process. Our team’s proficiency in various tools and frameworks ensures efficient testing of complex software systems, contributing to enhanced product quality and faster iteration cycles.


Leave a Reply

Your email address will not be published. Required fields are marked *