1

I wrote a cloudformation template with this resource to create an SNS topic.

"mySNS" : {
   "Type" : "AWS::SNS::Topic",
   "Properties" : {
      "Subscription" : [
         { "Endpoint" : { "Fn::GetAtt": [ "myLambda", "Arn" ] }, "Protocol" : "lambda" }
      ],
      "TopicName" : "mySNS"
   }
}

Is there a way to publish to this SNS topic from within the template? I can't find anything in CloudFormation template reference http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html.

Rakesh Ranjan
  • 91
  • 1
  • 10

1 Answers1

3

Short Answer: no

Long Answer / Workaround: AWS CloudFormation is more about defining resources in AWS than actually using them. However, if publishing in this topic is important for your provisioning process I would suggest you create a lambda-backed custom resource. It would be quite simple for the lambda to then publish to the topic.

If you still need help to get it done I can update my answer with a short example

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
Laurent Jalbert Simard
  • 5,949
  • 1
  • 28
  • 36
  • I added a custom resource to invoke lambda function. But cloudformation stack is stuck in that custom resource creation step since an hour. It's probably expecting a response from lambda function. Can you help with a lambda function snippet(in python hopefully) to respond to CFT? – Rakesh Ranjan Sep 20 '17 at 17:27
  • 1
    You're right, CloudFormation expects to be called back by the lambda, even in case of failure. I would highly recommend you use the cfn-wrapper-python library to handle this task for you: https://github.com/ryansb/cfn-wrapper-python – Laurent Jalbert Simard Sep 20 '17 at 17:51
  • the wrapper helps but you can still get stuck if sg or nacls are misconfigured, a dirty hack to get the stack complete is to go to the logs, where you print the event variable everytime, the callback url is there and if the correct json success message is sent to that url the stack will complete or rollback. – OmegaOdie Jun 10 '20 at 19:31
  • @OmegaOdie Yes that's a great way to get unstuck. Also there's new library officially supported by AWS that makes it harder to get stuck (yet not impossible) check it out: https://github.com/aws-cloudformation/custom-resource-helper. – Laurent Jalbert Simard Jun 12 '20 at 15:05