1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LocalStack を用いたAmazon SNS動作確認方法メモ

Posted at
  • LocalStack上で動作するAmazon SNSをAWS CLIで操作し、動作確認する。

LocalStack準備

  • docker-compose.yml

    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
    
  • 起動

    docker-compose up
    

動作確認

トピックの作成

aws sns create-topic --name test-topic --endpoint-url http://localhost:4566 --profile localstack 
{
    "TopicArn": "arn:aws:sns:ap-northeast-1:000000000000:test-topic"
}

トピックへのサブスクライブ

  • email プロトコルと notification-endpoint の E メールアドレスを指定する。
aws sns subscribe --topic-arn arn:aws:sns:ap-northeast-1:000000000000:test-topic --protocol email --notification-endpoint test@example.com --endpoint-url http://localhost:4566 --profile localstack 
{
    "SubscriptionArn": "arn:aws:sns:ap-northeast-1:000000000000:test-topic:ac2e3322-10a1-413d-98aa-f0fe5c34cb44"
}

トピックへの発行

  • トピックのすべての受信者にメッセージを送信する
aws sns publish --topic-arn arn:aws:sns:ap-northeast-1:000000000000:test-topic --message "Hello SNS!" --endpoint-url http://localhost:4566 --profile localstack
{
    "MessageId": "f39fe4ce-15e8-4333-a7b8-2c75d3b27ed9"
}

トピックからサブスクリプションを解除する

  • トピックのサブスクリプションを解除し、そのトピックに発行されたメッセージの受信を停止する。
aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:000000000000:test-topic:ac2e3322-10a1-413d-98aa-f0fe5c34cb44 --endpoint-url http://localhost:4566 --profile localstack

トピックの削除

# トピック確認
aws sns list-topics --endpoint-url http://localhost:4566 --profile localstack
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:ap-northeast-1:000000000000:test-topic"
        }
    ]
}
# トピック削除
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:000000000000:test-topic --endpoint-url http://localhost:4566 --profile localstack

# トピック確認(削除されたことの確認)
aws sns list-topics --endpoint-url http://localhost:4566 --profile localstack
{
    "Topics": []
}

参考情報

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?