How to create SNS Topic and Subscription using CloudFormation

This post provides example cloudformation template to create SNS Topic, SNS Topic Subscription and SNS Topic Policy using yaml template.

See Also

SNS Topic using CloudFormation

Follow below steps to create and deploy the CloudFormation template, this article uses vscode to create yaml template, you can use text editor of your choice in case vscode is not available.

Step 1: Create directory with name cft-tutorials and open it in vscode.

Step 2: Create a file sample_cft.yaml inside cft-tutorials.

Step 3: Copy the below YAML template in sample_cft.yaml. Below CloudFormation template create a SNS topic with name SampleTopic.

   
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: "SampleTopic"
      DisplayName: "SampleTopic"
      KmsMasterKeyId: "alias/aws/sns"
      Tags:
        - Key: "Name"
          Value: "SampleTopic"
        - Key: "Purpose"
          Value: "Testing"
   

SNS Subscription using CloudFormation

Step 4: Update the CloudFormation as shown below to create an email subscription for SampleTopic. Replace the sample@example.com with the proper mail id.

   
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: "SampleTopic"
      DisplayName: "SampleTopic"
      KmsMasterKeyId: "alias/aws/sns"
      Tags:
        - Key: "Name"
          Value: "SampleTopic"
        - Key: "Purpose"
          Value: "Testing"
  MySubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Endpoint: sample@example.com
      Protocol: email
      TopicArn: !Ref 'MySNSTopic'
       

SNS Topic Policy for S3 events using CFT

Step 5: Update the CloudFormation as shown below to create a SNS topic policy. This SNS topic policy allows S3 event notifications to publish to a SNS topic.

   
AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MySNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: "SampleTopic"
      DisplayName: "SampleTopic"
      KmsMasterKeyId: "alias/aws/sns"
      Tags:
        - Key: "Name"
          Value: "SampleTopic"
        - Key: "Purpose"
          Value: "Testing"
  MySubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Endpoint: sample@example.com
      Protocol: email
      TopicArn: !Ref 'MySNSTopic'
  MyTopicPolicy:
    Type: AWS::SNS::TopicPolicy
    Properties:
      PolicyDocument:
        Statement:
          - Sid: AllowS3
            Effect: Allow
            Principal:
              Service:
                - s3.amazonaws.com                
            Action: 'sns:Publish'
            Resource:
              - !Ref MySNSTopic
      Topics:
        - !Ref MySNSTopic
       

Deploy stack to create resources

Step 6: Open the AWS CloudFormation directly with the URL https://console.aws.amazon.com/cloudformation/ .

Step 7: Navigate to Stacks, Click on Create stack and click on With new resources (standard).

Step 8: Select Template is ready . Select Upload a template file. Click on Choose file to select sample_cft.yaml from cft-tutorials directory and click on Next.

cft-iam-role

Step 9: Enter Stack name and click on Next.

cft-iam-role

Step 10: In "Configure stack options" page click on Next.

Step 11: Click on Create stack.

Step 12: Check Stack Events section, on completion you should see CREATE_COMPLETE for the stack.

cft-iam-role


Category: AWS