🎉 New book alert! 'From Coder To CTO' by CloudExpat founder. Level up your career - get your copy today! 🚀 X

Terraform VS CloudFormation

/images/blog/posts/terraform-vs-cloudformation-bg.png

Terraform and AWS CloudFormation are both tools that allow users to define and deploy infrastructure as code.
Here we will be comparing some key differences between Terraform and CloudFormation:

/images/blog/posts/terraform-vs-cloudformation-bg.png

  1. Language: Terraform uses its own proprietary language, HashiCorp Configuration Language (HCL), to define infrastructure as code. CloudFormation uses JSON or YAML to define infrastructure as code.
  2. Resource management: Terraform allows users to manage a wide range of resources across multiple cloud providers, not just AWS. CloudFormation is specific to AWS and can only manage AWS resources.
  3. Execution plan: Terraform creates an execution plan before making any changes to infrastructure. This plan shows the user what changes will be made, allowing them to review and approve before applying the changes. CloudFormation does not have this feature, so changes are applied immediately.
  4. State management: Terraform maintains a state file that keeps track of the infrastructure it is managing. This state file can be stored locally or in a remote backend, such as S3. CloudFormation does not have a state file and relies on the AWS Management Console to display the resources that have been created.
  5. Rollback: Terraform allows users to roll back changes using the “terraform destroy” command. CloudFormation does not have a built-in rollback feature, but users can manually delete the stack to remove all of the resources that were created.

Terraform vs CloudFormation - State Management

Terraform maintains a state file that keeps track of the infrastructure it is managing. This state file is used to compare the current infrastructure with the desired infrastructure defined in the configuration files. The state file is stored locally or in a remote backend, such as S3, and can be used to track the dependencies between resources.

CloudFormation, on the other hand, does not have a state file. Instead, it relies on the AWS Management Console to display the resources that have been created. This means that users must manually keep track of dependencies between resources, or use CloudFormation outputs and cross-stack references to link resources in different stacks.

There are some advantages and disadvantages to each approach to state management. The use of a state file allows Terraform to more easily track dependencies and perform operations like rolling back changes, but it also means that users must be careful to manage and secure the state file. CloudFormation’s reliance on the AWS Management Console means that users do not have to worry about managing a state file, but it can make it more difficult to track dependencies and perform certain operations.

In conclusion, Terraform is likely to be a better fit for general resources management, whereas when one has a need for a limited number of AWS based resources, or any need for AWS Lambda (Serverless) related infrastructure, we recommend going in the direction of using CloudFormation or a CloudFormation and Serverless Framework hybrid.

Both Terraform and CloudFormation are popular choices for infrastructure as code and have their own strengths and weaknesses. It’s critical to evaluate your needs and choose the tool that best fits your use case.