Infrastructure as Code has become the standard way to provision and manage cloud and on-premises resources. Terraform, the open-source Infrastructure as Code tool developed by HashiCorp, enables definition, provisioning, and management of infrastructure resources across various cloud providers and on-premises environments in a declarative manner. The configuration files define the resources, their properties, and the relationships between them. This allows infrastructure to be treated as code, providing version control, collaboration, and repeatability benefits.
Terraform supports a wide range of cloud providers, including Amazon Web Services, Microsoft Azure, Google Cloud Platform, and many others. It also supports managing resources in on-premises environments and various other services such as DNS providers, content delivery networks, and databases.
The effectiveness of Terraform comes from its structured workflow that ensures a seamless creation, modification, and provisioning process for managing infrastructure more easily, quickly, and reliably. Whether working as a solo practitioner or part of a larger team, understanding the Terraform Workflow process is key to achieving efficient, scalable, and reproducible infrastructure management automation within an overall DevOps process.
Core Three Stage Workflow
The core Terraform workflow has three steps:
- Write - Author infrastructure as code
- Plan - Preview changes before applying
- Apply - Provision reproducible infrastructure
This provides a structured process that ensures a seamless creation, modification, and provisioning process for managing infrastructure more easily, quickly, and reliably.
The short TLDR description of the three key stages is:
- Write – Develop infrastructure as code configuration to define the infrastructure to be provisioned and managed
- Plan – Preview the modifications necessary before implementation as a means to verify changes before applying them to the infrastructure environment
- Apply – Reliably and scalably apply infrastructure changes in an automated fashion to ensure continuity across environments
The process is iterative. As new infrastructure needs to be provisioned and reconfigured, the necessary Terraform code changes will need to be made to the Terraform project by repeating the Write stage. Then the Plan and Apply stages will be repeated to perform the necessary provisioning and management process to manage those resources.
Write
The first stage in the Terraform Workflow process is to Write Terraform code. The workflow starts with defining infrastructure in .tf files.
Terraform configuration is written using HashiCorp Configuration Language, HCL. You write Terraform configuration just like you write code: in your editor of choice.
A typical resource declaration is declarative:
hcl
resource "aws_instance" "web" {
ami = "ami-xxxxxxxx"
instance_type = "t2.micro"
tags = {
Name = "web-server"
}
}
Here, infrastructure is described declaratively instead of manually creating resources from the console.
Define Infrastructure is the first practical step. Create a directory for your Terraform project and define your infrastructure using Terraform configuration files. These files describe the desired state of your infrastructure resources, such as virtual machines, networks, databases, and more. The main configuration file is usually named main.tf, but you can split the configuration across multiple files as needed.
Initialize
Before deployment, Terraform must initialize the working directory.
terraform init
In the project directory, run Terraform init to initialize Terraform. This command prepares Terraform to understand your infrastructure. Think of it as preparing Terraform to understand your infrastructure.
When this command is executed, Terraform will use the specified Terraform Providers to interact with the necessary platforms for the declared resources to perform the provisioning and configuration tasks necessary.
Validate
terraform validate
This checks configuration syntax and basic correctness. A small but important step that prevents deployment issues.
Plan
Preview the modifications necessary before implementation as a means to verify changes before applying them to the infrastructure environment.
terraform plan
Running plan provides a speculative view of what will change. In team and organization contexts, HCP Terraform provides a centralized and secure location for storing input variables and state while also bringing back a tight feedback loop for speculative plans for config authors. Terraform configuration can interact with HCP Terraform through the CLI integration.
Output can stream from remote runs. For example:
$ terraform plan
Running plan remotely in Terraform Enterprise.
Output will stream here
Apply
terraform apply
When this command is executed, Terraform will use the specified Terraform Providers to interact with the necessary platforms for the declared resources to perform the provisioning and configuration tasks necessary.
Once the terraform apply command has finished executing any infrastructure changes, it will modify the Terraform State .tfstate file, so it contains the currently configured state of the infrastructure managed by Terraform.
Reliably and scalably apply infrastructure changes in an automated fashion to ensure continuity across environments.
Verify and Destroy
A typical Terraform workflow looks like this:
Write Code → Initialize → Validate → Plan → Apply → Verify → Destroy
Each stage plays an important role in ensuring infrastructure is deployed safely and consistently.
Verify is the post-apply confirmation that resources match desired state. Destroy is used to teardown resources when they are no longer needed.
Workflow In Practice For Individuals And Teams
Let's first walk through how these parts fit together as an individual working on infrastructure as code.
You write Terraform configuration just like you write code: in your editor of choice. Then you initialize, validate, plan, and apply. The workflow is really an iterative process to follow as you manage the infrastructure through Terraform going forward.
When a team is collaborating on infrastructure, the workflow evolves. HCP Terraform is designed to support and enhance the core Terraform workflow for anyone collaborating on infrastructure, from small teams to large organizations.
HCP Terraform provides a centralized and secure location for storing input variables and state while also bringing back a tight feedback loop for speculative plans for config authors.
Terraform configuration can interact with HCP Terraform through the CLI integration:
hcl
terraform {
cloud {
organization = "my-org"
hostname = "app.terraform.io"
workspaces {
tags = {
layer = "networking"
source = "cli"
}
}
}
}
After you configure the integration, an HCP Terraform API key is all your team members need to edit config and run speculative plans against the latest version of the state file using all the remotely stored input variables.
Workspace selection is common in multi-environment setups:
$ terraform workspace select my-app-dev
Switched to workspace "my-app-dev".
$ terraform plan
Terraform Workflow With Cloud Providers
Terraform is an infrastructure-as-code tool that lets you predictably create, change, and improve your cloud infrastructure by using code. Learn how to use Terraform to provision infrastructure on Google Cloud.
A quickstart example shows how to create, deploy, and execute a first workflow using Terraform. The sample workflow sends a request to a public API and then returns the API's response.
The steps completed are:
- Enable the Workflows API using Terraform
- Create a service account for the workflow using Terraform
- Define and deploy a workflow using Terraform
- Execute the workflow using the Google Cloud CLI
Before you begin, note that Cloud Shell has Terraform already integrated. If you need to install Terraform, see the HashiCorp Terraform documentation. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios.
Security constraints defined by your organization might prevent you from completing the following steps. For troubleshooting information, see Develop applications in a constrained Google Cloud environment.
Workflow Stages Compared
The stages can be compared by purpose, action, and artifact.
| Stage | Purpose | Typical Command | Artifact Produced |
|---|---|---|---|
| Write | Develop IaC configuration to define infrastructure | editor | .tf files |
| Initialize | Prepare working directory and providers | terraform init | .terraform directory |
| Validate | Check syntax and correctness | terraform validate | validation report |
| Plan | Preview modifications before implementation | terraform plan | execution plan |
| Apply | Provision reproducible infrastructure | terraform apply | .tfstate updated |
| Verify | Confirm resources match desired state | manual / tests | verification result |
| Destroy | Remove managed infrastructure | terraform destroy | resources removed |
Terraform isn't the only IaC tool available, but it's one of the most popular and most versatile tools as it supports over 1,000 Terraform Providers for managing all kinds of different infrastructure all from within the same Terraform project if necessary.
Conclusion
The Terraform Workflow process is Write, Plan, Apply, Repeat as you manage infrastructure through a repeatable and reliable process using Infrastructure as Code. The full practical chain expands to Write Code, Initialize, Validate, Plan, Apply, Verify, Destroy, with each step playing an important role in ensuring infrastructure is deployed safely and consistently.
Write establishes the desired state declaratively in HCL. Initialize prepares the working directory and providers. Validate prevents deployment issues early. Plan provides a verifiable preview before any change reaches real infrastructure. Apply reliably and scalably provisions changes and updates the Terraform State file so it contains the currently configured state of the infrastructure managed by Terraform.
For individuals, the workflow is a local loop of authoring, planning, and applying. For teams and organizations, HCP Terraform centralizes state, input variables, and speculative plans, enabling secure collaboration and workspace isolation. The workflow remains iterative, with new infrastructure needs triggering a repeat of Write, then Plan and Apply, to keep environments consistent and reproducible.
Happy Terraform-ing.