This article is the second part of a series about AWS SNS. Be sure to read through all the posts by clicking on the links below:
AWS SNS: The Complete Guide – Part 1: Theory
AWS SNS: The Complete Guide – Part 2: AWS CLI SNS Commands
AWS SNS: The Complete Guide – Part 3: AWS SNS Boto3 Interface
AWS SNS: The Complete Guide – Part 4: AWS SNS Unit Testing Using Moto
AWS SNS: The Complete Guide – Part 5: AWS SNS using Terraform
AWS SNS: The Complete Guide – Part 6: AWS SNS FAQ

We discussed AWS SNS in theory and all related concepts in the previous article. This article focuses on using AWS CLI to perform various AWS SNS operations like creating a topic, publishing data, etc.

Create SNS topic using AWS-CLI:

The below command creates the standard SNS topic.

aws sns create-topic --name test-topic

Sample Output
{
    "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
}

Set SNS topic attribute using AWS-CLI

aws sns set-topic-attributes --topic-arn arn:aws:sns:us-east-1:123456789012:test-topic --attribute-name DisplayName --attribute-value MyTopicDisplayName

Sample Output:
None

List all SNS topics using AWS-CLI.

aws sns list-topics

Sample Output
{
    "Topics": [
        {
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
        }
    ]
}

Get attributes for a single SNS topic using AWS-CLI.

aws sns get-topic-attributes --topic-arn "arn:aws:sns:us-east-1:123456789012:test-topic"

Sample Output
{
    "Attributes": {
        "Policy": "{\"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\"],\"Resource\":\"arn:aws:sns:us-east-1:123456789012:test-topic\",\"Condition\":{\"StringEquals\":{\"AWS:SourceOwner\":\"123456789012\"}}}]}",
        "Owner": "123456789012",
        "SubscriptionsPending": "0",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic",
        "EffectiveDeliveryPolicy": "{\"http\":{\"defaultHealthyRetryPolicy\":{\"minDelayTarget\":20,\"maxDelayTarget\":20,\"numRetries\":3,\"numMaxDelayRetries\":0,\"numNoDelayRetries\":0,\"numMinDelayRetries\":0,\"backoffFunction\":\"linear\"},\"disableSubscriptionOverrides\":false}}",
        "SubscriptionsConfirmed": "0",
        "DisplayName": "MyTopicDisplayName",
        "SubscriptionsDeleted": "0"
    }
}

Publish messages to SNS topic using AWS-CLI

aws sns publish --topic-arn "arn:aws:sns:us-east-1:123456789012:test-topic" --message file://message.txt

Sample Output
{
    "MessageId": "4648178f-18be-418e-bf06-20e42e70db6e"
}

Subscribe to the SNS topic using AWS-CLI

aws sns subscribe --topic-arn arn:aws:sns:us-east-1:123456789012:test-topic --protocol email --notification-endpoint test@gmail.com

Sample Output
{
    "SubscriptionArn": "pending confirmation"
}

Set SNS Subscription attributes using AWS-CLI

aws sns set-subscription-attributes --subscription-arn arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857 --attribute-name RawMessageDelivery --attribute-value true

List all the SNS subscriptions using AWS-CLI.

aws sns list-subscriptions

Sample Output
{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857",
            "Owner": "123456789012",
            "Protocol": "email",
            "Endpoint": "test@gmail.com",
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
        }
    ]
}

Get the list of all the subscription attributes using AWS-CLI.

aws sns get-subscription-attributes --subscription-arn "arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857"

Sample Output
{
    "Attributes": {
        "Owner": "123456789012",
        "RawMessageDelivery": "false",
        "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic",
        "Endpoint": "test@gmail.com",
        "Protocol": "email",
        "PendingConfirmation": "false",
        "ConfirmationWasAuthenticated": "false",
        "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857"
    }
}

List the subscription for a particular topic using AWS-CLI.

aws sns list-subscriptions-by-topic --topic-arn "arn:aws:sns:us-east-1:123456789012:test-topic"

Sample Output
{
    "Subscriptions": [
        {
            "SubscriptionArn": "arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857",
            "Owner": "123456789012",
            "Protocol": "email",
            "Endpoint": "test@gmail.com",
            "TopicArn": "arn:aws:sns:us-east-1:123456789012:test-topic"
        }
    ]
}

Unsubscribe from SNS topic using AWS-CLI

aws sns unsubscribe --subscription-arn arn:aws:sns:us-east-1:123456789012:test-topic:27f2927d-5271-4e7d-b78a-0fdc4d68c857

Sample Output
None

Delete an SNS topic using AWS-CLI

aws sns delete-topic  --topic-arn "arn:aws:sns:us-east-1:123456789012:test-topic"

Sample Output
None

Tag the SNS resource using AWS-CLI

aws sns tag-resource  --resource-arn arn:aws:sns:us-east-1:123456789012:test-topic --tags Key=Team,Value=Alpha


Sample Output
None

Remove the tag from the SNS resource using AWS-CLI.

aws sns untag-resource --resource-arn arn:aws:sns:us-east-1:123456789012:test-topic --tag-keys Team

Sample Output
None

Add permissions to SNS topic using AWS-CLI.

aws sns add-permission --topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic --label Publish-Permission --aws-account-id 123456789013 --action-name Publish

Sample Output
None

Remove permission from the SNS topic using AWS-CLI.

aws sns remove-permission --topic-arn arn:aws:sns:us-east-1:123456789012:MyTopic --label Publish-Permission
Sample Output
None

Conclusion

In this article, we discussed how to use AWS CLI for performing various AWS SNS operations.

Related Articles

AWS SNS: The Complete Guide
AWS SNS: What it is, how does it work and what are the benefits?
AWS SNS: The Complete Guide to Python and BOTO3 Interface
AWS SNS: The Complete Guide to AWS-CLI SNS Commands