CloudFormation vs Terraform: My Experience With Both

Amroj Sandhu
thecloudbee
Published in
3 min readJan 11, 2021

Infra-as-code is the backbone for any cloud. But, why should one look beyond CloudFormation Templates?

Why Infrastructure as Code?

Infra-as-code implies we are provisioning infrastructure by writing a code. As in the case of AWS CloudFormation, we input .yaml files and AWS resources are generated.

We are inclined towards using infra as code due to two reasons.

  1. Speed — Quick and Consistent changes across different environments.
  2. Accountability — Who changed and what changed?

Terraform was impressive.

I have been writing CloudFormation Templates (CFTs) for 4 years. But once I wrote my first Terraform, I never looked back. The first impression of terraform was.

  • Less code
  • Environments made easy
  • Terraform makes the changes more visual

Terraform means Manageable Code

Terraform understands the Abstraction. One can build a database module separated from the functions module. On the other hand, doing this through CFT means one CFTs for database and another one for functions.

Folder structure in Terraform for abstraction between data and app layer

The Terraform modules provide a light-weight abstraction. We can leverage this abstraction to make our code understandable and easy to navigate. As in the above example we have different modules for app and data.

Environments made easy in Terraform

Let’s say you have a CFT that you have tested on the lab environment. Further, it is desired to run the same CFT on the staging environment. One can manage environments in CFT as follows.

Now, switching between the argument in FindInMap, one can get different flavors.

Comparing the above scenario with Terraform. We can leverage Terraform variables to reuse the code for different deployment plans. The terraform init command creates a working directory with the Terraform Configuration Files.

Folder structure in Terraform for easy environment toggling

The above folder structure in Terraform makes it easy to switch between the lab and staging flavours. Example is the following command.

Terraform makes the changes more visual

Both CloudFormation and Terraform have state management in-built. Both of these create a changeset before deployment and one can validate before deploying.

This is how the AWS CloudFormation changeset looks like when you create a change over an existing CFT.

Not much of a changeset in AWS CloudFormation

Now compare the above with the extra details provided by terraform when we are over-writing a change.

Terraform pin points what is changing in the deployment

The above snippet shows the details Terraform plan provides in comparison to AWS CloudFormation. We can make out that only the tags are changing for the Instance in Terraform. But in AWS CloudFormation no details are provided — only the resources that are changing are listed.

Conclusion

Terraform is more flexible and easy to learn. So, if you’re starting the journey of infra-as-code, Terraform is the answer.

This is not a full list of comparisons between AWS CloudFormation and Terraform, but a part of that.

Originally published at https://www.thecloudbee.blog on January 11, 2021.

--

--