Skip to content

CloudFormation vs Terraform: which Infrastructure as Code tool to choose?

Objective comparison between AWS CloudFormation and HashiCorp Terraform for managing your cloud infrastructure as code.

Updated on 2 July 2024

Two approaches to managing your infrastructure as code

Infrastructure as Code (IaC) lets you define and manage your cloud infrastructure in versioned configuration files. AWS CloudFormation and HashiCorp Terraform are the two most widely used tools for this task. CloudFormation is native to AWS and free. Terraform is open source and multi-cloud. The choice depends on your context: single-cloud or multi-cloud, team size and existing ecosystem.

CloudFormation: the native AWS tool

CloudFormation is the IaC service built into AWS. You define your resources in a YAML or JSON template and CloudFormation creates, updates and deletes them in an orderly manner. The native integration with AWS means that new services are supported as soon as they launch, often before Terraform.

CloudFormation automatically manages dependencies between resources. If an RDS database depends on a VPC, CloudFormation creates the VPC first. In case of an error, CloudFormation performs an automatic rollback that restores the previous state. This safety is valuable in production.

The CDK (Cloud Development Kit) lets you write CloudFormation templates in a programming language (TypeScript, Python, Java, Go) instead of YAML. The CDK generates the CloudFormation template from your code, which offers the power of a programming language with the reliability of CloudFormation.

Terraform: the multi-cloud tool

Terraform uses HCL (HashiCorp Configuration Language) to define infrastructure. Its main advantage is multi-cloud support: you manage AWS, Azure, GCP and hundreds of other providers in the same configuration. If your company uses several clouds, Terraform unifies the management.

Terraform’s state file records the current state of your infrastructure. Terraform compares this state with your configuration to determine the changes to apply. The execution plan (terraform plan) shows the changes before applying them, which reduces risk.

The Terraform module ecosystem is rich. The Terraform Registry offers thousands of reusable modules that encapsulate best practices for common architectures. You compose your infrastructure from proven modules rather than writing everything from scratch.

Point-by-point comparison

In terms of AWS support, CloudFormation has the advantage of immediate coverage of new services. Terraform generally catches up within a few weeks. For state management, CloudFormation manages state on the AWS side (no file to manage), while Terraform requires a remote backend (S3 + DynamoDB) for team work.

For rollback, CloudFormation performs an automatic rollback in case of an error. Terraform does not roll back automatically but lets you revert to a previous state manually. For modularity, Terraform excels with its reusable modules. CloudFormation offers nested stacks and modules, but the ecosystem is less rich.

Our recommendation

For an SMB that is 100% AWS, CloudFormation (or the CDK) is the simplest choice. The native integration, automatic rollback and absence of a state file to manage reduce operational complexity. For a multi-cloud company or one with an experienced DevOps team, Terraform offers more flexibility.

LCMH uses CloudFormation and CDK to manage its clients’ AWS infrastructure.

To understand cloud architecture principles, read our article on the Well-Architected Framework.


Sources

  1. AWS, AWS CloudFormation. aws.amazon.com/cloudformation
  2. HashiCorp, Terraform AWS Provider. registry.terraform.io/providers/hashicorp/aws
  3. AWS, AWS CDK. aws.amazon.com/cdk

Frequently asked questions

Is CloudFormation free?
Yes, CloudFormation is free. You only pay for the AWS resources created by your stacks. Terraform is also free in its open source version. Terraform Cloud (the managed version) is paid starting at $20 per user per month.
Can you use Terraform with AWS?
Yes, Terraform supports AWS through the AWS provider, one of the most mature and complete in the Terraform ecosystem. Nearly all AWS services are supported. Terraform can also manage multi-cloud resources (AWS + Azure + GCP) in the same configuration.
Which one is easier to learn?
CloudFormation uses YAML or JSON, formats familiar to most developers. Terraform uses HCL (HashiCorp Configuration Language), a dedicated language that takes a few days to learn. Both have a moderate learning curve.

Related Articles