AWS CLI を使用する Amazon SNS の例 - AWS Command Line Interface

AWS CLI を使用する Amazon SNS の例

次のコードサンプルは、Amazon SNS で AWS Command Line Interface を使用してアクションを実行し、一般的なシナリオを実装する方法を示しています。

アクションはより大きなプログラムからのコードの抜粋であり、コンテキスト内で実行する必要があります。アクションは個々のサービス機能を呼び出す方法を示していますが、コンテキスト内のアクションは、関連するシナリオで確認できます。

「シナリオ」は、1 つのサービス内から、または他の AWS のサービスと組み合わせて複数の関数を呼び出し、特定のタスクを実行する方法を示すコード例です。

各例には、完全なソースコードへのリンクが含まれており、そこからコンテキストにおけるコードの設定方法と実行方法についての手順を確認できます。

アクション

次の例は、add-permission を使用する方法を説明しています。

AWS CLI

トピックにアクセス許可を追加するには

次の add-permission の例では、AWS アカウント 123456789012 の指定されたトピックで Publish アクションを使用する 987654321098 アクセス許可を AWS アカウントに追加します。

aws sns add-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission \ --aws-account-id 987654321098 \ --action-name Publish

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「AddPermission」を参照してください。

次の例は、check-if-phone-number-is-opted-out を使用する方法を説明しています。

AWS CLI

電話番号での SMS メッセージのオプトアウトを確認するには

次の check-if-phone-number-is-opted-out の例では、指定した電話番号で現在の AWS アカウントからの SMS メッセージの受信をオプトアウトしたかどうかを確認します。

aws sns check-if-phone-number-is-opted-out \ --phone-number +1555550100

出力:

{ "isOptedOut": false }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「CheckIfPhoneNumberIsOptedOut」を参照してください。

次の例は、confirm-subscription を使用する方法を説明しています。

AWS CLI

サブスクリプションを確認するには

次の confirm-subscription コマンドは、my-topic という名前の SNS トピックをサブスクライブしたときに開始された確認プロセスを完了します。--token パラメータは、subscribe の呼び出しで指定した通知エンドポイントに送信される確認メッセージから取得されます。

aws sns confirm-subscription \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --token 2336412f37fb687f5d51e6e241d7700ae02f7124d8268910b858cb4db727ceeb2474bb937929d3bdd7ce5d0cce19325d036bc858d3c217426bcafa9c501a2cace93b83f1dd3797627467553dc438a8c974119496fc3eff026eaa5d14472ded6f9a5c43aec62d83ef5f49109da7176391

出力:

{ "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ConfirmSubscription」を参照してください。

次のコード例は、create-platform-application を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションを作成するには

次の create-platform-application の例では、指定されたプラットフォーム認証情報を使用して Google Firebase プラットフォームアプリケーションを作成します。

aws sns create-platform-application \ --name MyApplication \ --platform GCM \ --attributes PlatformCredential=EXAMPLEabcd12345jklm67890stuv12345bcdef

出力:

{ "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication" }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「CreatePlatformApplication」を参照してください。

次のコード例は、create-topic を使用する方法を示しています。

AWS CLI

SNS トピックを作成するには

次の create-topic の例では、my-topic という名前の SNS トピックを作成します。

aws sns create-topic \ --name my-topic

出力:

{ "ResponseMetadata": { "RequestId": "1469e8d7-1642-564e-b85d-a19b4b341f83" }, "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" }

詳細については、「AWS コマンドラインインターフェイスユーザーガイド」の「AWS コマンドラインインターフェイスで Amazon SQS と Amazon SNS を使用する」を参照してください。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「CreateTopic」を参照してください。

次のコード例は、delete-endpoint を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションのエンドポイントを削除するには

次の delete-endpoint の例では、指定されたプラットフォームアプリケーションのエンドポイントを削除します。

aws sns delete-endpoint \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「DeleteEndpoint」を参照してください。

次の例は、delete-platform-application を使用する方法を説明しています。

AWS CLI

プラットフォームアプリケーションを削除するには

次の delete-platform-application の例では、指定されたプラットフォームアプリケーションを削除します。

aws sns delete-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「DeletePlatformApplication」を参照してください。

次の例は、delete-topic を使用する方法を説明しています。

AWS CLI

SNS トピックを削除するには

次の delete-topic の例では、指定した SNS トピックを削除します。

aws sns delete-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「DeleteTopic」を参照してください。

次のコード例は、get-endpoint-attributes を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションエンドポイント属性を一覧表示するには

次の get-endpoint-attributes の例では、指定されたプラットフォームアプリケーションのエンドポイントの属性を一覧表示します。

aws sns get-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234

出力:

{ "Attributes": { "Enabled": "true", "Token": "EXAMPLE12345..." } }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「GetEndpointAttributes」を参照してください。

次のコード例は、get-platform-application-attributes を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションの属性を一覧表示するには

次の get-platform-application-attributes の例では、指定されたプラットフォームアプリケーションの属性を一覧表示します。

aws sns get-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/MPNS/MyApplication

出力:

{ "Attributes": { "Enabled": "true", "SuccessFeedbackSampleRate": "100" } }

次の例は、get-sms-attributes を使用する方法を説明しています。

AWS CLI

デフォルトの SMS メッセージ属性を一覧表示するには

次の get-sms-attributes の例では、SMS メッセージを送信するためのデフォルト属性を一覧表示しています。

aws sns get-sms-attributes

出力:

{ "attributes": { "DefaultSenderID": "MyName" } }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「GetSMSAttributes」を参照してください。

次の例は、get-subscription-attributes を使用する方法を説明しています。

AWS CLI

トピックのサブスクリプション属性を取得するには

次の get-subscription-attributes の例では、指定されたサブスクリプションの属性を表示します。list-subscriptions コマンドの出力から subscription-arn を取得できます。

aws sns get-subscription-attributes \ --subscription-arn "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f"

出力:

{ "Attributes": { "Endpoint": "my-email@example.com", "Protocol": "email", "RawMessageDelivery": "false", "ConfirmationWasAuthenticated": "false", "Owner": "123456789012", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「GetSubscriptionAttributes」を参照してください。

次のコード例は、get-topic-attributes を使用する方法を示しています。

AWS CLI

トピックの属性を取得するには

次の get-topic-attributes の例では、指定したトピックの属性を表示します。

aws sns get-topic-attributes \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

出力:

{ "Attributes": { "SubscriptionsConfirmed": "1", "DisplayName": "my-topic", "SubscriptionsDeleted": "0", "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}", "Owner": "123456789012", "Policy": "{\"Version\":\"2008-10-17\",\"Id\":\"__default_policy_ID\",\"Statement\":[{\"Sid\":\"__default_statement_ID\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":[\"SNS:Subscribe\",\"SNS:ListSubscriptionsByTopic\",\"SNS:DeleteTopic\",\"SNS:GetTopicAttributes\",\"SNS:Publish\",\"SNS:RemovePermission\",\"SNS:AddPermission\",\"SNS:SetTopicAttributes\"],\"Resource\":\"arn:aws:sns:us-west-2:123456789012:my-topic\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"0123456789012\"}}}]}", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionsPending": "0" } }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「GetTopicAttributes」を参照してください。

次の例は、list-endpoints-by-platform-application を使用する方法を説明しています。

AWS CLI

プラットフォームアプリケーションのエンドポイントを一覧表示するには

次の list-endpoints-by-platform-application の例では、指定されたプラットフォームアプリケーションのエンドポイントとエンドポイント属性を一覧表示します。

aws sns list-endpoints-by-platform-application \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication

出力:

{ "Endpoints": [ { "Attributes": { "Token": "EXAMPLE12345..., "Enabled": "true" }, "EndpointArn": "arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" } ] }

次のコード例は、list-phone-numbers-opted-out を使用する方法を示しています。

AWS CLI

SMS メッセージのオプトアウトを一覧表示するには

次の list-phone-numbers-opted-out の例では、SMS メッセージの受信をオプトアウトした電話番号を一覧表示しています。

aws sns list-phone-numbers-opted-out

出力:

{ "phoneNumbers": [ "+15555550100" ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListPhoneNumbersOptedOut」を参照してください。

次のコード例は、list-platform-applications を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションを一覧表示するには

次の list-platform-applications の例では、ADM と MPNS のプラットフォームアプリケーションを一覧表示します。

aws sns list-platform-applications

出力:

{ "PlatformApplications": [ { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } }, { "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/MPNS/MyOtherApplication", "Attributes": { "SuccessFeedbackSampleRate": "100", "Enabled": "true" } } ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListPlatformApplications」を参照してください。

次のコード例は、list-subscriptions-by-topic を使用する方法を示しています。

AWS CLI

トピックに関連付けられたサブスクリプションを一覧表示するには

次の list-subscriptions-by-topic の例では、指定されたトピックに関連付けられた SNS サブスクリプションのリストを取得します。

aws sns list-subscriptions-by-topic \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic"

出力:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListSubscriptionsByTopic」を参照してください。

次のコード例は、list-subscriptions を使用する方法を示しています。

AWS CLI

SNS サブスクリプションを一覧表示するには

次の list-subscriptions の例では、AWS アカウントの SNS サブスクリプションのリストを表示しています。

aws sns list-subscriptions

出力:

{ "Subscriptions": [ { "Owner": "123456789012", "Endpoint": "my-email@example.com", "Protocol": "email", "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic", "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" } ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListSubscriptions」を参照してください。

次のコード例は、list-tags-for-resource を使用する方法を示しています。

AWS CLI

トピックのタグを一覧表示するには

次の list-tags-for-resource の例では、指定された Amazon SNS トピックのタグを一覧表示します。

aws sns list-tags-for-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic

出力:

{ "Tags": [ { "Key": "Team", "Value": "Alpha" } ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListTagsForResource」を参照してください。

次のコード例は、list-topics を使用する方法を示しています。

AWS CLI

SNS トピックを一覧表示するには

次の list-topics の例では、AWS アカウント内のすべての SNS トピックを一覧表示します。

aws sns list-topics

出力:

{ "Topics": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } ] }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「ListTopics」を参照してください。

次のコード例は、opt-in-phone-number を使用する方法を示しています。

AWS CLI

SMS メッセージをオプトインするには

次の opt-in-phone-number の例では、指定された電話番号で SMS メッセージの受信をオプトインできるようにします。

aws sns opt-in-phone-number \ --phone-number +15555550100

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「OptInPhoneNumber」を参照してください。

次のコード例は、publish を使用する方法を示しています。

AWS CLI

例 1: トピックにメッセージを発行するには

次の publish の例では、指定した Amazon SNS トピックに指定した通知を公開します。メッセージはテキストファイルから取得されたもので、改行を含めることができます。

aws sns publish \ --topic-arn "arn:aws:sns:us-west-2:123456789012:my-topic" \ --message file://message.txt

message.txt の内容:

Hello World Second Line

出力:

{ "MessageId": "123a45b6-7890-12c3-45d6-111122223333" }

例 2: 電話番号に SMS メッセージを公開するには

次の publish の例では、Hello world! メッセージを電話番号 +1-555-555-0100 に公開します。

aws sns publish \ --message "Hello world!" \ --phone-number +1-555-555-0100

出力:

{ "MessageId": "123a45b6-7890-12c3-45d6-333322221111" }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「Publish」を参照してください。

次のコード例は、put-data-protection-policy を使用する方法を示しています。

AWS CLI

データ保護ポリシーを設定するには

例 1: パブリッシャーが CreditCardNumber を使用してメッセージをパブリッシュすることを拒否するには

次の put-data-protection-policy の例では、パブリッシャーによる CreditCardNumber を使用したメッセージのパブリッシュを拒否します。

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-east-1:123456789012:mytopic \ --data-protection-policy "{\"Name\":\"data_protection_policy\",\"Description\":\"Example data protection policy\",\"Version\":\"2021-06-01\",\"Statement\":[{\"DataDirection\":\"Inbound\",\"Principal\":[\"*\"],\"DataIdentifier\":[\"arn:aws:dataprotection::aws:data-identifier/CreditCardNumber\"],\"Operation\":{\"Deny\":{}}}]}"

このコマンドでは何も出力されません。

例 2: ファイルからパラメータをロードするには

次の put-data-protection-policy では、ファイルからパラメータをロードします。

aws sns put-data-protection-policy \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --data-protection-policy file://policy.json

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「PutDataProtectionPolicy」を参照してください。

次の例は、remove-permission を使用する方法を説明しています。

AWS CLI

トピックからアクセス許可を削除するには

次の remove-permission の例では、指定されたトピックからアクセス許可 Publish-Permission を削除します。

aws sns remove-permission \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --label Publish-Permission

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLIコマンドリファレンス」の「RemovePermission」を参照してください。

次の例は、set-endpoint-attributes を使用する方法を説明しています。

AWS CLI

エンドポイント属性を設定するには

次の set-endpoint-attributes の例では、指定されたプラットフォームアプリケーションエンドポイントを無効にします。

aws sns set-endpoint-attributes \ --endpoint-arn arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234 \ --attributes Enabled=false

出力:

{ "Attributes": { "Enabled": "false", "Token": "EXAMPLE12345..." } }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「SetEndpointAttributes」を参照してください。

次のコード例は、set-platform-application-attributes を使用する方法を示しています。

AWS CLI

プラットフォームアプリケーションの属性を設定するには

次の set-platform-application-attributes の例では、指定されたプラットフォームアプリケーションの EventDeliveryFailure 属性を、指定された Amazon SNS トピックの ARN に設定します。

aws sns set-platform-application-attributes \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --attributes EventDeliveryFailure=arn:aws:sns:us-west-2:123456789012:AnotherTopic

このコマンドでは何も出力されません。

次のコード例は、set-sms-attributes を使用する方法を示しています。

AWS CLI

SMS メッセージ属性を設定するには

次の set-sms-attributes の例では、SMS メッセージのデフォルトの送信者 ID を MyName に設定します。

aws sns set-sms-attributes \ --attributes DefaultSenderID=MyName

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「SetSMSAttributes」を参照してください。

次のコード例は、set-subscription-attributes を使用する方法を示しています。

AWS CLI

サブスクリプション属性を設定するには

次の set-subscription-attributes の例では、RawMessageDelivery 属性を SQS サブスクリプションに設定します。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name RawMessageDelivery \ --attribute-value true

このコマンドでは何も出力されません。

次の set-subscription-attributes の例では、FilterPolicy 属性を SQS サブスクリプションに設定します。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{ \"anyMandatoryKey\": [\"any\", \"of\", \"these\"] }"

このコマンドでは何も出力されません。

次の set-subscription-attributes の例では、FilterPolicy 属性を SQS サブスクリプションから削除します。

aws sns set-subscription-attributes \ --subscription-arn arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc \ --attribute-name FilterPolicy \ --attribute-value "{}"

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「SetSubscriptionAttributes」を参照してください。

次の例は、set-topic-attributes を使用する方法を説明しています。

AWS CLI

トピックの属性を設定するには

次の set-topic-attributes の例では、指定したトピックの DisplayName 属性を設定します。

aws sns set-topic-attributes \ --topic-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --attribute-name DisplayName \ --attribute-value MyTopicDisplayName

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「SetTopicAttributes」を参照してください。

次のコード例は、subscribe を使用する方法を示しています。

AWS CLI

トピックにサブスクライブするには

次の subscribe コマンドは、指定したトピックに E メールアドレスをサブスクライブします。

aws sns subscribe \ --topic-arn arn:aws:sns:us-west-2:123456789012:my-topic \ --protocol email \ --notification-endpoint my-email@example.com

出力:

{ "SubscriptionArn": "pending confirmation" }
  • API の詳細については、「AWS CLI コマンドリファレンス」の「Subscribe」を参照してください。

次のコード例は、tag-resource を使用する方法を示しています。

AWS CLI

トピックにタグを追加するには

次の tag-resource の例では、指定した Amazon SNS トピックにメタデータタグを追加します。

aws sns tag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tags Key=Team,Value=Alpha

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「TagResource」を参照してください。

次の例は、unsubscribe を使用する方法を説明しています。

AWS CLI

トピックからサブスクライブを解除するには

次の unsubscribe の例では、指定したサブスクリプションをトピックから削除します。

aws sns unsubscribe \ --subscription-arn arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「Unsubscribe」を参照してください。

次のコード例は、untag-resource を使用する方法を示しています。

AWS CLI

トピックからタグを削除するには

次の untag-resource の例では、指定された Amazon SNS トピックから、指定されたキーを持つタグを削除します。

aws sns untag-resource \ --resource-arn arn:aws:sns:us-west-2:123456789012:MyTopic \ --tag-keys Team

このコマンドでは何も出力されません。

  • API の詳細については、「AWS CLI コマンドリファレンス」の「UntagResource」を参照してください。

シナリオ

次のコード例は、Amazon SNS プッシュ通知のプラットフォームエンドポイントを作成する方法を示しています。

AWS CLI

プラットフォームアプリケーションのエンドポイントを作成するには

次の create-platform-endpoint の例では、指定したトークンを使用して、指定したプラットフォームアプリケーションのエンドポイントを作成します。

aws sns create-platform-endpoint \ --platform-application-arn arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication \ --token EXAMPLE12345...

出力:

{ "EndpointArn": "arn:aws:sns:us-west-2:1234567890:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" }