Terraform Workflow & Commands
With Terraform installed and authenticated, this
page is how you actually operate it: the five-phase workflow, then the five commands that
drive it - init, plan, apply, fmt, and destroy. The
configuration language itself - resources, variables,
outputs, modules - comes next.
The configuration workflow
The full IaC lifecycle is five phases. Scope is human planning before Terraform - you decide
what resources a project needs and how they connect (e.g. a web-server pool plus a database tier).
The remaining phases - Author, Initialize, Plan, Apply - are the Terraform workflow proper,
the ones backed by terraform commands. Between Plan and Apply sits an optional
validate phase that runs pre-deployment checks against
organization policies.
Only phases 2-5 map to terraform commands (init → Initialize, plan → Plan, apply →
Apply; Author is you writing .tf files). Scope is planning you do first, with no command. If a
question lists the "Terraform workflow" steps, it's Author → Initialize → Plan → Apply, not Scope.
Running the workflow
With Terraform installed and authenticated, the
hands-on loop is four commands - init, plan, apply, destroy - which implement the
Author → Initialize → Plan → Apply workflow.
On Cloud Shell, gcloud credentials are already in place, so once Terraform is installed you
run the loop setup-free; verify the install first with terraform version.
Copy the provider from the Registry
Author .tf files (e.g. main.tf) in a project folder such as infra/. Rather
than memorize syntax, copy the provider block from the Terraform Registry:
open the Google Cloud provider, click Use Provider, and copy the snippet.
The source shortens to hashicorp/google. Then edit the block to set your
project ID before running init.
terraform init - Initialize phase
terraform init is the first command to run after authoring a new configuration
or checking out an existing one from version control. It downloads and installs the
provider plugin (here, Google) so the rest of the workflow has something to talk to.
Terraform uses a plugin-based architecture: each provider is its own encapsulated
binary, distributed separately from Terraform itself, so it can support the many
infrastructure and service providers available. The provider block's source
attribute tells init where to download the plugin from.
- Reads the provider block's
sourceand downloads + installs that provider binary (e.g.hashicorp/google). - Creates a hidden
.terraform/directory in the current working dir, plus various bookkeeping files. - Prints "Initializing provider plugins...", finds the latest plugin, and reports the installed version (e.g.
v4.21).
terraform plan - Plan phase
terraform plan creates an execution plan detailing every resource that will be
created, modified, or destroyed on the next apply. It does not change any
infrastructure - it's a preview.
- Reads the current state of existing remote objects so Terraform state is up to date.
- Compares the current configuration to the prior state and notes any differences.
- Builds a plan that modifies only what is necessary to reach your desired state.
terraform plan never creates or changes infrastructure; it only previews. Run it
before committing a change to version control to confirm it behaves as expected.
Use -out=FILE to save the generated plan to disk, then pass that file to
terraform apply to execute exactly that plan.
terraform apply - Apply phase
terraform apply executes the actions from the plan: it creates the resources and
establishes their dependencies. As with plan, it first shows the execution plan and
waits for your approval before making any changes - if anything looks incorrect or
unsafe, abort here and nothing is touched. The symbols next to each resource say what
action Terraform will take:
Terraform works out the order operations must happen in. Google Cloud won't let a VPC
network be deleted while it still has resources, so Terraform waits until the instance
is destroyed before destroying the network. If apply fails, read the error message
and troubleshoot before re-running.
terraform fmt - consistent formatting
terraform fmt auto-formats your modules and code to match canonical conventions, so
you don't have to hand-tune configuration to meet the standard.
- Separate meta arguments from other arguments - place them first or last, set off by a blank line.
- Indent arguments two spaces from the block definition.
- When two or more arguments share a block, align the values at the
=sign. - When a block has a nested block, place it after all the arguments.
- When code has multiple blocks, separate them with a blank line for readability.
Running terraform fmt rewrites the file so those rules hold - meta arguments move
together, values align at the =, nested blocks drop below the arguments, and blocks
gain separating blank lines:
terraform fmtterraform fmtterraform destroy - tear down resources
terraform destroy behaves like apply but as if all resources had been removed from
the configuration - it tears them down. It's handy for ephemeral development
infrastructure: spin up a dev/test/staging environment, then destroy it once you're done.
You can also destroy specific resources by passing a target in the command.
Destroying infrastructure is a rare event in production. Use terraform destroy
carefully: it destroys any resource and the data associated with it. If a Cloud
Storage bucket holds data, that data cannot be recovered once the bucket is destroyed.