How to use CloudFormation to Create SNS Topic and Subscription

How to use CloudFormation to Create SNS Topic and Subscription
Sharing is Caring:

Dear Reader, Hope you are doing well. In this post, you will learn to use AWS CloudFormation to Create SNS Topic and Subscription. We will be discussing the creation of Standard topic as well as FIFO topic(Yes they do exists and are meant to be used with SQS FIFO queues 🙂 ).

Let’s start with understanding amazon SNS a bit and then we’ll proceed towards creation of SNS Topic and Subscription using CloudForamation.

Alright?

Lets go !!!

What is Amazon SNS?

Amazon SNS or Simple Notification Service is a fully managed(serverless) many to many messaging Service provided by AWS. It provides application to application as well as application to person communication.

It’s based on pub-sub(Publish-Subscribe) concept. You create a topic which is a logical access point and publish the message to topic. Whoever needs to get your message needs to subscribe to the topic. Once a message is published to a topic, SNS will send the message to all the subscriber applicable.

Few things to know about SNS-

Suggested Read: AWS SQS Standard Queue vs FIFO Queue: All You Need to Know

P.S. : Most of us think of sending SMS to mobile numbers when it comes to SNS. No doubt, it does that well. however, trust me, it can do a lot more the that. As mentioned in above bullet points it lets you handover messages to all the mentioned endpoints so that you can design your architecture well.

Steps to use CloudFormation to Create SNS Topic and Subscription

Let’s see the step by step instruction to create SNS topic and subscription using CloudFormation.

Step 1: Provide proper permission

If you are not an admin user, you should explicitly provide sns:CreateTopic permission for your user/role. Additionally, you will also needs cloudformation:* as well to be able to do CloudFormation stack creation, updation etc.

Step 2: Prepare a template

You can use YAML or JSON for your template. I prefer YAML for writing my templates. But don’t worry, If you want it in JSON, I will provide JSON template as well.

To create an simple SNS standard and FIFO topic, all you need is a AWS::SNS::Topic resource like below.

  MyTopic: 
    Type: AWS::SNS::Topic
    Properties: 
      DisplayName: String
      FifoTopic: Boolean
      KmsMasterKeyId: String
      Subscription: 
        - Subscription
      TopicName: String

Few things to note there –

  • By default CloudFormation creates a standard topic for you. However, If you need a FIFO topic, you can specify the parameter FifoTopic as true.
  • If you are creating a FIFO topic, name must end with .fifo suffix.
  • You can specify DisplayName property for display name of topic in case of SMS subscription
  • KmsMasterKeyId is used for server side encryption
  • In the Subscription property you can specify list of endpoints that subscribe to this topic. This is option and you may just decide to create a topic without any subscription and that’s fine. just skip this property
  • Also, subscription here is a very basic one and if you wish you have advanced features like delivery policy, filtering, raw message delivery, and cross-region subscriptions, use the AWS::SNS::Subscription resource.

We saw how a simple SNS topic can be declared in CloudFormation. But What about FIFO Topic?

Well, Ideally the template to create a simple FIFO topic looks like below.

  MyFIFOTopic: 
    Type: AWS::SNS::Topic
    Properties: 
      TopicName: DemoFifoTopic.fifo
      FifoTopic: true

Template to Create SNS Topic and Subscription : YAML

In this template, we are creating an SNS topic and an Email subscription. After the creation of topic we are outputting topic ARN for later use.

AWSTemplateFormatVersion: '2010-09-09'
Description: Template to Create an SNS Topic and Subscriptions

Parameters:

  TopicName:
    Type: String
    Description:  Topic Name
    Default: my-topic

Resources:

 MySNSTopic: 
   Type: AWS::SNS::Topic
   Properties: 
     Subscription: 
       - Endpoint: "cloudkatha@gmail.com"
         Protocol: "email"
     TopicName: !Ref TopicName     
          
Outputs: 
  MyTopicArn: 
    Description: Arn of Created SNS Topic
    Value: !Ref MySNSTopic

Template to Create SNS Topic and Subscription JSON

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Template to Create an SNS Topic and Subscriptions",
    "Parameters": {
        "TopicName": {
            "Type": "String",
            "Description": "Topic Name",
            "Default": "my-topic"
        }
    },
    "Resources": {
        "MySNSTopic": {
            "Type": "AWS::SNS::Topic",
            "Properties": {
                "Subscription": [
                    {
                        "Endpoint": "Preeti.PragyaJha@rs-components.com",
                        "Protocol": "email"
                    }
                ],
                "TopicName": {
                    "Ref": "TopicName"
                }
            }
        }
    },
    "Outputs": {
        "MyTopicArn": {
            "Description": "Arn of Created SNS Topic",
            "Value": {
                "Ref": "MySNSTopic"
            }
        }
    }
}

Step3: Create a Stack using prepared template

Now, we know the basics and we have the template so let’s go and create the stack.

  1. Grab the YAML or JSON template from above as per your convenience.
  2. Change the parameter section to change the name of topic if you wish
  3. Save the template with .yml or .json as per the choice of template and follow below steps.
  4. Login to AWS Management Console, navigate to CloudFormation and click on Create stack
  5. Click on “Upload a template file”, upload your saved .yml  or .json file and click Next
  6. Enter the stack name and click on Next. In configuration, keep everything as default and click on Next.
  7. In the events tab of stack, you can view the status.
  8. Once stack is successfully created, you can go to SNS service and verify your topic and subscription.
  9. Also, you can check output tab of your CloudFormation stack to view Topic Arn of topic.
How to use CloudFormation to create SNS topic and subscription

Clean Up

If you are creating this SNS topic for learning purpose, don’t forget to delete your CloudFormation stack so that your topic is deleted and you don’t bear any cost by any chance.

Happy Learning !!!

Conclusion:

In this post, we learnt to use CloudFormation to Create SNS Topic and Subscription

  • As AWS provides two type of topic–Standard and FIFO, we learnt to create each of them using CloudFormation
  • We learnt that by default, topic is created as standard.
  • Once created, topic type can not be changed.
  • FIFO topic name must have .fifo in the end as suffix
  • FIFO topics are used with SQS FIFO Queues and doesn’t support any other endpoint other then SQS as of now.
  • Embedded subscription parameter supports basics and if you need advanced subscription features you can create with AWS::SNS::Subscription resource.

That’s all for today. I hope you found this post helpful. Feel free to drop your questions in comment section.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on
  • Subscribe to our newsletter to get notified each time we post new content
  • Share this post with your friends

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published. Required fields are marked *