Docker, Localstack, and CloudFormation

Marcos
3 min readApr 10, 2023

--

Hello, my name is Marcos and in this text, we will build some services in Localstack using CloudFormation, while Localstack is running using Docker.

First, what is AWS CloudFormation?

Excerpt of text taken from AWS itself:

For a scalable web application that also includes a backend database, you might use an Auto Scaling group, an Elastic Load Balancing load balancer, and an Amazon Relational Database Service database instance. You might use each individual service to provision these resources and after you create the resources, you would have to configure them to work together. All these tasks can add complexity and time before you even get your application up and running.

Instead, you can create a CloudFormation template or modify an existing one. A template describes all your resources and their properties. When you use that template to create a CloudFormation stack, CloudFormation provisions the Auto Scaling group, load balancer, and database for you. After the stack has been successfully created, your AWS resources are up and running. You can delete the stack just as easily, which deletes all the resources in the stack. By using CloudFormation, you easily manage a collection of resources as a single unit.

Link: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html

Running Localstack

At first, you have to use a docker image, in this case, I used docker-compose:

version: "3.8"

services:
localstack:
container_name: localstack
image: localstack/localstack:0.14.2
network_mode: bridge
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:53:53" #
- "127.0.0.1:53:53/udp" #
- "127.0.0.1:443:443" #
- "127.0.0.1:4510-4530:4510-4530" # ext services port range
- "127.0.0.1:4571:4571" #
environment:
- DEBUG=${DEBUG-}
- SERVICES=${SERVICES-}
- DATA_DIR=${DATA_DIR-}
- LAMBDA_EXECUTOR=local
- LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-}
- HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
- DOCKER_HOST=unix:///var/run/docker.sock
- DISABLE_CORS_CHECKS=1
volumes:
- "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"

Creating a queue using CloudFormation

Create a file called cloudformation-example.yaml:

Resources:
LocalSQS:
Type: AWS::SQS::Queue
Properties:
QueueName: sqs-test-1
VisibilityTimeout: 300
UpdateReplacePolicy: Delete
DeletionPolicy: Delete

Now, execute this command:

aws --endpoint-url=http://localhost:4566 cloudformation deploy --stack-name cloudformation-example.yml --template-file ".\cloudformation-example.yml"

The result was:

Let’s verify if the queue was created, for this you can use this

aws --endpoint-url=http://127.0.0.1:4566 sqs list-queues

And the result was:

Nice, huh?

Adding an S3 Bucket


Resources:
LocalSQS:
Type: AWS::SQS::Queue
Properties:
QueueName: sqs-test-1
VisibilityTimeout: 300
UpdateReplacePolicy: Delete
DeletionPolicy: Delete
LocalBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: bucket-test-1

You can use this command in order to verify if the bucket was created:

aws --endpoint-url=http://127.0.0.1:4566 s3api list-buckets

And the result was:

Some notes

  • This command makes it unnecessary to access the container to execute some localstack’s API commands:
aws --endpoint-url=http://127.0.0.1:4566 s3api list-buckets
aws --endpoint-url=http://127.0.0.1:4566 sqs list-queues
  • It’s possible to create a lot of services like API Gateway, AWS SNS, EC2, etc.
  • On some days I will post an article about API Gateway and certainly, I will use AWS CloudFormation to deploy it.
  • Here you can build some spring boot services to integrate with these two examples: S3 and SQS.

The repo is here: https://github.com/mmarcosab/cloudformation-example

That’s it for today =]

--

--