LocalStack を用いたAWS SQS動作確認方法メモ

* LocalStack上で動くAWS SQSをAWS CLIで操作して動作確認する。

## LocalStack準備

* `docker-compose.yml`

  ```yaml
  version: "3.8"
  networks:
    container-link:
      name: docker.internal
  services:
    localstack:
      container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
      image: localstack/localstack
      ports:
        - "127.0.0.1:53:53"                # only required for Pro
        - "127.0.0.1:53:53/udp"            # only required for Pro
        - "127.0.0.1:443:443"              # only required for Pro
        - "127.0.0.1:4510-4530:4510-4530"  # only required for Pro
        - "127.0.0.1:4566:4566"
        - "127.0.0.1:4571:4571"
      environment:
        - SERVICES=${SERVICES- }
        - DEBUG=${DEBUG- }
        - DATA_DIR=${DATA_DIR- }
        - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
        - LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY- }  # only required for Pro
        - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
        - DOCKER_HOST=unix:///var/run/docker.sock
      volumes:
        - "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
        - "/var/run/docker.sock:/var/run/docker.sock"
      networks:
        - container-link
  ```

* 起動

  ```shell
  docker-compose up
  ```

  



## 動作確認

### キュー作成

```shell
aws sqs create-queue --queue-name test-queue --endpoint-url http://localhost:4566 --profile localstack 
{
    "QueueUrl": "http://localhost:4566/000000000000/test-queue"
}
```



### キューの確認

```shell
aws sqs list-queues --endpoint-url http://localhost:4566 --profile localstack 
{
    "QueueUrls": [
        "http://localhost:4566/000000000000/test-queue"
    ]
}
```



```shell
aws sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/test-queue --attribute-names All --endpoint-url http://localhost:4566 --profile localstack 
{
    "Attributes": {
        "ApproximateNumberOfMessages": "0",
        "ApproximateNumberOfMessagesDelayed": "0",
        "ApproximateNumberOfMessagesNotVisible": "0",
        "CreatedTimestamp": "1635594888.406909",
        "DelaySeconds": "0",
        "LastModifiedTimestamp": "1635594888.406909",
        "MaximumMessageSize": "262144",
        "MessageRetentionPeriod": "345600",
        "QueueArn": "arn:aws:sqs:ap-northeast-1:000000000000:test-queue",
        "ReceiveMessageWaitTimeSeconds": "0",
        "VisibilityTimeout": "30"
    }
}
```



### メッセージをキューに送信する

```shell
 aws sqs send-message --queue-url "http://localhost:4566/000000000000/test-queue" --message-body "hello sqs" --endpoint-url http://localhost:4566 --profile localstack
 {
    "MD5OfMessageBody": "3b7bef57d06c0021d0aafe8f6d587241",
    "MessageId": "f6c15b00-73fe-703c-9dea-58a452416096"
}
```



### キューにあるメッセージ数を確認する

```shell
aws sqs get-queue-attributes --queue-url 'http://localhost:4566/000000000000/test-queue' --attribute-names ApproximateNumberOfMessages --query 'Attributes.ApproximateNumberOfMessages' --endpoint-url http://localhost:4566 --profile localstack
"1"
```



### メッセージをキューから消費する

```shell
aws sqs receive-message --queue-url 'http://localhost:4566/000000000000/test-queue' --endpoint-url http://localhost:4566 --profile localstack
{
    "Messages": [
        {
            "MessageId": "f6c15b00-73fe-703c-9dea-58a452416096",
            "ReceiptHandle": "qalxyspqosaejvtpyzymnopxtjvfyahzzfuelsesxkjcnqkiwgsztcklctzrxzwqdsimfpawzjwkwgleykejpskdhkhytmrtciocgegskkinjaqzvqnkvuzylkelzwckqtwfioxaywpfhfkfqghaorhiluvpmsdydkrkawatjlqircwnykvdayydy",
            "MD5OfBody": "3b7bef57d06c0021d0aafe8f6d587241",
            "Body": "hello sqs"
        }
    ]
}
```



### キューからメッセージを削除する

```shell
# メッセージ削除
aws sqs delete-message --queue-url 'http://localhost:4566/000000000000/test-queue' --receipt-handle "afofosypthoqphrwwnayjnlnoiuvhvoqzrawwlerwtitpxbyskgksvnzkqrwtwcvvavdcnlrlbdawkzyyijgoznacuiavkkygokonnoxdstwcsktczryujjsypselwdeoicoivsmjgqkxiilhbjnzojdqmzvsjqtqpjpiqqbscoemntwzufgjdkje" --endpoint-url http://localhost:4566 --profile localstack
# メッセージ数確認
aws sqs get-queue-attributes --queue-url 'http://localhost:4566/000000000000/test-queue' --attribute-names ApproximateNumberOfMessages --query 'Attributes.ApproximateNumberOfMessages' --endpoint-url http://localhost:4566 --profile localstack"0"
```