Synopsis
Prepare your working directory for other commandsDescription
Theterraform init command initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.
This command performs several initialization steps:
- Downloads and installs provider plugins
- Initializes the backend for storing state
- Downloads modules referenced in the configuration
- Creates a dependency lock file
Usage
Options
Backend Configuration
Configure the backend for this configuration. Alias for
-cloud.Configure HCP Terraform or backend for this configuration. Alias for
-backend.Configuration to be merged with what is in the configuration file’s ‘backend’ block. This can be either a path to an HCL file with key/value assignments or a ‘key=value’ format. This can be specified multiple times. The backend type must be in the configuration itself.
Reconfigure the backend, ignoring any saved configuration.
Reconfigure the backend, and attempt to migrate any existing state. Cannot be used with
-reconfigure.Module Installation
Download modules for this configuration.
Upgrade modules and plugins to the latest versions that comply with the configuration’s version constraints.
Copy the contents of the given module into the target directory before initialization.
Provider Configuration
Set a dependency lockfile mode. Valid values:
readonly (don’t modify the lock file) or empty string (default behavior).Directory containing plugin binaries. This overrides all default search paths for plugins, and prevents the automatic installation of plugins. This flag can be used multiple times.
State Management
Lock the state file when locking is supported.
Duration to retry a state lock. For example: ”10s” for 10 seconds.
Suppress prompts about copying state data. This is equivalent to providing a “yes” to all confirmation prompts.
Other Options
Ask for input for variables if not directly set.
Produce output in a machine-readable JSON format.
If specified, output won’t contain any color.
Continue even if remote and local Terraform versions are incompatible. This can result in an unusable workspace, and should be used with extreme caution.
Set the Terraform test directory, defaults to “tests”.
Examples
Basic Initialization
Initialize a working directory:Initialize with Backend Configuration
Initialize with additional backend configuration:Upgrade Providers and Modules
Upgrade all providers and modules to the latest versions:Non-Interactive Mode
Run init in non-interactive mode (useful for CI/CD):Initialize from Module
Copy a module and initialize:Exit Codes
0- Success1- Error occurred