{SNS}サブスクリプションフィルターポリシーを適用する

https://docs.aws.amazon.com/ja_jp/sns/latest/dg/sns-subscription-filter-policies.html
https://dev.classmethod.jp/articles/send-sms-only-to-subscriptions-that-match-message-filtering-policies-on-amazon-sns/

 


-- 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. 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 email \
--notification-endpoint hoge@example.com

 


-- 3. サブスクリプションフィルターポリシーを適用する

 

aws sns set-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555 \
--attribute-name FilterPolicy \
--attribute-value "{\"key\":[\"topic01\"]}"

 

aws sns get-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555

 

-- 4. 動作確認

aws sns publish \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--message "message01" \
--subject "subject01" \
--message-attributes '{"key": {"DataType": "String","StringValue": "topic01"}}'

 

メールが届くことを確認

aws sns publish \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--message "message11" \
--subject "subject11" \
--message-attributes '{"key": {"DataType": "String","StringValue": "topic11"}}'


メールが届かないことを確認

 

-- 5. クリーンアップ

-- サブスクリプションフィルターポリシーの削除

aws sns set-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555 \
--attribute-name FilterPolicy \
--attribute-value "{}"


aws sns get-subscription-attributes \
--subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555

 

-- 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