A Lambda function which implements a Custom Resource for Cloud Formation that
creates a SNS subscription to an already existing SNS topic. This is useful if you create the
AWS::SNS::Topic
and subscriptions in different cloudformation stacks, ie using stackDependency
.
For example if you want to achieve the following:
- One service producer is reading from Kinesis and publishing some events to a SNS topic.
- Another service consumer wants to consume these events.
- Each service is deployed using its own cloudformation stack.
- The producer should not be aware of the consumers.
Solution:
- Create the
AWS::SNS::Topic
in the producer stack - Use a
Custom::stackDependency
andCustom::SnsSubscription
in the consumer stack
Create a Role with ./create-role.sh
. This creates a new stack with the
appropriate permissions for the function.
Deploy the lambda function with ./deploy-lambda.sh
.
Use the function inside your Cloud Formation template by declaring a custom
resource, Custom::SnsSubscription
. It takes the same parameters as the
SNS.subscribe function:
- TopicArn
- Protocol
- Endpoint
The only output is the SubscriptionArn
which can be referred with Fn:GetAtt
,
like this "Fn::GetAtt": ["MySubscription", "SubscriptionArn"]
.
See test-stack.template
for example usage.