How to Subscribe to AWS SNS Topic [SMS/Email/Lambda] via CLI


If you want to subscribe to an AWS SNS Topic via the AWS CLI, then you can make use of the aws sns subscribe command.

Let's see step-by-step how to do that.

Step 1: Get the list of all SNS Topics

% aws sns list-topics

-----------------------------------------------------
|                    ListTopics                     |
+---------------------------------------------------+
||                     Topics                      ||
|+-------------------------------------------------+|
||                    TopicArn                     ||
|+-------------------------------------------------+|
||  arn:aws:sns:us-east-1:107xx89xx95x:apiTopic    ||
||  arn:aws:sns:us-east-1:107xx89xx95x:emailTopic  ||
||  arn:aws:sns:us-east-1:107xx89xx95x:smsTopic    ||
|+-------------------------------------------------+|

Step 2: Subscribing to a SNS Topic

    Example 1: Subscribing an Mobile number to an SNS topic:

    % aws sns subscribe 
         --topic-arn arn:aws:sns:us-east-1:107xx89xx95x:smsTopic 
         --protocol sms 
         --notification-endpoint +1-mobile-no
    ---------------- -----------------
    |                                               Subscribe |
    +-----------------+--------------------- ---------------------+
    |  SubscriptionArn|  arn:aws:sns:us-east-1:107xx89xx95x:smsTopic:6b20axxd-c0xx. |
    +-----------------+------------------+

    Example 2: Subscribing an Email Address to an SNS topic:

    % aws sns subscribe 
         --topic-arn arn:aws:sns:us-east-1: 107xx89xx95x:emailTopic 
         --protocol email 
         --notification-endpoint email@example.com

    Example 3: Subscribing an Email Address to an SNS topic:

    % aws sns subscribe 
         --topic-arn arn:aws:sns:us-east-1: 107xx89xx95x:emailTopic 
         --protocol lambda 
         --notification-endpoint arn:aws:lambda:us-east-1:107xx89xx95x:function:SNSLambdaInvocationFunc
Subscribing to an AWS SNS Topic Example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap