G
GuideDevOps
Lesson 11 of 14

Terraform Lifecycle (Init, Plan, Apply)

Part of the Terraform tutorial series.

The Terraform lifecycle is a predictable process that ensures your desired state (HCL code) matches the actual state (Cloud resources).

1. Write: Define the Desired State

You describe what you want in your .tf files.

main.tf:

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-devops-bucket-2026"
}

2. Init: Initialize the Workspace

This step prepares your environment and ensures all plugins are ready.

Action:

terraform init

3. Plan: Preview the Changes

Terraform compares your code with what is already in the cloud and tells you the difference.

Action:

terraform plan

Result:

  # aws_s3_bucket.my_bucket will be created
  + resource "aws_s3_bucket" "my_bucket" {
      + arn                         = (known after apply)
      + bucket                      = "my-unique-devops-bucket-2026"
      + force_destroy               = false
      + id                          = (known after apply)
      ...
    }
 
Plan: 1 to add, 0 to change, 0 to destroy.

4. Apply: Reach the Desired State

Terraform executes the plan and updates the State File (terraform.tfstate).

Action:

terraform apply -auto-approve

5. Summary: Why this workflow matters?

  1. Safety: The plan step prevents accidental destructions.
  2. State Management: The state file keeps track of everything, so you don't have to.
  3. Collaboration: Everyone on the team follows the same steps, making infrastructure changes predictable.