Amazon SNS examples using Amazon CLI
The following code examples show you how to perform actions and implement common scenarios by using the Amazon Command Line Interface with Amazon SNS.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Scenarios are code examples that show you how to accomplish specific tasks by calling multiple functions within a service or combined with other Amazon Web Services services.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Actions
The following code example shows how to use add-permission.
- Amazon CLI
-
To add a permission to a topic
The following
add-permissionexample adds the permission for Amazon account987654321098to use thePublishaction with the specified topic under Amazon account123456789012.aws sns add-permission \ --topic-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --labelPublish-Permission\ --aws-account-id987654321098\ --action-namePublishThis command produces no output.
-
For API details, see AddPermission
in Amazon CLI Command Reference.
-
The following code example shows how to use check-if-phone-number-is-opted-out.
- Amazon CLI
-
To check SMS message opt-out for a phone number
The following
check-if-phone-number-is-opted-outexample checks whether the specified phone number is opted out of receiving SMS messages from the current Amazon account.aws sns check-if-phone-number-is-opted-out \ --phone-number+1555550100Output:
{ "isOptedOut": false }-
For API details, see CheckIfPhoneNumberIsOptedOut
in Amazon CLI Command Reference.
-
The following code example shows how to use confirm-subscription.
- Amazon CLI
-
To confirm a subscription
The following
confirm-subscriptioncommand completes the confirmation process started when you subscribed to an SNS topic namedmy-topic. The --token parameter comes from the confirmation message sent to the notification endpoint specified in the subscribe call.aws sns confirm-subscription \ --topic-arnarn:aws:sns:us-west-2:123456789012:my-topic\ --token2336412f37fb687f5d51e6e241d7700ae02f7124d8268910b858cb4db727ceeb2474bb937929d3bdd7ce5d0cce19325d036bc858d3c217426bcafa9c501a2cace93b83f1dd3797627467553dc438a8c974119496fc3eff026eaa5d14472ded6f9a5c43aec62d83ef5f49109da7176391Output:
{ "SubscriptionArn": "arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f" }-
For API details, see ConfirmSubscription
in Amazon CLI Command Reference.
-
The following code example shows how to use create-platform-application.
- Amazon CLI
-
To create a platform application
The following
create-platform-applicationexample creates a Google Firebase platform application using the specified platform credential.aws sns create-platform-application \ --nameMyApplication\ --platformGCM\ --attributesPlatformCredential=EXAMPLEabcd12345jklm67890stuv12345bcdefOutput:
{ "PlatformApplicationArn": "arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication" }-
For API details, see CreatePlatformApplication
in Amazon CLI Command Reference.
-
The following code example shows how to use create-topic.
- Amazon CLI
-
To create an SNS topic
The following
create-topicexample creates an SNS topic namedmy-topic.aws sns create-topic \ --namemy-topicOutput:
{ "ResponseMetadata": { "RequestId": "1469e8d7-1642-564e-b85d-a19b4b341f83" }, "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" }For more information, see Using the Amazon Command Line Interface with Amazon SQS and Amazon SNS
in the Amazon Command Line Interface User Guide. -
For API details, see CreateTopic
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-endpoint.
- Amazon CLI
-
To delete a platform application endpoint
The following
delete-endpointexample deletes the specified platform application endpoint.aws sns delete-endpoint \ --endpoint-arnarn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234This command produces no output.
-
For API details, see DeleteEndpoint
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-platform-application.
- Amazon CLI
-
To delete a platform application
The following
delete-platform-applicationexample deletes the specified platform application.aws sns delete-platform-application \ --platform-application-arnarn:aws:sns:us-west-2:123456789012:app/ADM/MyApplicationThis command produces no output.
-
For API details, see DeletePlatformApplication
in Amazon CLI Command Reference.
-
The following code example shows how to use delete-topic.
- Amazon CLI
-
To delete an SNS topic
The following
delete-topicexample deletes the specified SNS topic.aws sns delete-topic \ --topic-arn"arn:aws:sns:us-west-2:123456789012:my-topic"This command produces no output.
-
For API details, see DeleteTopic
in Amazon CLI Command Reference.
-
The following code example shows how to use get-endpoint-attributes.
- Amazon CLI
-
To list platform application endpoint attributes
The following
get-endpoint-attributesexample lists the attributes for the specified platform application endpoint.aws sns get-endpoint-attributes \ --endpoint-arnarn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234Output:
{ "Attributes": { "Enabled": "true", "Token": "EXAMPLE12345..." } }-
For API details, see GetEndpointAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use get-platform-application-attributes.
- Amazon CLI
-
To list the platform application attributes
The following
get-platform-application-attributesexample lists the attributes for the specified platform application.aws sns get-platform-application-attributes \ --platform-application-arnarn:aws:sns:us-west-2:123456789012:app/MPNS/MyApplicationOutput:
{ "Attributes": { "Enabled": "true", "SuccessFeedbackSampleRate": "100" } }-
For API details, see GetPlatformApplicationAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use get-sms-attributes.
- Amazon CLI
-
To list the default SMS message attributes
The following
get-sms-attributesexample lists the default attributes for sending SMS messages.aws sns get-sms-attributesOutput:
{ "attributes": { "DefaultSenderID": "MyName" } }-
For API details, see GetSMSAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use get-subscription-attributes.
- Amazon CLI
-
To retrieve subscription attributes for a topic
The following
get-subscription-attributesdisplays the attributes of the specified subscription. You can get thesubscription-arnfrom the output of thelist-subscriptionscommand.aws sns get-subscription-attributes \ --subscription-arn"arn:aws:sns:us-west-2:123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f"Output:
{ "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" } }-
For API details, see GetSubscriptionAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use get-topic-attributes.
- Amazon CLI
-
To retrieve the attributes of a topic
The following
get-topic-attributesexample displays the attributes for the specified topic.aws sns get-topic-attributes \ --topic-arn"arn:aws:sns:us-west-2:123456789012:my-topic"Output:
{ "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" } }-
For API details, see GetTopicAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use list-endpoints-by-platform-application.
- Amazon CLI
-
To list the endpoints for a platform application
The following
list-endpoints-by-platform-applicationexample lists the endpoints and endpoint attributes for the specified platform application.aws sns list-endpoints-by-platform-application \ --platform-application-arnarn:aws:sns:us-west-2:123456789012:app/GCM/MyApplicationOutput:
{ "Endpoints": [ { "Attributes": { "Token": "EXAMPLE12345..., "Enabled": "true" }, "EndpointArn": "arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" } ] }-
For API details, see ListEndpointsByPlatformApplication
in Amazon CLI Command Reference.
-
The following code example shows how to use list-phone-numbers-opted-out.
- Amazon CLI
-
To list SMS message opt-outs
The following
list-phone-numbers-opted-outexample lists the phone numbers opted out of receiving SMS messages.aws sns list-phone-numbers-opted-outOutput:
{ "phoneNumbers": [ "+15555550100" ] }-
For API details, see ListPhoneNumbersOptedOut
in Amazon CLI Command Reference.
-
The following code example shows how to use list-platform-applications.
- Amazon CLI
-
To list platform applications
The following
list-platform-applicationsexample lists the platform applications for ADM and MPNS.aws sns list-platform-applicationsOutput:
{ "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" } } ] }-
For API details, see ListPlatformApplications
in Amazon CLI Command Reference.
-
The following code example shows how to use list-subscriptions-by-topic.
- Amazon CLI
-
To list the subscriptions associated with a topic
The following
list-subscriptions-by-topicretrieves a list of SNS subscriptions associated with the specified topic.aws sns list-subscriptions-by-topic \ --topic-arn"arn:aws:sns:us-west-2:123456789012:my-topic"Output:
{ "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" } ] }-
For API details, see ListSubscriptionsByTopic
in Amazon CLI Command Reference.
-
The following code example shows how to use list-subscriptions.
- Amazon CLI
-
To list your SNS subscriptions
The following
list-subscriptionsexample displays a list of the SNS subscriptions in your Amazon account.aws sns list-subscriptionsOutput:
{ "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" } ] }-
For API details, see ListSubscriptions
in Amazon CLI Command Reference.
-
The following code example shows how to use list-tags-for-resource.
- Amazon CLI
-
To list tags for a topic
The following
list-tags-for-resourceexample lists the tags for the specified Amazon SNS topic.aws sns list-tags-for-resource \ --resource-arnarn:aws:sns:us-west-2:123456789012:MyTopicOutput:
{ "Tags": [ { "Key": "Team", "Value": "Alpha" } ] }-
For API details, see ListTagsForResource
in Amazon CLI Command Reference.
-
The following code example shows how to use list-topics.
- Amazon CLI
-
To list your SNS topics
The following
list-topicsexample lists all of SNS topics in your Amazon account.aws sns list-topicsOutput:
{ "Topics": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } ] }-
For API details, see ListTopics
in Amazon CLI Command Reference.
-
The following code example shows how to use opt-in-phone-number.
- Amazon CLI
-
To opt-in for SMS messages
The following
opt-in-phone-numberexample opts the specified phone number into receiving SMS messages.aws sns opt-in-phone-number \ --phone-number+15555550100This command produces no output.
-
For API details, see OptInPhoneNumber
in Amazon CLI Command Reference.
-
The following code example shows how to use publish.
- Amazon CLI
-
Example 1: To publish a message to a topic
The following
publishexample publishes the specified message to the specified SNS topic. The message comes from a text file, which enables you to include line breaks.aws sns publish \ --topic-arn"arn:aws:sns:us-west-2:123456789012:my-topic"\ --messagefile://message.txtContents of
message.txt:Hello World Second LineOutput:
{ "MessageId": "123a45b6-7890-12c3-45d6-111122223333" }Example 2: To publish an SMS message to a phone number
The following
publishexample publishes the messageHello world!to the phone number+1-555-555-0100.aws sns publish \ --message"Hello world!"\ --phone-number+1-555-555-0100Output:
{ "MessageId": "123a45b6-7890-12c3-45d6-333322221111" }-
For API details, see Publish
in Amazon CLI Command Reference.
-
The following code example shows how to use put-data-protection-policy.
- Amazon CLI
-
To set data protection policy
Example 1: To deny publishers from publishing messages with CreditCardNumber
The following
put-data-protection-policyexample denies publishers from publishing messages with CreditCardNumber.aws sns put-data-protection-policy \ --resource-arnarn: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\":{}}}]}"This command produces no output.
Example 2: To load parameters from a file
The following
put-data-protection-policyloads parameters from a file.aws sns put-data-protection-policy \ --resource-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --data-protection-policyfile://policy.jsonThis command produces no output.
-
For API details, see PutDataProtectionPolicy
in Amazon CLI Command Reference.
-
The following code example shows how to use remove-permission.
- Amazon CLI
-
To remove a permission from a topic
The following
remove-permissionexample removes the permissionPublish-Permissionfrom the specified topic.aws sns remove-permission \ --topic-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --labelPublish-PermissionThis command produces no output.
-
For API details, see RemovePermission
in Amazon CLI Command Reference.
-
The following code example shows how to use set-endpoint-attributes.
- Amazon CLI
-
To set endpoint attributes
The following
set-endpoint-attributesexample disables the specified platform application endpoint.aws sns set-endpoint-attributes \ --endpoint-arnarn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234\ --attributesEnabled=falseOutput:
{ "Attributes": { "Enabled": "false", "Token": "EXAMPLE12345..." } }-
For API details, see SetEndpointAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use set-platform-application-attributes.
- Amazon CLI
-
To set platform application attributes
The following
set-platform-application-attributesexample sets theEventDeliveryFailureattribute for the specified platform application to the ARN of the specified Amazon SNS topic.aws sns set-platform-application-attributes \ --platform-application-arnarn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication\ --attributesEventDeliveryFailure=arn:aws:sns:us-west-2:123456789012:AnotherTopicThis command produces no output.
-
For API details, see SetPlatformApplicationAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use set-sms-attributes.
- Amazon CLI
-
To set SMS message attributes
The following
set-sms-attributesexample sets the default sender ID for SMS messages toMyName.aws sns set-sms-attributes \ --attributesDefaultSenderID=MyNameThis command produces no output.
-
For API details, see SetSMSAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use set-subscription-attributes.
- Amazon CLI
-
To set subscription attributes
The following
set-subscription-attributesexample sets theRawMessageDeliveryattribute to an SQS subscription.aws sns set-subscription-attributes \ --subscription-arnarn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc\ --attribute-nameRawMessageDelivery\ --attribute-valuetrueThis command produces no output.
The following
set-subscription-attributesexample sets aFilterPolicyattribute to an SQS subscription.aws sns set-subscription-attributes \ --subscription-arnarn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc\ --attribute-nameFilterPolicy\ --attribute-value "{ \"anyMandatoryKey\": [\"any\", \"of\", \"these\"] }"This command produces no output.
The following
set-subscription-attributesexample removes theFilterPolicyattribute from an SQS subscription.aws sns set-subscription-attributes \ --subscription-arnarn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc\ --attribute-nameFilterPolicy\ --attribute-value"{}"This command produces no output.
-
For API details, see SetSubscriptionAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use set-topic-attributes.
- Amazon CLI
-
To set an attribute for a topic
The following
set-topic-attributesexample sets theDisplayNameattribute for the specified topic.aws sns set-topic-attributes \ --topic-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --attribute-nameDisplayName\ --attribute-valueMyTopicDisplayNameThis command produces no output.
-
For API details, see SetTopicAttributes
in Amazon CLI Command Reference.
-
The following code example shows how to use subscribe.
- Amazon CLI
-
To subscribe to a topic
The following
subscribecommand subscribes an email address to the specified topic.aws sns subscribe \ --topic-arnarn:aws:sns:us-west-2:123456789012:my-topic\ --protocolemail\ --notification-endpointmy-email@example.comOutput:
{ "SubscriptionArn": "pending confirmation" }-
For API details, see Subscribe
in Amazon CLI Command Reference.
-
The following code example shows how to use tag-resource.
- Amazon CLI
-
To add a tag to a topic
The following
tag-resourceexample adds a metadata tag to the specified Amazon SNS topic.aws sns tag-resource \ --resource-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --tagsKey=Team,Value=AlphaThis command produces no output.
-
For API details, see TagResource
in Amazon CLI Command Reference.
-
The following code example shows how to use unsubscribe.
- Amazon CLI
-
To unsubscribe from a topic
The following
unsubscribeexample deletes the specified subscription from a topic.aws sns unsubscribe \ --subscription-arnarn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7fThis command produces no output.
-
For API details, see Unsubscribe
in Amazon CLI Command Reference.
-
The following code example shows how to use untag-resource.
- Amazon CLI
-
To remove a tag from a topic
The following
untag-resourceexample removes any tags with the specified keys from the specified Amazon SNS topic.aws sns untag-resource \ --resource-arnarn:aws:sns:us-west-2:123456789012:MyTopic\ --tag-keysTeamThis command produces no output.
-
For API details, see UntagResource
in Amazon CLI Command Reference.
-
Scenarios
The following code example shows how to create a platform endpoint for Amazon SNS push notifications.
- Amazon CLI
-
To create a platform application endpoint
The following
create-platform-endpointexample creates an endpoint for the specified platform application using the specified token.aws sns create-platform-endpoint \ --platform-application-arnarn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication\ --tokenEXAMPLE12345...Output:
{ "EndpointArn": "arn:aws:sns:us-west-2:1234567890:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234" }