Create SQS queues on localstack container start

Put this file to your filesystem as ←anyname.sh→:

#!/usr/bin/env bash

set -euo pipefail

# enable debug
# set -x


echo "configuring sqs"
echo "==================="
LOCALSTACK_HOST=localhost
AWS_REGION=eu-central-1


# https://docs.aws.amazon.com/cli/latest/reference/sqs/create-queue.html
create_queue() {
	local QUEUE_NAME_TO_CREATE=$1
	awslocal --endpoint-url=http://${LOCALSTACK_HOST}:4566 sqs create-queue --queue-name ${QUEUE_NAME_TO_CREATE} --region ${AWS_REGION} --attributes VisibilityTimeout=30
}

create_queue "queue2"
create_queue "queue1"

For instance, ./localstack_bootstrap/sqs_bootstrap.sh.
Don’t forget to run chmod +x ./localstack_bootstrap/sqs_bootstrap.sh.

Add this line to your docker-compose.yml:
- ./localstack_bootstrap:/docker-entrypoint-initaws.d/.
So it could look like this:

services:
  localstack:
    container_name: aws_sqs
    hostname: sqs
    image: localstack/localstack:latest
    environment:
      - AWS_DEFAULT_REGION=ap-southeast-1
      - EDGE_PORT=4566
      - SERVICES=sqs
    ports:
      - '4566:4566'
    volumes:
      - "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
      - ./localstack_bootstrap:/docker-entrypoint-initaws.d/

Then your queues can be reached by URLs
http://localhost:4566/000000000000/queue1
and
http://localhost:4566/000000000000/queue2.

Further reading:
https://docs.aws.amazon.com/cli/latest/reference/sqs/create-queue.html
https://joerg-pfruender.github.io/software/docker/microservices/testing/2020/01/25/Localstack_in_Docker.html

LEAVE A COMMENT