LibraryTerraform CLI Basics: `init`, `plan`, `apply`, `destroy`

Terraform CLI Basics: `init`, `plan`, `apply`, `destroy`

Learn about Terraform CLI Basics: `init`, `plan`, `apply`, `destroy` as part of Terraform Infrastructure as Code Mastery

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

code
terraform init
command is the first step in any Terraform project. It initializes a working directory containing Terraform configuration files. This command performs several crucial tasks:

  • 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.
What is the primary purpose of the 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

code
terraform plan
command generates an execution plan. It reads your Terraform configuration, compares it with the current state of your infrastructure, and shows you exactly what actions Terraform will take to achieve the desired state. This includes:

  • 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

code
terraform apply
to create, update, or destroy infrastructure resources. Terraform will prompt you to confirm the action unless you provide the
code
-auto-approve
flag. This command executes the actions outlined in the plan, bringing your infrastructure into the desired state defined by your configuration.

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

code
terraform destroy
command. This command reads your configuration and state file to identify all resources managed by Terraform and prompts you to confirm their destruction. It's a powerful command that should be used with caution, as it will permanently remove your infrastructure. Similar to
code
apply
, you can use
code
-auto-approve
to bypass the confirmation prompt.

What is the command used to remove all infrastructure managed by Terraform?

terraform destroy

Understanding the Workflow

The typical Terraform workflow involves a sequence of these commands:

  1. code
    terraform init
    : Prepare your working directory.
  2. code
    terraform validate
    (optional but recommended): Check the syntax of your Terraform files.
  3. code
    terraform plan
    : Preview the changes.
  4. code
    terraform apply
    : Execute the changes.
  5. code
    terraform destroy
    : Remove the infrastructure when no longer needed.

Loading diagram...

Learning Resources

Terraform CLI Documentation - Initialization(documentation)

Official HashiCorp documentation detailing the `terraform init` command, its purpose, and common options.

Terraform CLI Documentation - Plan(documentation)

Comprehensive guide to the `terraform plan` command, explaining how to generate and interpret execution plans.

Terraform CLI Documentation - Apply(documentation)

Official documentation for the `terraform apply` command, covering its functionality and approval mechanisms.

Terraform CLI Documentation - Destroy(documentation)

Detailed explanation of the `terraform destroy` command, including its impact and safety features.

Terraform Basics: The Core Workflow(tutorial)

A practical tutorial from HashiCorp that walks through the fundamental Terraform workflow, including init, plan, and apply.

Understanding Terraform's Execution Plan(blog)

A blog post from HashiCorp that dives deeper into the importance and interpretation of Terraform execution plans.

Terraform Workflow Explained(blog)

An article that breaks down the standard Terraform workflow and the role of each core CLI command.

Terraform: A Gentle Introduction(video)

A beginner-friendly video that introduces Terraform and demonstrates the basic CLI commands in action.

Terraform State: The Heart of Terraform(documentation)

Essential reading on Terraform state management, which is critical for understanding how `plan` and `apply` work.

Infrastructure as Code with Terraform(wikipedia)

A Wikipedia article providing context on the broader concept of Infrastructure as Code, which Terraform embodies.