https://docs.aws.amazon.com/ja_jp/sns/latest/dg/sns-configure-dead-letter-queue.html
-- 1. コマンド等のインストール
-- 1.1 aws cli version 2 インストール
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
-- 1.2 jqインストール
sudo yum -y install jq
-- 2. SQSキュー作成
qu01 --> SNSサブスクライブ用
qu02 --> デッドレターキュー用
aws sqs create-queue \
--queue-name qu01
aws sqs create-queue \
--queue-name qu02
aws sqs list-queues
-- 3. SNSトピック作成
aws sns list-topics
aws sns list-subscriptions
aws sns create-topic --name topic01
aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--protocol sqs \
--notification-endpoint arn:aws:sqs:ap-northeast-1:999999999999:qu01
-- 4. SQSアクセスポリシー設定
aws sqs set-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01 \
--attributes '{"Policy": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:ap-northeast-1:999999999999:qu01\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:ap-northeast-1:999999999999:topic01\"}}}]}"}'
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01 \
--attribute-names Policy
aws sqs set-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02 \
--attributes '{"Policy": "{\"Version\":\"2008-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"SQS:SendMessage\",\"Resource\":\"arn:aws:sqs:ap-northeast-1:999999999999:qu02\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"arn:aws:sns:ap-northeast-1:999999999999:topic01\"}}}]}"}'
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02 \
--attribute-names Policy
-- 5. サブスクリプションのデッドレターキューを設定
aws sns set-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555 \
--attribute-name RedrivePolicy \
--attribute-value "{\"deadLetterTargetArn\": \"arn:aws:sqs:ap-northeast-1:999999999999:qu02\"}"
aws sns get-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555
-- 6. 動作確認
aws sns publish \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--message "message01" \
--subject "subject01"
メッセージ数確認
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01 \
--attribute-names ApproximateNumberOfMessages
aws sqs get-queue-attributes \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02 \
--attribute-names ApproximateNumberOfMessages
メッセージ受信
aws sqs receive-message \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01
aws sqs receive-message \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02
全メッセージ削除
aws sqs purge-queue \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01
aws sqs purge-queue \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02
-- 7. クリーンアップ
-- SNSトピック削除
aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01
aws sns list-topics
aws sns list-subscriptions
-- SQSキュー削除
aws sqs list-queues
aws sqs delete-queue \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu01
aws sqs delete-queue \
--queue-url https://sqs.ap-northeast-1.amazonaws.com/999999999999/qu02