Rock your CloudFormation with troposphere and boto

So you’re using AWS CloudFormation in order to bring up complex infrastructures; haven’t you already told yourself that instead of writing down all those JSON lines by hand, you could bring more fun to your architect life? I did, and I found a way to programmatically design a whole architecture using troposphere and boto3. Simply put, troposphere gives you bindings in order to generate CloudFormation’s JSON template, but hey, it’s python, meaning that you can create loops, use conditions and even dynamically build objects.

Let me give a simple example on how this will change your Cloud builder life. Let’s assume you’d like to create a couple of defaults SecurityGroups, we’ll create a convenient dict that will hold needed informations:

Note that you could use any structure type you like, I just found this one to be practical. Now, dynamically build all the associated SecurityGroups:

Now, to bring a little bit more of awesomeness, consider adding the power of Amazon’s own python module, boto3. Haven’t you struggled by writing AMI mapping directly on the JSON file? Well consider the following function:

And later on your JSON generator, using troposphere wrappers:

Troposphere website has an example section that contains a lot of useful examples in order to manipulate all well-known AWS products.

HTH!