Cloudformation and/or boto3

Hello,

We are running into a situation in the department where we are trying to decide what strategy to pursue when deploying resources (such as EC2 servers, VPCs, RDS, etc.) on AWS.

I know we can use cloudformation templates as well as boto3. However, I want to read some of your thoughts/opinions on this (would be great if you can base it on your experience):

  • Is there any pro/cons of using cloudformation?
  • Is there any pro/cons of using boto3?
  • Can both these technologies be used 100% interchangeably or is there somethings cloudformation (or boto3) can’t do?

I know people will mention terraform instead of using cloudformation but due to some issues, we decided not to pursue terraform so terraform is a no-go for us.


Related discussions:

I’ve limited experience with both, but one thing I would really call out is the ability in cloudformation to set up dependencies - i.e. dont do THIS until our NAT Gateway/IGW is online. There are also CloudFormation Signals, which are a really powerful thing and let you generate custom ready criteria

I use cloudformation, and prefer it over boto3 natively. Cloudformation resolves resource dependencies for you, and provides update and rollback mechanisms. You can also create custom resources to work around any shortcomings you find.

I do, however, use troposphere to build my templates. I like being able to create libraries, and use programming constructs (functions, loops, constants, etc.) to streamline the templates.

boto3 is useful to drive cloudformation to manage changesets, and potentially orchestrate multiple templates builds, as part of a larger application build out. There are also tools like stacker and sceptre that help with the orchestration problem, too.

Can both these technologies be used 100% interchangeably

You probably can use them interchangably, but you shouldn’t. Cloudformation is an infrastructure-as-code tool. Boto3 is a Python wrapper for an API.

If your goal was to build a VPC and some subnets and some SGs and stuff, that’s Cloudformation.

If your goal was to write a script that queries some tags or cleans up some snapshots or something, that’s Boto3. So while you can build resources with Boto3, you probably shouldn’t. And if you do go down this road, you’re basically reinventing Terraform (but in Python).

I can’t speak to boto3 very much, but I can give you my input on CloudFormation as we’re using it quite heavily in various environments.

We setup CloudFormation in such a way to isolate resources into separate templates. i.e. VPC, IGW, subnets, ACL’s and routes/route tables into one template. EC2 and related resources into another template and so on and so forth for any other resources being deployed. This makes for easier management of resources as you only need to modify smaller templates rather than one master template.

We then use a private GitHub repository for storing and version control over the templates, and a dedicated S3 bucket for all CloudFormation templates. You could use CodeCommit and the other AWS services on offer if you’d prefer, but I haven’t had too much experience with that.

I hope this info can help point you in the right direction when it comes to making your final decision on how to proceed.

Cloudformation is the most declarative way to go, which brings many benefits. But building the templates themselves can be a challenge.

IMO the trick is to figure out how to use a minimal amount Python/Boto3 to build CloudFormation templates and to drive your automation workflow with it.

Using Python/Boto3 with Troposphere is great, if you really know what you are doing. See [Caktus/aws-web-stacks] (https://github.com/caktus/aws-web-stacks) for inspiration.

What I have found is that awslabs has some great Reference Architectures (RA) that you can use for as a starting point. Typically, these RA’s are provided as flat Cloudformation templates. The challenge you then face is: do I convert this to Troposphere or try to use it as is?

I think it is best to leave the RA templates as they are, because they are a moving target. Those templates will be updated by AWS experts over time, and you want to leverage that. So, here is where Python/Boto3 comes into play. It takes very little effort to build a python tool that can treat the RA templates as Jinja2 templates that you tweak in an automated manner and deploy to Cloudformation. Yes, you will make some minor edits to the original RA templates, but nothing major. When changes happen upstream, you should be able to pull and merge those fairly easily.

Anyhow thats how I view the problem space, good luck!

CF’s main advantage over other approaches is the built-in rollback when problems occur. I really like the sound of that.

Terraform

I have no clue if I’m doing things remotely right, but I use both – I have cloudformation templates that are deployed by my boto3 script. Every “stack” that we run is actually two CF stacks – one stack for persistent resources (VPC, S3 buckets, RDS, ElastiCache, SNS Topics, ELBs, etc), and one stack for temporary resources (EC2s). The temporary stacks ingest exports from the persistent stack for configuration.

My team’s CI and deployment strategy, then, works such that each build artifact contains a copy of the cloudformation templates associated with that build, and the artifact is shipped to S3 upon build completion. When a new build completes, a python/boto3 script is invoked that creates a new “temporary” stack based on the CF template in the artifact. Upon success, it points the persistent stack’s ELBs at the new temporary stack, deleting the old (so, we essentially have blue/green deployment going on).

Using Python/Boto3 with Troposphere is great, if you really know what you are doing

I use stacker to achieve exactly that, and it’s remarkably better than raw templates (although they have improved quite a lot with YAML support).

Why not both?

I use CloudFormation for most of the base resource setup, but then use Ansible + boto3 and all of Ansible’s deep set of aws_* modules to work with resources, perform updates, etc.

CloudFormation is best at managing instance and resource creation, instance size changes, certain kinds of deployments, etc.—however, it’s pretty weak when it comes to configuration management for EC2 servers, and general ops work (e.g. patching servers, performing a rolling patch update, etc.).

I try to get as much as I can in CF templates… but when they start getting too hefty and I find myself writing an giant bash script in UserData… that’s when I switch that set of resources to a hybrid model.

Stacker is a great tool - we use it currently as well.

Terraform. For real tho.

As a former aws employee… Terraform.

TF or CloudFormation, boto3 is technically capable of what you want here but it’s not the right tool for the job.

Both, but not in the way you think. I deploy all of my CloudFormation templates using either CodePipeline or simple boto3 deployments via StackSets depending on the task. I create a yaml configuration file for a particular application and then abstract the CloudFormation templates away using Troposphere. The amount of power you have to deploy your resources anywhere and keep their states consistent with this method is amazing.

Like others have mentioned, Terraform is good too, especially if you want to get some infrastructure up and running quickly and move on to other tasks. If you’re into all the fine details of full stack development for an application though, you should look into doing things the way I described.

Check out serverless as well. It has a great scripting front end to cloud formation, and makes the development process much easier if you’re building lambada etc.

Definitely Terraform. Worked with Cloudformation for a bit and it just felt clunky and took too much effort to jump back and forth in the templates to work with resource relationships. It isn’t modular in any useful terms, and good luck loosely coupling with other parts of your infrastructure. I shouldn’t have to write a lambda to do something lookup a subnet by tags.

edit: fixed brain to finger packet loss

Until a stack hangs for 1+ hour :frowning:

What’s that?

From a learning perspective…I would like to ask that If i learn CloudFormation and Terraform…is there need to learn boto3 then ?