Deploy AWS LocalStack using Docker Composer

Introduction

LocalStack is an open source tool that lets you simulate AWS (Amazon Web Services) cloud services, on your own computer. It creates a self contained and user environment for developers to experiment with and build AWS applications without worrying about any real costs from AWS usage. The main goal of LocalStack is to mirror the functionality of AWS services giving you the flexibility to develop and test your applications offline or, in a development setting.

Step 1: Creating Docker Compose File

Create a docker-compose.yml file to define the LocalStack service and its dependencies. Here’s an example docker-compose.yml file:

version: '3'
services:
  localstack:
    image: localstack/localstack
    container_name: localstack_main
    ports:
      - "4566:4566"
      - "4571:4571"
      - "8055:8080"
    environment:
      - SERVICES=sqs,s3,lambda,cloudwatch,iam,apigateway
      - DEBUG=1
      - PERSISTENCE=1
    volumes:
      - "./localstack_data:/var/lib/localstack" # Mounts the local directory to the container
      - "/var/run/docker.sock:/var/run/docker.sock" # Mounts the docker socket

This example sets up LocalStack to emulate several AWS services (S3, Lambda, CloudWatch, IAM, and API Gateway) and maps their respective ports.

Step 2: Deployment of AWS LocalStack

Start LocalStack Container: Open a terminal and navigate to the directory containing your docker-compose.yml file. Run the following command to start the LocalStack container:

sudo docker compose up -d

This will download the LocalStack image if you don’t already have it and start the services you specified.

Step 3: Verify the LocalStack Container

To validate the localstack container, We can execute the given command.

sudo docker ps 

Step 4: Configure with Terraform

We need to use the follwing terraform code that configure terrafrom with localstack locally.

Create a provider.tf file and add the following code.

provider "aws" {
  region                      = "us-east-1"
  access_key                  = "test"
  secret_key                  = "test"


  endpoints {
    apigateway     = "http://localhost:4566"
    cloudformation = "http://localhost:4566"
    cloudwatch     = "http://localhost:4566"
    dynamodb       = "http://localhost:4566"
    ec2            = "http://localhost:4566"
    es             = "http://localhost:4566"
    firehose       = "http://localhost:4566"
    iam            = "http://localhost:4566"
    kinesis        = "http://localhost:4566"
    kms            = "http://localhost:4566"
    lambda         = "http://localhost:4566"
    rds            = "http://localhost:4566"
    redshift       = "http://localhost:4566"
    route53        = "http://localhost:4566"
    s3             = "http://localhost:4566"
    secretsmanager = "http://localhost:4566"
    ses            = "http://localhost:4566"
    sns            = "http://localhost:4566"
    sqs            = "http://localhost:4566"
    ssm            = "http://localhost:4566"
    sts            = "http://localhost:4566"
  }
}

Step 5: Testing with Terraform

We need to execute the followijg command to test connection between localstack and terraform.

terraform init

Step 6: Clean-up

To stop and remove the LocalStack container when you’re done, press Ctrl+C in the terminal where you started the container, and then run the following command to remove the stopped container:

docker-compose down

Conclusion

That’s it! You now have a LocalStack instance running in a Docker container, emulating AWS services locally for development and testing purposes. You can customize the docker-compose.yml file to add or remove services and configure specific settings as needed.

Author

Deploy AWS LocalStack using Docker Composer

Leave a Reply

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

Scroll to top