Using Terraform with LocalStack

Using Terraform with LocalStack

In our previous article, Running AWS Lambda Functions Locally Using LocalStack, we saw how simple it was to interact with AWS SAM and AWS CLI. However, we are not limited to those tools; several integrations are available, and Terraform is among the most popular.


Running LocalStack

Create a docker-compose.yml file with the following content:

version: "3.8"
    container_name: "my-localstack"
    image: localstack/localstack
      - ""
      - ""
      - DEBUG=1
      - DOCKER_HOST=unix:///var/run/docker.sock
      - ".volume/tmp/localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

Run docker-compose up. To verify the availability of all services, navigate to http://localhost:4566/health in a web browser.

Terraform Script

Create a file as follows:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.22.0"

provider "aws"{
  region                      = "us-east-1"
  access_key                  = "mock_access_key"
  secret_key                  = "mock_secret_key"
  s3_use_path_style           = true
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true 
  endpoints {
    s3             = "http://localhost:4566"

resource "aws_s3_bucket" "my-bucket" {
  bucket = "my-bucket"

The arguments region, access_key, and secret_key are mock credentials. Let's discuss the other arguments:

  • skip_credentials_validation: Whether to skip credentials validation via the STS API.

  • skip_metadata_api_check: Whether to skip the AWS Metadata API check.

  • skip_requesting_account_id: Whether to skip requesting the account ID.

  • s3_use_path_style: Whether to enable the request to use path-style addressing, i.e.,

  • endpoints: Configuration block for customizing service endpoints. In our case, we are only using s3, but we might need to set up the rest of the services.

Run the following commands:

terraform init
terraform plan -out app.tfplan
terraform apply 'app.tfplan'

And that’s it. Run aws s3 ls --endpoint-url=http://localhost:4566 to see the list of buckets. Using Terraform with LocalStack enables us to accelerate Infrastructure as Code (IaC) development, identify errors sooner, and eliminate expenses during testing. Thank you, and happy coding!