O LocalStack é um emulador de serviços na nuvem que permite que serviços AWS sejam testados localmente, sem que seja necessário se conectar a uma conta remota.
O que iremos fazer:
Vou documentar aqui como executei o LocalStack pela primeira vez.
Os passos que segui foram:
- iniciar um container com o LocalStack usado
docker-compose
- configurar o
aws cli
- criar e consumir uma fila SQS
Vou utilizar o docker-compose
, conforme a documentação disponível na página https://docs.localstack.cloud/get-started/#docker-compose.
Requisitos:
docker
-
docker-compose
versão >= 1.9.0 - AWS Cli
Iniciando o container
Iremos utilizar o arquivo disponibilizado na própria página do LocalStack:
version: "3.8" services: localstack: container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}" image: localstack/localstack ports: - "127.0.0.1:4566:4566" # LocalStack Gateway - "127.0.0.1:4510-4559:4510-4559" # external services port range - "127.0.0.1:53:53" # DNS config (only required for Pro) - "127.0.0.1:53:53/udp" # DNS config (only required for Pro) - "127.0.0.1:443:443" # LocalStack HTTPS Gateway (only required for Pro) environment: - DEBUG=${DEBUG-} - PERSISTENCE=${PERSISTENCE-} - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR-} # - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY-} # only required for Pro - DOCKER_HOST=unix:///var/run/docker.sock volumes: - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" - "/var/run/docker.sock:/var/run/docker.sock"
E iniciar o container:
docker compose up
Configurando AWS Cli
Caso não tenha o Cli da AWS pode seguir essa documentação: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
$ aws --version $ export AWS_ACCESS_KEY_ID="test" $ export AWS_SECRET_ACCESS_KEY="test" $ export AWS_DEFAULT_REGION="us-east-1" $ aws configure --profile localstack $ aws configure list
Criando uma fila
Criando uma fila chamada "teste", utilizando o profile "localstack":
$ aws sqs create-queue --endpoint-url http://localhost:4566 --queue-name teste --profile localstack { "QueueUrl": "http://localhost:4566/000000000000/teste" }
Listando filas
Conferindo as filas criadas:
$ aws sqs list-queues --endpoint-url http://localhost:4566 --profile localstack { "QueueUrls": [ "http://localhost:4566/000000000000/teste" ] }
Enviando uma mensagem à fila
Antes de mais nada vamos criar um arquivo message.json
contendo os atributos da mensagem:
{ "id": { "DataType": "String", "StringValue": "e8f50240-be67-463a-a479-d540697931c0" }, "name": { "DataType": "String", "StringValue": "blue-car-toy" } }
Após isso irei enviar a nossa fila "teste" a mensagem "Mensagem de Teste":
$ aws sqs send-message --endpoint-url http://localhost:4566 --queue-url http://localhost:4566/000000000000/teste --message-body "Mensagem de Teste" --message-attributes file://./message.json --profile localstack { "MD5OfMessageBody": "90a9cc3ce1350489e33ee38d19bf287a", "MD5OfMessageAttributes": "16894947ed19d2117e32efdb4f734250", "MessageId": "bc406501-3632-4f87-934b-11a1c909f8c6" }
Recebendo as mensagens da fila
Lendo a mensagem da fila, uma a uma:
$ aws sqs receive-message --endpoint-url http://localhost:4566 --queue-url http://localhost:4566/000000000000/teste --attribute-names All --message-attribute-names All --profile localstack { "Messages": [ { "MessageId": "bc406501-3632-4f87-934b-11a1c909f8c6", "ReceiptHandle": "N2VkNDdhZGQtMmUxZS00NmVjLWE5NzYtNGQzM2 ExNWZmODVlIGFybjphd3M6c3FzOnVzLWVhc3QtMTowMDAwMDAwMDAwMDA6dGVzdGUgYm M0MDY1MDEtMzYzMi00Zjg3LTkzNGItMTFhMWM5MDlmOGM2IDE2NjU5MzkyODIuNDQ3NTc4NA==", "MD5OfBody": "90a9cc3ce1350489e33ee38d19bf287a", "Body": "Mensagem de Teste", "Attributes": { "SenderId": "000000000000", "SentTimestamp": "1665939195032", "ApproximateReceiveCount": "1", "ApproximateFirstReceiveTimestamp": "1665939282447" }, "MD5OfMessageAttributes": "16894947ed19d2117e32efdb4f734250", "MessageAttributes": { "id": { "StringValue": "e8f50240-be67-463a-a479-d540697931c0", "DataType": "String" }, "name": { "StringValue": "blue-car-toy", "DataType": "String" } } } ] }
É isso! 😉
Referências:
https://github.com/flflima/localstack-docker
LocalStack
AWS Cli
Top comments (0)