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).
Is it possible to create a SNS subscription with CloudFormation without creating a topic?
-
27-06-2022 - |
Question
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.
Solution 3
OTHER TIPS
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: test@email.com
Protocol: email
TopicArn: !Ref 'MySNSTopic'
JSON:
"MySubscription" : {
"Type" : "AWS::SNS::Subscription",
"Properties" : {
"Endpoint" : "test@email.com",
"Protocol" : "email",
"TopicArn" : {"Ref" : "MySNSTopic"}
}
}
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