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.

Prerequisites

  • Up and running docker compose ubuntu 22.04 LTS machine.
  • Terrafrom should be instaled on the system.
  • Basic knowledge in docker CLI and terraform.

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:

nano docker-compose.yml        
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:

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.

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     = "https://localhost:4566"
    cloudformation = "https://localhost:4566"
    cloudwatch     = "https://localhost:4566"
    dynamodb       = "https://localhost:4566"
    ec2            = "https://localhost:4566"
    es             = "https://localhost:4566"
    firehose       = "https://localhost:4566"
    iam            = "https://localhost:4566"
    kinesis        = "https://localhost:4566"
    kms            = "https://localhost:4566"
    lambda         = "https://localhost:4566"
    rds            = "https://localhost:4566"
    redshift       = "https://localhost:4566"
    route53        = "https://localhost:4566"
    s3             = "https://localhost:4566"
    secretsmanager = "https://localhost:4566"
    ses            = "https://localhost:4566"
    sns            = "https://localhost:4566"
    sqs            = "https://localhost:4566"
    ssm            = "https://localhost:4566"
    sts            = "https://localhost:4566"
  }
}        

Step 5: Testing with Terraform

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

To initialize the terraform with provider.

terraform init        

To get plan

terraform plan        

To apply

terraform apply        

Step 6: LocalStack Container 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.



要查看或添加评论,请登录

社区洞察

其他会员也浏览了