keepcalculator

06 Numbers and Powers

Terraform Infrastructure as Code Explained

Card No. 06·72327 · Updated · Standard formula, shown below

Terraform is an infrastructure-as-code (IaC) tool that lets you define cloud and on-premises resources in human-readable configuration files, then provision and manage them consistently. You write what your infrastructure should look like, and Terraform figures out the steps to create, change, or destroy resources to match that desired state.

Why Use Terraform for IaC?

Traditional infrastructure management involves clicking through web consoles or running one-off scripts. Terraform replaces that with declarative configuration files – you describe the end state, not the steps. Benefits include:

  • Version control: Your infrastructure lives in Git, so changes are reviewable and auditable.
  • Automation: Apply the same configuration to staging and production without manual drift.
  • Collaboration: Teams can propose changes via pull requests before they touch real resources.
  • Reproducibility: Spin up an entire environment in minutes, then tear it down cleanly.

How Terraform Works

Terraform uses a core workflow: write, plan, apply. You write configuration in HashiCorp Configuration Language (HCL) or JSON. Then you run terraform plan to see what actions will be taken – it compares your desired state with the current state stored in a state file. Finally, terraform apply executes the plan.

Providers are plugins that translate Terraform’s generic resource types into API calls for specific platforms like AWS, Azure, Google Cloud, or Kubernetes. For example, an AWS EC2 instance is declared as aws_instance with attributes like ami and instance_type.

The state file (terraform.tfstate) tracks resource metadata. It’s crucial to store it remotely (e.g., in an S3 bucket with DynamoDB locking) so multiple team members don’t overwrite each other’s changes.

Common Problems and Misconceptions

A frequent misconception is that Terraform is only for cloud servers. In reality, it also manages databases, DNS records, load balancers, IAM roles, and even SaaS resources via community providers. Another misconception: Terraform is not a configuration management tool like Ansible or Chef. It provisions infrastructure, but it doesn’t install software or manage files on existing servers – those tasks are better left to tools like Ansible or cloud-init.

Common problems include:

  • State file conflicts – solved with remote state and locking.
  • Drift – when someone changes resources outside Terraform; use terraform plan to detect it.
  • Secrets in plain text – never store passwords in configuration; use variables and a secret manager.

How to Choose Between Terraform and Alternatives

Alternatives include AWS CloudFormation, Azure Resource Manager (ARM), Google Deployment Manager, Pulumi, and Ansible. Terraform stands out because it is cloud-agnostic – you can manage multi-cloud or hybrid environments with one tool. CloudFormation is deeply integrated with AWS but locks you into that ecosystem. Pulumi lets you write infrastructure in general-purpose languages like Python or TypeScript, which some teams prefer over HCL.

Choose Terraform if you need multi-cloud support, a large provider ecosystem, or a mature community. Choose a cloud-native tool if you are all-in on one cloud and want tighter integration with that cloud’s features.

Costs and Safety Considerations

Terraform itself is open-source and free. Paid tiers (Terraform Cloud) add collaboration features like remote state, policy enforcement, and run history. The real cost is the cloud resources you provision – Terraform can help control costs by making it easy to destroy temporary environments.

Safety is critical: terraform destroy removes everything in the configuration. Always review the plan output carefully, especially before destroying resources. Use terraform plan in CI/CD to catch mistakes, and enable state locking to prevent concurrent modifications.

Frequently Asked Questions

Is Terraform hard to learn?

It has a learning curve, but the basics are approachable. Start with a simple provider like AWS, follow the official tutorials, and you can create your first EC2 instance within an hour. The HCL syntax is deliberately simple – it reads like a structured list of resources and attributes.

What is the difference between Terraform and Ansible?

Terraform is infrastructure provisioning – it creates and manages the underlying resources (servers, networks, databases). Ansible is configuration management – it connects to existing servers and ensures software is installed and configured. Many teams use both: Terraform to build, Ansible to configure.

Can Terraform manage Kubernetes?

Yes. The Kubernetes provider lets you define namespaces, deployments, services, and other resources. You can also use Terraform to provision a cluster (e.g., EKS, GKE) and then manage workloads on it, though some prefer Helm for application-level deployments.

What does “state” mean in Terraform?

State is Terraform’s record of what resources it manages, mapping configuration to real-world IDs. It’s stored in a JSON file. Without state, Terraform can’t know what to update or delete. Storing state remotely is essential for team use.

Is Terraform only for cloud?

No. It works with on-premises tools like VMware vSphere, OpenStack, and even physical hardware via providers like libvirt. It’s cloud-agnostic, so the same workflow applies to any platform with a provider.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →