terraform
Provision and manage infrastructure as code
TLDR
Initialize a new or existing Terraform configuration
Verify that the configuration files are syntactically valid
Format configuration according to Terraform language style conventions
Generate and show an execution plan
Build or change infrastructure
Destroy Terraform-managed infrastructure
SYNOPSIS
terraform [global options] <subcommand> [args]
PARAMETERS
init
Initializes a working directory containing Terraform configuration files, downloading necessary providers and modules.
plan
Generates an execution plan, showing what actions Terraform will take (create, modify, destroy) without actually performing them.
apply
Applies the changes required to reach the desired state of the configuration, provisioning or updating infrastructure.
destroy
Destroys all Terraform-managed infrastructure specified in the configuration.
validate
Checks the configuration files for syntax errors and internal consistency.
fmt
Rewrites configuration files to a canonical format, improving readability and consistency.
output [NAME]
Extracts and displays the value of an output variable from the state file. If NAME is omitted, all outputs are shown.
state <subcommand>
Provides advanced commands for inspecting and manipulating the Terraform state.
version
Prints the Terraform version and its plugin versions.
DESCRIPTION
Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows users to define and provision datacenter infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL). Terraform supports a multitude of cloud providers (AWS, Azure, GCP, etc.) and on-premise solutions. Its core functionality revolves around managing the lifecycle of infrastructure: planning, applying, changing, and destroying resources predictably and efficiently. By treating infrastructure as code, Terraform enables version control, collaboration, and automated deployments, significantly reducing manual errors and improving operational consistency. It uses a state file to track the current state of managed resources, allowing it to determine necessary changes to reach the desired state defined in the configuration. This makes it ideal for managing complex, multi-cloud environments.
CAVEATS
State File Management: Terraform relies heavily on a state file to track managed resources. Mismanagement or corruption of this file can lead to infrastructure drift, unintended changes, or data loss. Using remote state backends (e.g., S3, Azure Blob Storage, HashiCorp Cloud) with locking is highly recommended to prevent conflicts and ensure consistency.
Infrastructure Drift: Manual changes made to infrastructure outside of Terraform's control can lead to 'drift' between the actual state and Terraform's recorded state, potentially causing unexpected behavior during subsequent plan or apply operations.
Provider Dependencies: Terraform's functionality is dependent on the features and stability of its various cloud and service providers. Some bleeding-edge features of cloud services might not be immediately available via Terraform providers.
HASHICORP CONFIGURATION LANGUAGE (HCL)
Terraform configurations are written in HCL, a declarative language designed to be both human-readable and machine-friendly. HCL allows users to define resources, data sources, variables, outputs, and modules in a structured, consistent manner across different providers.
THE <I>TERRAFORM</I> STATE FILE
The state file (typically terraform.tfstate) is a crucial component that maps real-world resources to your configuration. Terraform uses this file to understand the current state of your infrastructure, track metadata about managed resources, and plan changes to reach the desired state defined in your configuration. It's critical to manage this file carefully, especially in team environments, often by storing it remotely.
PROVIDERS AND MODULES
Providers are plugins that enable Terraform to interact with various cloud platforms (e.g., AWS, Azure, GCP) and other services (e.g., Kubernetes, Docker, DNS). They expose resources and data sources for Terraform to manage. Modules are self-contained Terraform configurations that can be reused across different projects or within the same project, promoting organization, reusability, and best practices.
HISTORY
Terraform was first publicly released by HashiCorp in July 2014. It emerged as a groundbreaking tool for provisioning and managing cloud infrastructure using a declarative approach, distinguishing itself with its vendor-agnostic nature and extensibility through a vast ecosystem of providers. Its adoption grew rapidly among DevOps and SRE teams due to its ability to manage infrastructure as code, enabling version control, collaboration, and automated deployments. Over the years, Terraform has continuously evolved, introducing key features like modules for reusability, workspaces for isolating environments, and robust remote state backends with locking, solidifying its position as a cornerstone tool for modern infrastructure automation and multi-cloud management.