Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp. It enables users to define and provision infrastructure using declarative configuration files. With Terraform, you can manage various resources (like virtual machines, storage, networking, etc.) across a variety of cloud platforms (such as AWS, Azure, Google Cloud, etc.) as well as on-premises solutions.
Declarative Language: Terraform uses its own domain-specific language (HCL - HashiCorp Configuration Language) to define infrastructure, where you declare what you want, and Terraform figures out how to achieve it.
Multi-cloud Support: It allows you to manage infrastructure across multiple providers (public and private clouds) in a unified way.
Plan and Apply: Before applying changes, Terraform creates an execution plan to preview what it will do. This ensures safety and reduces the risk of unintended changes.
State Management: Terraform maintains the state of your infrastructure in a state file. This is crucial because Terraform compares the desired state (in your configuration files) with the actual state of the infrastructure to determine the necessary actions.
Modular: You can break down your infrastructure into reusable modules, making your code more manageable, reusable, and easier to collaborate on.
Basic Workflow
- Write: Define your infrastructure using configuration files (
.tf
files). - Plan: Run
terraform plan
to see what changes will be made to achieve the desired state. - Apply: Run
terraform apply
to implement the changes and provision the resources. - Destroy: Run
terraform destroy
to remove all resources that were created.
Terraform:
- Supports multiple cloud providers (AWS, Azure, Google Cloud, etc.) and third-party services.
- Uses HCL (HashiCorp Configuration Language), offers strong modularity and reusable modules.
- Requires managing a state file for tracking infrastructure.
- Excellent for multi-cloud and hybrid environments.
- Larger community and more diverse ecosystem.
CloudFormation:
- AWS-only tool, tightly integrated with AWS services.
- Uses JSON/YAML, no need to manage a state file (AWS handles it).
- Supports change sets and rollback for safer deployments.
- Good for AWS-native setups with deep integration.
- Less flexible, but perfect for users fully within AWS.
In short: Terraform is ideal for multi-cloud setups and flexibility, while CloudFormation is best for AWS-centric environments and ease of AWS management.