【AWS CLI】SNS関連の情報取得編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。

はじめに

今回は、SNS関連の情報を取得する方法をご紹介いたします。

利用するコマンド,サブコマンド

まず、AWS CLIの構造は以下の通りです。

aws <command> <subcommand> [options and parameters]

上記を前提に今回使う <command>,<subcommand>は、以下の通りです。

<command>

  • sns

<subcommand>

  • list-topics
    Topicの一覧を出力します。
  • get-topic-attributes
    Topicの詳細を出力します。
  • list-subscriptions
    Subscriptionの一覧を出力します。
  • get-subscription-attributes
    Subscriptionの詳細を出力します。

Topicの一覧

Topicの一覧

  • 実行コマンド
aws sns list-topics --query "Topics[].[TopicArn]" --output text
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG
arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns
arn:aws:sns:ap-northeast-1:111111111111:codestar-notifications-

Topicのディスプレイ名,Confirme数,Delete数

  • 実行コマンド
aws sns list-topics --query "Topics[].[TopicArn]" --output text | while read line
do    
   echo $line
   aws sns get-topic-attributes --topic-arn $line --query "Attributes.[DisplayName,SubscriptionsConfirmed,SubscriptionsDeleted]" --output text
done
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic
autoscaling-topic       0       0
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG
        1       0
arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns
chat-bot-sns    0       1
arn:aws:sns:ap-northeast-1:111111111111:codestar-notifications-
        0       0

Topicのポリシー一覧

  • 実行コマンド
aws sns list-topics --query "Topics[].[TopicArn]" --output text | while read line
do
   echo $line
   aws sns get-topic-attributes --topic-arn $line --query "Attributes.Policy" --output text | jq
done
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic
{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": [
        "SNS:GetTopicAttributes",
        "SNS:SetTopicAttributes",
        "SNS:AddPermission",
        "SNS:RemovePermission",
        "SNS:DeleteTopic",
        "SNS:Subscribe",
        "SNS:ListSubscriptionsByTopic",
        "SNS:Publish",
        "SNS:Receive"
      ],
      "Resource": "arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic",
      "Condition": {
        "StringEquals": {
          "AWS:SourceOwner": "111111111111"
        }
      }
    }
  ]
}
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG
{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": [
        "SNS:GetTopicAttributes",
        "SNS:SetTopicAttributes",
        "SNS:AddPermission",
        "SNS:RemovePermission",
        "SNS:DeleteTopic",
        "SNS:Subscribe",
        "SNS:ListSubscriptionsByTopic",
        "SNS:Publish",
        "SNS:Receive"
      ],
      "Resource": "arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG",
      "Condition": {
        "StringEquals": {
          "AWS:SourceOwner": "111111111111"
        }
      }
    }
  ]
}

DeliveryPolicy一覧

  • 実行コマンド
aws sns list-topics --query "Topics[].[TopicArn]" --output text | while read line
do    
   echo $line
   aws sns get-topic-attributes --topic-arn $line --query "Attributes.[EffectiveDeliveryPolicy]" --output text | jq
done
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic
{
  "http": {
    "defaultHealthyRetryPolicy": {
      "minDelayTarget": 20,
      "maxDelayTarget": 20,
      "numRetries": 3,
      "numMaxDelayRetries": 0,
      "numNoDelayRetries": 0,
      "numMinDelayRetries": 0,
      "backoffFunction": "linear"
    },
    "disableSubscriptionOverrides": false
  }
}
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG
{
  "http": {
    "defaultHealthyRetryPolicy": {
      "minDelayTarget": 20,
      "maxDelayTarget": 20,
      "numRetries": 3,
      "numMaxDelayRetries": 0,
      "numNoDelayRetries": 0,
      "numMinDelayRetries": 0,
      "backoffFunction": "linear"
    },
    "disableSubscriptionOverrides": false
  }
}

Topicの各種ロール一覧

  • 実行コマンド
aws sns list-topics --query "Topics[].[TopicArn]" --output text | while read line
do
   echo $line
   aws sns get-topic-attributes --topic-arn $line --query "Attributes.[LambdaSuccessFeedbackRoleArn,LambdaFailureFeedbackRoleArn,HTTPSuccessFeedbackRoleArn,HTTPFailureFeedbackRoleArn]" --output text
done
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:autoscaling-topic
None    None    None    None
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG
None    None    None    None
arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns
None    None    None    None
arn:aws:sns:ap-northeast-1:111111111111:codestar-notifications-
arn:aws:iam::111111111111:role/SNSSuccessFeedback       arn:aws:iam::111111111111:role/SNSFailureFeedback       arn:aws:iam::111111111111:role/SNSSuccessFeedback       arn:aws:iam::111111111111:role/SNSFailureFeedback

Subscriptionsの一覧

Protocol,Endpoint

  • 実行コマンド
aws sns list-subscriptions --query "Subscriptions[].[Protocol,Endpoint]" --output text
  • 実行結果
email   hoge@gmail.com
https   https://global.sns-api.chatbot.amazonaws.com
lambda  arn:aws:lambda:ap-northeast-1:111111111111:function:marklogic-NodeMgrLambdaStack--NodeManagerFunction3-WRG44onEPNdE
sqs     arn:aws:sqs:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupQueue-3KH4TGA6XAMU

TopicArn,SubscriptionArn

  • 実行コマンド
aws sns list-subscriptions --query "Subscriptions[].[TopicArn,SubscriptionArn]" --output text
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns    arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns:fd223e0e-1e69-4a59-a642-43f014eb9c2b
arn:aws:sns:ap-northeast-1:111111111111:datahub-NodeMgrLambdaStack-TQYCU36Z4IFO-NodeManagerSnsTopic-4QKM54WKPVU1        arn:aws:sns:ap-northeast-1:111111111111:datahub-NodeMgrLambdaStack-TQYCU36Z4IFO-NodeManagerSnsTopic-4QKM54WKPVU1:4763e054-48a3-46f4-b7ee-cdc40c2bc26a

Protocol,Endpoint,PendingConfirmation,ConfirmationWasAuthenticated

  • 実行コマンド
aws sns list-subscriptions --query "Subscriptions[].[SubscriptionArn]" --output text | while read line
do
   echo $line
   aws sns get-subscription-attributes --subscription-arn $line --query "Attributes.[Protocol,Endpoint,PendingConfirmation,ConfirmationWasAuthenticated]" --output text
done
  • 実行結果
arn:aws:sns:ap-northeast-1:111111111111:marklogic-stack-02-NodeMgrLambdaStack-1KG604K4JY4TO-NodeManagerSnsTopic-16HASYLCAQFOD:fd693a5e-1ceb-4b23-8c48-d19b98552018
lambda  arn:aws:lambda:ap-northeast-1:111111111111:function:marklogic-stack-02-NodeMgrLamb-NodeManagerFunction-1G7690IE46Y11    false   true
arn:aws:sns:ap-northeast-1:111111111111:test:253f691c-cc18-4a34-85af-8f918b6ba1a5
email   xxxxxx@gmail.com     false   false
arn:aws:sns:ap-northeast-1:111111111111:chat-bot-sns:fd223e0e-1e69-4a59-a642-43f014eb9c2b
https   https://global.sns-api.chatbot.amazonaws.com    false   false
arn:aws:sns:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupTopic-15LBB16O3AJVG:e7de114f-4227-4068-ab1b-a9ef9c5315dd
sqs     arn:aws:sqs:ap-northeast-1:111111111111:awseb-e-5fy4tauppe-stack-NewSignupQueue-3KH4TGA6XAMU    false   true

終わりに

今回は、SNS関連の情報を取得するコマンドをご紹介いたしました。 どなたかのお役に立てれば幸いです。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。