This documentation is for Version 1 of the AWS CLI only. For documentation related to Version 2 of the AWS CLI, see the Version 2 User Guide.
Amazon SNS examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS 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 AWS 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.
- AWS CLI
- 
             
                    To add a permission to a topic The following add-permissionexample adds the permission for AWS account987654321098to use thePublishaction with the specified topic under AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use check-if-phone-number-is-opted-out.
- AWS 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 AWS account.aws sns check-if-phone-number-is-opted-out \ --phone-number+1555550100Output: { "isOptedOut": false }- 
                    For API details, see CheckIfPhoneNumberIsOptedOut in AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use confirm-subscription.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use create-platform-application.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use create-topic.
- AWS 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 AWS Command Line Interface with Amazon SQS and Amazon SNS in the AWS Command Line Interface User Guide. - 
                    For API details, see CreateTopic in AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use delete-endpoint.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use delete-platform-application.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use delete-topic.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use get-endpoint-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use get-platform-application-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use get-sms-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use get-subscription-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use get-topic-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-endpoints-by-platform-application.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-phone-numbers-opted-out.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-platform-applications.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-subscriptions-by-topic.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-subscriptions.
- AWS CLI
- 
             
                    To list your SNS subscriptions The following list-subscriptionsexample displays a list of the SNS subscriptions in your AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-tags-for-resource.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use list-topics.
- AWS CLI
- 
             
                    To list your SNS topics The following list-topicsexample lists all of SNS topics in your AWS account.aws sns list-topicsOutput: { "Topics": [ { "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-topic" } ] }- 
                    For API details, see ListTopics in AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use opt-in-phone-number.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use publish.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use put-data-protection-policy.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use remove-permission.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use set-endpoint-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use set-platform-application-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use set-sms-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use set-subscription-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use set-topic-attributes.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use subscribe.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use tag-resource.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use unsubscribe.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
The following code example shows how to use untag-resource.
- AWS 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 AWS CLI Command Reference. 
 
- 
                    
Scenarios
The following code example shows how to create a platform endpoint for Amazon SNS push notifications.
- AWS 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" }