5
votes

Is it possible to run a CloudFormation stack whenever I get a specific SNS notification. Any suggestions how to achieve this scenario.

Whenever I get a specific SNS notification, a Lambda function should be triggered which will then launch a CloudFormation stack.

1

1 Answers

11
votes

As you can access the AWS API from within AWS Lambda that's no problem at all. If you're using Python that could look like:

import boto3
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
    StackName='your-stack',
    TemplateURL='https://s3.amazonaws.com/your-bucket/your-template'
)

Of course lots of additional parameters are supported as well.

There is one big caveat: The code above will create a stack, but will not track if the stack creation succeeds. While you can get that information via the describe_stacks call, you can't rely on having a finished stack within that instance of the AWS Lambda function, as the maximum runtime of the AWS Lambda function is 15 minutes, but the CloudFormation stack creation might take longer than that.

If you don't care if the stack creation succeeded you should be good, otherwise I suggest you write the stack id, returned by the create_stack call, to a persistent storage (e.g. DynamoDB) and have a separate scheduled AWS Lambda function which checks the status of the CloudFormation stacks stored in DynamoDB and handles the possible stack creation outcomes.