Archive · AWS

LocalStack in Tests: Terraform and CI

Apply Terraform to LocalStack and run integration tests in a pipeline.

1 min read

  • LocalStack 4.0
  • tflocal
  • Terraform 1.10
  • GitHub Actions

tflocal wraps Terraform and points the AWS provider at LocalStack:

pip install terraform-local
tflocal init
tflocal apply -auto-approve
awslocal s3 ls

Test your infrastructure code and application against it before touching a real account.

A CI job with LocalStack as a service container:

jobs:
  integration:
    runs-on: ubuntu-latest
    services:
      localstack:
        image: localstack/localstack:4.0
        ports: ["4566:4566"]
    env:
      AWS_ENDPOINT_URL: http://localhost:4566
      AWS_ACCESS_KEY_ID: test
      AWS_SECRET_ACCESS_KEY: test
      AWS_DEFAULT_REGION: ap-south-1
    steps:
      - uses: actions/checkout@v4
      - run: pip install awscli-local terraform-local
      - run: tflocal init && tflocal apply -auto-approve
      - run: npm ci && npm run test:integration

Seed resources on start-up by mounting scripts into /etc/localstack/init/ready.d/. Keep a small number of tests against real AWS too, since an emulator can differ from the real service.