This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.
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-permission
example adds the permission for Amazon account987654321098
to use thePublish
action with the specified topic under Amazon account123456789012
.aws sns add-permission \ --topic-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --labelPublish-Permission
\ --aws-account-id987654321098
\ --action-namePublish
This 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-out
example 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
+1555550100
Output:
{ "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-subscription
command 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-arn
arn:aws:sns:us-west-2:123456789012:my-topic
\ --token2336412f37fb687f5d51e6e241d7700ae02f7124d8268910b858cb4db727ceeb2474bb937929d3bdd7ce5d0cce19325d036bc858d3c217426bcafa9c501a2cace93b83f1dd3797627467553dc438a8c974119496fc3eff026eaa5d14472ded6f9a5c43aec62d83ef5f49109da7176391
Output:
{ "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-application
example creates a Google Firebase platform application using the specified platform credential.aws sns create-platform-application \ --name
MyApplication
\ --platformGCM
\ --attributesPlatformCredential=EXAMPLEabcd12345jklm67890stuv12345bcdef
Output:
{ "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-topic
example creates an SNS topic namedmy-topic
.aws sns create-topic \ --name
my-topic
Output:
{ "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-endpoint
example deletes the specified platform application endpoint.aws sns delete-endpoint \ --endpoint-arn
arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234
This 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-application
example deletes the specified platform application.aws sns delete-platform-application \ --platform-application-arn
arn:aws:sns:us-west-2:123456789012:app/ADM/MyApplication
This 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-topic
example 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-attributes
example lists the attributes for the specified platform application endpoint.aws sns get-endpoint-attributes \ --endpoint-arn
arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234
Output:
{ "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-attributes
example lists the attributes for the specified platform application.aws sns get-platform-application-attributes \ --platform-application-arn
arn:aws:sns:us-west-2:123456789012:app/MPNS/MyApplication
Output:
{ "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-attributes
example lists the default attributes for sending SMS messages.aws sns get-sms-attributes
Output:
{ "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-attributes
displays the attributes of the specified subscription. You can get thesubscription-arn
from the output of thelist-subscriptions
command.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-attributes
example 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-application
example lists the endpoints and endpoint attributes for the specified platform application.aws sns list-endpoints-by-platform-application \ --platform-application-arn
arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication
Output:
{ "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-out
example lists the phone numbers opted out of receiving SMS messages.aws sns list-phone-numbers-opted-out
Output:
{ "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-applications
example lists the platform applications for ADM and MPNS.aws sns list-platform-applications
Output:
{ "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-topic
retrieves 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-subscriptions
example displays a list of the SNS subscriptions in your Amazon account.aws sns list-subscriptions
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 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-resource
example lists the tags for the specified Amazon SNS topic.aws sns list-tags-for-resource \ --resource-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
Output:
{ "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-topics
example lists all of SNS topics in your Amazon account.aws sns list-topics
Output:
{ "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-number
example opts the specified phone number into receiving SMS messages.aws sns opt-in-phone-number \ --phone-number
+15555550100
This 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
publish
example 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.txt
Contents of
message.txt
:Hello World Second Line
Output:
{ "MessageId": "123a45b6-7890-12c3-45d6-111122223333" }
Example 2: To publish an SMS message to a phone number
The following
publish
example publishes the messageHello world!
to the phone number+1-555-555-0100
.aws sns publish \ --message
"Hello world!"
\ --phone-number+1-555-555-0100
Output:
{ "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-policy
example denies publishers from publishing messages with 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\":{}}}]}"This command produces no output.
Example 2: To load parameters from a file
The following
put-data-protection-policy
loads parameters from a file.aws sns put-data-protection-policy \ --resource-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --data-protection-policyfile://policy.json
This 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-permission
example removes the permissionPublish-Permission
from the specified topic.aws sns remove-permission \ --topic-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --labelPublish-Permission
This 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-attributes
example disables the specified platform application endpoint.aws sns set-endpoint-attributes \ --endpoint-arn
arn:aws:sns:us-west-2:123456789012:endpoint/GCM/MyApplication/12345678-abcd-9012-efgh-345678901234
\ --attributesEnabled=false
Output:
{ "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-attributes
example sets theEventDeliveryFailure
attribute for the specified platform application to the ARN of the specified Amazon SNS topic.aws sns set-platform-application-attributes \ --platform-application-arn
arn:aws:sns:us-west-2:123456789012:app/GCM/MyApplication
\ --attributesEventDeliveryFailure=arn:aws:sns:us-west-2:123456789012:AnotherTopic
This 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-attributes
example sets the default sender ID for SMS messages toMyName
.aws sns set-sms-attributes \ --attributes
DefaultSenderID=MyName
This 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-attributes
example sets theRawMessageDelivery
attribute to an SQS subscription.aws sns set-subscription-attributes \ --subscription-arn
arn:aws:sns:us-east-1:123456789012:mytopic:f248de18-2cf6-578c-8592-b6f1eaa877dc
\ --attribute-nameRawMessageDelivery
\ --attribute-valuetrue
This command produces no output.
The following
set-subscription-attributes
example sets aFilterPolicy
attribute to an SQS subscription.aws sns set-subscription-attributes \ --subscription-arn
arn: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-attributes
example removes theFilterPolicy
attribute from an SQS subscription.aws sns set-subscription-attributes \ --subscription-arn
arn: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-attributes
example sets theDisplayName
attribute for the specified topic.aws sns set-topic-attributes \ --topic-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --attribute-nameDisplayName
\ --attribute-valueMyTopicDisplayName
This 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
subscribe
command subscribes an email address to the specified topic.aws sns subscribe \ --topic-arn
arn:aws:sns:us-west-2:123456789012:my-topic
\ --protocolemail
\ --notification-endpointmy-email@example.com
Output:
{ "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-resource
example adds a metadata tag to the specified Amazon SNS topic.aws sns tag-resource \ --resource-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --tagsKey=Team,Value=Alpha
This 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
unsubscribe
example deletes the specified subscription from a topic.aws sns unsubscribe \ --subscription-arn
arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f
This 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-resource
example removes any tags with the specified keys from the specified Amazon SNS topic.aws sns untag-resource \ --resource-arn
arn:aws:sns:us-west-2:123456789012:MyTopic
\ --tag-keysTeam
This 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-endpoint
example creates an endpoint for the specified platform application using the specified token.aws sns create-platform-endpoint \ --platform-application-arn
arn: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" }