Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS SNS Topic Policy Cloudformation

Trying to create an SNS topic using cloud formation script. It all works fine, except the topic policy.

This is what we get by default,

enter image description here

I want to update the policy as below using cloud formation script.

enter image description here Any suggestions on how to achieve this?

like image 380
User0911 Avatar asked Jul 10 '17 19:07

User0911


People also ask

What is an SNS topic policy?

Ensures SNS topics do not allow global send or subscribe. SNS policies should not be configured to allow any AWS user to subscribe or send messages. This could result in data leakage or financial DDoS.

How do you get SNS topic Arn in CloudFormation?

In your CloudFormation template, if you simply reference your SNS topic, then you'll get the ARN. So you can use that as input to your nested CloudFormation template.


1 Answers

As was pointed out in one of the comments, you don't want to use AWS:* as a principal since it grants anyone with an AWS account access.

To create a SNS topic, and restrict access to certain services, or anyone in the account, use the following example.

The "AllowServices" SID show how to add multiple services, while the AllowAWS allows anything in the account to access it.

---
AWSTemplateFormatVersion: '2010-09-09'

Parameters:
  Email:
    Type: String
    Default: <your name here>

Resources:
  Topic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: TestTopic
      Subscription:
      - Endpoint: !Ref Email
        Protocol: email

  TopicPolicy:
    Type: AWS::SNS::TopicPolicy
    Properties:
      PolicyDocument:
        Statement:
          - Sid: AllowServices
            Effect: Allow
            Principal:
              Service:
                - events.amazonaws.com
                - cloudwatch.amazonaws.com
            Action: 'sns:Publish'
            Resource:
              - !Ref Topic
          - Sid: AllowAWS
            Effect: Allow
            Principal:
              AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
            Action: 'sns:Publish'
            Resource:
              - !Ref Topic
      Topics:
        - !Ref Topic
like image 52
chris Avatar answered Sep 22 '22 00:09

chris