Skip to content

What is Terraform

Key idea:

Terraform — Infrastructure as Code tool from HashiCorp (2014). You declare desired state of cloud resources in HCL (HashiCorp Configuration Language), terraform apply creates/updates. Supports AWS, GCP, Azure, Yandex Cloud, Cloudflare, GitHub — via 3000+ providers. August 2023: HashiCorp changed license from MPL to BSL (Business Source License) → community fork **OpenTofu** (Linux Foundation, 100% compatible).

Below: details, example, related terms, FAQ.

Check your site →

Details

  • HCL: JSON-like syntax for resource definitions
  • State file: tracks real resources (local or remote S3 + DynamoDB lock)
  • Providers: AWS, GCP, Cloudflare, GitHub, Vault, Helm — via terraform registry
  • Modules: reusable chunks (VPC module, K8s cluster module)
  • OpenTofu: fork after BSL — compatible, Linux Foundation

Example

# AWS EC2 instance
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.small"
  tags = { Name = "WebServer" }
}

# Remote state in S3
terraform {
  backend "s3" {
    bucket = "tfstate"
    key    = "prod/terraform.tfstate"
    region = "us-east-1"
  }
}

Related Terms

TL;DR

Terraform is an open-source tool that enables Infrastructure as Code (IaC), allowing users to define and provision data center infrastructure using a declarative configuration language. It supports various cloud providers like AWS, Azure, and Google Cloud, making it a versatile choice for managing infrastructure at scale.

Understanding Terraform: Definition and Key Concepts

Terraform is a powerful tool developed by HashiCorp that enables Infrastructure as Code (IaC). IaC is a modern approach to managing and provisioning infrastructure through code, rather than manual processes. This allows for greater automation, consistency, and version control in infrastructure management.

Terraform uses a declarative configuration language known as HashiCorp Configuration Language (HCL). HCL allows users to define the desired state of their infrastructure, which Terraform then reconciles with the actual state. This approach provides a clear and concise way to manage resources across various platforms.

Key concepts in Terraform include:

  • Providers: These are plugins that allow Terraform to interact with cloud providers, SaaS providers, and other APIs. Popular providers include AWS, Azure, and Google Cloud.
  • Resources: These are the components of your infrastructure, such as virtual machines, storage accounts, and networking configurations. Each resource is described in the configuration file.
  • Modules: Modules are reusable configurations that can encapsulate resources, making it easier to manage complex infrastructures by breaking them down into smaller, manageable pieces.
  • State Management: Terraform maintains a state file that records the current state of the infrastructure. This state file is crucial for planning and applying changes.

By utilizing these concepts, Terraform enables teams to collaborate effectively on infrastructure projects, reducing the risk of inconsistencies and errors.

Practical Example: Provisioning AWS Resources with Terraform

To illustrate how Terraform can be used to provision infrastructure, here’s a practical example of creating an AWS EC2 instance. This example assumes you have Terraform installed and configured with appropriate AWS credentials.

First, create a new directory for your Terraform project and navigate into it:

mkdir my-terraform-project && cd my-terraform-project

Next, create a file named main.tf and add the following configuration:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe01e"
  instance_type = "t2.micro"

  tags = {
    Name = "MyExampleInstance"
  }
}

In this configuration:

  • The provider block specifies that we are using AWS and the region is set to us-west-2.
  • The resource block defines an EC2 instance using a specific Amazon Machine Image (AMI) and instance type. The specified AMI is for a basic Amazon Linux 2 instance.
  • Tags are added for better management and identification of the instance.

Once your configuration is complete, you can initialize Terraform and apply your configuration:

terraform init
terraform apply

After running these commands, Terraform will prompt you to confirm the creation of the resources. Type yes to proceed. Terraform will then create the EC2 instance as defined in your configuration.

To verify that your instance has been created, you can log into the AWS Management Console and navigate to the EC2 dashboard. You should see your instance listed there. To clean up and remove the resources, you can run:

terraform destroy

This command will remove all resources defined in your configuration, demonstrating the power of Terraform for managing infrastructure efficiently.

Learn more

Frequently Asked Questions

Terraform or OpenTofu?

OpenTofu: free, MPL license, Linux Foundation. Terraform: BSL (non-commercial ok, commercial vendors restricted). For most teams — OpenTofu safer long-term.

State management?

Remote backend mandatory for teams: S3 + DynamoDB (lock), Terraform Cloud, or gitlab-managed-terraform-state. Local state = race conditions.

Terraform vs Pulumi?

Pulumi — IaC in real programming languages (TS, Python, Go). Terraform — HCL DSL. Pulumi better for developers, Terraform for sysadmins.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.