0
0
TerraformHow-ToBeginner · 3 min read

How to Use terraform init: Syntax, Example, and Tips

Use terraform init to initialize a Terraform working directory by downloading required providers and setting up the backend. Run it once before applying or planning your infrastructure changes.
📐

Syntax

The basic syntax of terraform init is simple and can include optional flags to customize initialization.

  • terraform init: Initializes the current directory.
  • -backend=true|false: Enables or disables backend configuration.
  • -upgrade: Upgrades provider plugins to the latest versions.
  • -input=false: Disables interactive input prompts.
bash
terraform init [options]

# Common options:
# -backend=true|false
# -upgrade
# -input=false
💻

Example

This example shows how to initialize a Terraform directory that contains a simple AWS provider configuration. Running terraform init downloads the AWS provider plugin and prepares the directory for further commands.

hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-12345"
  acl    = "private"
}

# Run this command in the directory containing this file:
# terraform init
Output
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
⚠️

Common Pitfalls

Common mistakes when using terraform init include:

  • Running terraform init in the wrong directory without Terraform files.
  • Not running terraform init after changing provider versions or backend settings.
  • Ignoring errors about missing plugins or backend configuration.
  • Using -backend=false unintentionally, which skips backend setup.

Always check the output for errors and rerun terraform init after changes.

bash
## Wrong: skipping init after changing provider version
# terraform plan
# Error: Provider not initialized

## Right: run init first
terraform init
terraform plan
📊

Quick Reference

Here is a quick summary of useful terraform init options:

OptionDescription
terraform initInitialize the current directory
-backend=falseSkip backend initialization
-upgradeUpgrade provider plugins to latest versions
-input=falseDisable interactive input prompts
-reconfigureReconfigure backend, ignoring existing config

Key Takeaways

Always run terraform init before other Terraform commands to prepare your working directory.
Use -upgrade to update provider plugins when needed.
Check for errors during initialization and rerun terraform init after changes.
Avoid running Terraform commands without initializing to prevent errors.
Use terraform init -backend=false only if you want to skip backend setup.