Initialize
Theterraform init command prepares a working directory for use with Terraform. It is the first command you should run after writing a new Terraform configuration or cloning an existing one from version control.
What It Does
terraform init performs several initialization steps:
- Downloads and installs provider plugins required by your configuration
- Downloads and installs modules referenced in your configuration
- Initializes the backend for storing Terraform state
- Creates a dependency lock file (
.terraform.lock.hcl) to ensure consistent provider versions
When to Use It
Runterraform init when:
- Starting a new Terraform project
- Cloning an existing Terraform configuration from version control
- Adding or modifying provider requirements
- Changing backend configuration
- Adding or updating module sources
- Switching between workspaces that use different backends
Getting Started
Navigate to your Terraform directory
Change to the directory containing your Terraform configuration files:
Common Flags and Options
Provider Installation
-upgrade
Upgrade all provider plugins to the latest versions that comply with version constraints:
-plugin-dir=PATH
Force plugin installation from a local directory instead of downloading from registries:
Backend Configuration
-backend-config=PATH or -backend-config="KEY=VALUE"
Provide backend configuration during initialization:
-reconfigure
Reconfigure the backend, ignoring any saved configuration:
-migrate-state
Migrate state from a previous backend configuration to a new one:
Dependency Lock File
-lockfile=MODE
Control how Terraform handles the dependency lock file. Valid modes:
readonly- Do not update the lock file (useful in CI/CD)- Default - Update the lock file if provider selections change
Other Options
-input=false
Disable interactive prompts:
-no-color
Disable color output:
Best Practices
Version Control
Commit the lock file:.terraform.lock.hcl file ensures all team members and CI/CD systems use the same provider versions.
Ignore the .terraform directory:
Add to .gitignore:
CI/CD Pipelines
In automated environments, use appropriate flags to prevent interactive prompts and ensure reproducibility:Backend Migration
When migrating backends, always:- Backup your state file before running
init - Use
-migrate-stateto copy state to the new backend - Verify the state was migrated correctly before destroying the old backend
Module Updates
When updating module sources, use-upgrade to fetch the latest versions:
Provider Version Constraints
Always specify version constraints in your configuration to ensure stability:Troubleshooting
Provider Installation Failures
If provider installation fails:Lock File Conflicts
If you see warnings about incomplete lock file information:terraform providers lock to generate checksums for multiple platforms:
Backend Configuration Errors
If backend initialization fails:init.
Next Steps
After successful initialization:- Run
terraform planto preview infrastructure changes - Run
terraform applyto create or update infrastructure - Use
terraform workspacecommands to manage multiple environments