← 返回命令列表

Linux command

terraform-init 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Initialize the current directory

terraform init

Initialize and upgrade

terraform init -upgrade

Initialize with backend configuration

terraform init -backend-config=[path/to/config.hcl]

Initialize without configuring

terraform init -backend=false

Initialize and migrate state

terraform init -migrate-state

Initialize from a module source

terraform init -from-module=[source]

说明

terraform init initializes a working directory containing Terraform configuration files. This is the first command to run after writing a new configuration or cloning an existing one from version control. It is safe to run multiple times and will not delete existing configuration or state. The command performs several initialization steps: it configures the backend for storing state, downloads and installs provider plugins declared in the configuration, and downloads child modules referenced in the configuration. Each of these steps can be individually controlled with flags.

参数

-upgrade
Upgrade modules and plugins to the latest allowed versions.
-backend _true|false_
Enable or disable backend configuration. Default is true.
-backend-config _path_
Provide backend configuration via file path or key=value pair. Can be specified multiple times.
-reconfigure
Reconfigure the backend, ignoring any saved configuration. Prevents state migration.
-migrate-state
Attempt to copy existing state to the new backend with interactive prompts.
-force-copy
Suppress migration prompts and automatically copy state.
-from-module _source_
Copy a module into the target directory before initialization.
-get _true|false_
Download child modules. Default is true.
-plugin-dir _path_
Force plugin installation to read plugins only from the specified directory.
-lockfile _mode_
Set lockfile mode. Use readonly to verify checksums without updating the lockfile.
-input _true|false_
Enable or disable interactive prompts. Default is true.
-lock _true|false_
Lock state files during operations. Default is true.
-lock-timeout _duration_
Duration to retry acquiring a state lock. Default is 0s.
-no-color
Disable color codes in the output.
-json
Enable machine-readable JSON output.

FAQ

What is the terraform-init command used for?

terraform init initializes a working directory containing Terraform configuration files. This is the first command to run after writing a new configuration or cloning an existing one from version control. It is safe to run multiple times and will not delete existing configuration or state. The command performs several initialization steps: it configures the backend for storing state, downloads and installs provider plugins declared in the configuration, and downloads child modules referenced in the configuration. Each of these steps can be individually controlled with flags.

How do I run a basic terraform-init example?

Run `terraform init` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -upgrade do in terraform-init?

Upgrade modules and plugins to the latest allowed versions.