Course contentsShow
← Cloud and Infrastructure
Lesson 790 of 2,98915. Terraform in PracticeFree lesson

Backend Initialization and Migration

Use terraform init to initialize backends and safely migrate state from local to remote storage.

Backend Initialization and Migration

What You'll Learn

You'll learn how to use terraform init to set up backend configurations and safely migrate your state file from local storage to remote backends like S3, Azure Storage, or GCS.

Understanding Backend Initialization

When you configure a new backend in your Terraform code (like the S3, Azure Storage, or GCS backends you've learned), nothing happens automatically. You must run terraform init to tell Terraform about this backend configuration.

Think of it like moving your important documents from your desk drawer (local) to a bank vault (remote). You can't just decide to use the vault—you need to physically transport the documents there safely.

The Initialization Process

When you run terraform init after adding or changing a backend configuration:

  1. Terraform detects the backend change
  2. It prompts you to migrate your existing state file
  3. After confirmation, it safely copies your state to the new backend
  4. Your local state becomes a reference to the remote location

Safe Migration Steps

# 1. Add backend configuration to your Terraform code
# 2. Run initialization
terraform init

# Terraform will detect the change and prompt:
# "Do you want to copy existing state to the new backend?"

When you confirm, Terraform handles the migration automatically—copying your state file, verifying the transfer, and updating your local configuration to point to the remote backend. Your infrastructure remains unchanged; only the state file location moves.

Reconfiguring Backends

Use terraform init -reconfigure to change backends without migrating state, or -migrate-state to explicitly trigger migration without prompts.

Key Takeaway: terraform init is your safe gateway for backend changes—it initializes new backends and orchestrates state migration, ensuring your infrastructure state is never lost during the transition from local to remote storage.