Terraform CLI Basics: init, plan, apply, destroy
Terraform's Command Line Interface (CLI) is your primary tool for interacting with Terraform. Mastering its core commands is fundamental to managing your infrastructure as code. These commands orchestrate the lifecycle of your infrastructure, from initialization to destruction.
Initializing Your Terraform Project: `terraform init`
The
terraform init
- Downloads and installs the necessary provider plugins (e.g., AWS, Azure, GCP) specified in your configuration.
- Initializes backend configuration, which is where Terraform stores its state file.
- Downloads modules referenced in your configuration.
terraform init
command?To initialize a Terraform working directory by downloading provider plugins, configuring the backend, and fetching modules.
Previewing Infrastructure Changes: `terraform plan`
Before making any changes to your infrastructure, it's essential to understand what Terraform will do. The
terraform plan
- Creating new resources.
- Modifying existing resources.
- Destroying resources that are no longer defined in your configuration.
Always run terraform plan
before terraform apply
to avoid unintended infrastructure changes.
Applying Infrastructure Changes: `terraform apply`
Once you've reviewed the execution plan and are satisfied with the proposed changes, you use
terraform apply
-auto-approve
The Terraform workflow can be visualized as a cycle. terraform init
sets up the environment, terraform plan
creates a blueprint of changes, and terraform apply
builds or modifies the infrastructure according to that blueprint. Finally, terraform destroy
dismantles the infrastructure.
Text-based content
Library pages focus on text content
Destroying Infrastructure: `terraform destroy`
When you no longer need the infrastructure managed by Terraform, you can use the
terraform destroy
apply
-auto-approve
terraform destroy
Understanding the Workflow
The typical Terraform workflow involves a sequence of these commands:
- : Prepare your working directory.codeterraform init
- (optional but recommended): Check the syntax of your Terraform files.codeterraform validate
- : Preview the changes.codeterraform plan
- : Execute the changes.codeterraform apply
- : Remove the infrastructure when no longer needed.codeterraform destroy
Loading diagram...
Learning Resources
Official HashiCorp documentation detailing the `terraform init` command, its purpose, and common options.
Comprehensive guide to the `terraform plan` command, explaining how to generate and interpret execution plans.
Official documentation for the `terraform apply` command, covering its functionality and approval mechanisms.
Detailed explanation of the `terraform destroy` command, including its impact and safety features.
A practical tutorial from HashiCorp that walks through the fundamental Terraform workflow, including init, plan, and apply.
A blog post from HashiCorp that dives deeper into the importance and interpretation of Terraform execution plans.
An article that breaks down the standard Terraform workflow and the role of each core CLI command.
A beginner-friendly video that introduces Terraform and demonstrates the basic CLI commands in action.
Essential reading on Terraform state management, which is critical for understanding how `plan` and `apply` work.
A Wikipedia article providing context on the broader concept of Infrastructure as Code, which Terraform embodies.