13
votes

Is it possible to create a 'Subscription' resource in a AWS CloudFormation JSON template without creating a new AWS::SNS::Topic?

In my case, the topic is created outside of the CloudFormation script, and I would like to create some subscriptions to it, involving resources created within the script.

I.E.

   "DbfExtractQueue": {
        "Type": "AWS::SQS::Queue"
    },

    "EtlSubscription": {
        "Type": "AWS::SNS::Subscription",
        "Properties": {
            "Endpoint": { "Fn::GetAtt": ["DbfExtractQueue", "Arn"] },
            "Protocol": "sqs",
            "TopicArn": { "Ref": "EtlNotificationTopicARN" }
        }
    },

The EtlNotificationTopicARN is passed into the script and represents a SNS topic ARN.

3
This is Observer pattern 101: the Subject should not know anything about the Observers. The fact that CloudFormation doesn't support it makes me roll my eyes.giorgiosironi

3 Answers

0
votes

As you already discovered, AWS CloudFormation doesn't provide the expected AWS::SNS::Subscription resource (yet) and I'm not aware of this being possible by any other means, unfortunately - guess the rationale is that both are either managed within a template or externally, but your use case is sound and I can see no fundamental reason why this shouldn't be available (maybe they'll add it at some point, AWS is usually expanding their APIs over time to address such inconsistencies/missings).

14
votes

It is now possible to do this directly in native CloudFormation as of November 2016:

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-subscription.html

Samples from the above documentation.

YAML:

MySubscription:
  Type: AWS::SNS::Subscription
  Properties:
    Endpoint: [email protected]
    Protocol: email
    TopicArn: !Ref 'MySNSTopic'

JSON:

"MySubscription" : {
  "Type" : "AWS::SNS::Subscription",
  "Properties" : {
    "Endpoint" : "[email protected]",
    "Protocol" : "email",
    "TopicArn" : {"Ref" : "MySNSTopic"}
  }
}
3
votes

It is possible now since CloudFormation supports Custom Resource Types with Lambda functions.

I've created a gist here with CloudFormation tamplate: https://gist.github.com/martinssipenko/4d7b48a3d6a6751e7464.js