My path through starting with AWS CloudFormation was a somewhat rocky path. Often I wished for simple CF Templates which would only show one pattern at a time. So I’m starting a short series where I will try to describe some patterns I experienced and hopefully lower the entry barrier for CloudFormation since it is great tool.
Scenario:
Raise an S3-Object-Create event
Publish that event to SNS
Subscribe to that event with a Lambda
As picture this would look like this:
I use serverless as project template, since this by far the easiest starting Point for any serverless work.
So here is my serverless YAML explained step-by-step (whole YAML is attached at the bottom).
I left the header unchanged as it was created by the serverless framework.
First I created a resources section. In this section I can later define my CloudFormation resources.
Now I created an SNS topic first, since this object doesn’t have any dependencies on others.
After that, I can create an S3 bucket which will send notifications out to SNS.
To grant S3 access to publish events to SNS I created the following TopicPolicy.
To subscribe to a topic, I needed to create an subscription.
By default, SNS subscriptions aren’t allowed to invoke any Lambda function. To fix this, I created a LambdaPermission, which grants SNS access to Lambda.