Skip to content

init

Usage

The terraform init command is used to initialize 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.

Here are some of the steps that the terraform init command performs under the hood:

  • Download and install providers: Terraform uses providers to interact with the various systems it manages. During the initialization process, Terraform will download and install the providers required by the current configuration.
  • Configure the backend: The backend is responsible for storing Terraform’s state data. During the initialization process, Terraform will configure the backend specified in the current configuration.
  • Download and install modules: Modules allow you to package and reuse resource configurations. During the initialization process, Terraform will download and install any modules mentioned in the root module.
  • Create a lock file: Terraform uses a lock file to ensure that multiple users or processes do not run conflicting operations concurrently. During the initialization process, Terraform will create a lock file if it doesn’t already exist.

Basic Options

-input

The input option for terraform init controls whether Terraform prompts for input during initialization. Use input=false to disable this behavior. Terraform will either use default values or fail with an error if input is needed. Example: $ terraform init -input=false.

-lock

The lock option for terraform init controls whether Terraform locks the state file during initialization. By default, Terraform locks the state file to prevent concurrent modifications. Use lock=false to disable this behavior. For example: $ terraform init -lock=false. Be aware that disabling locking can increase the risk of data loss or corruption.

-lock-timeout

lock-timeout option specifies how long Terraform will wait to acquire a lock on the state file before failing. Example: $ terraform init -lock-timeout=30s. The value for this option should be a duration string, such as “30s” for 30 seconds. Default timeout is 0, which means that Terraform will fail immediately if it cannot acquire a lock.

-no-color

The no-color option disables color output in Terraform's init command. Use it to display plain text. For example: $ terraform init -no-color.

-upgrade

Use -upgrade option with terraform init command to upgrade modules and plugins installed in working directory to latest versions. Example: $ terraform init -upgrade.

-from-module

The -from-module option is used to copy the contents of a specified module into the current working directory before initialization. Use it to create a new Terraform configuration based on an existing module.

Example: $ terraform init -from-module=hashicorp/consul/aws

The terraform init command is automatically run after copying the module contents into the current working directory using the -from-module option.

Backend Options

-backend

backend=false option for terraform init skips backend configuration during initialization. It's not recommended as it can lead to data loss or corruption if multiple users or processes are running Terraform concurrently. Example: $ terraform init -backend=false

-migrate-state

The migrate-state option for terraform init reconfigures the backend and migrates existing state to the new backend. Use force-copy to suppress prompts and automatically answer "yes" to migration questions. Example: $ terraform init -migrate-state -force-copy.

-reconfigure

The reconfigure option re-initializes the backend, even if it has already been initialized. Use it to change your backend configuration. Example: $ terraform init -reconfigure.

-backend-config

backend-config option in terraform init sets backend configuration values directly on the command line. Use it with key-value pairs to set backend configuration values. For example, $ terraform init -backend-config="bucket=mybucket" -backend-config="key=path/to/my/key" -backend-config="region=us-east-1" sets bucket, key, and region backend configuration values for the s3 backend, allowing Terraform to store the state file in the specified S3 bucket.

Child Module Installation options

-get

You might want to use the get=false option with the terraform init command if you have already downloaded and installed the required modules manually, or if you want to use a specific version of a module that is not available from the module registry.

The -get option for terraform init controls whether modules mentioned in the root module are downloaded and installed during initialization. Use -get=false to disable this behavior. Example: $ terraform init -get=false.

Plugins options

-upgrade

The upgrade option upgrades modules and plugins in the working directory to their latest versions. Use it to ensure you are using up-to-date dependencies. Example: $ terraform init -upgrade.

-get-plugins

The get-plugins option with terraform init controls whether Terraform should download plugins. Set get-plugins=false to disable plugin download during initialization. Example: $ terraform init -get-plugins=false.

This can be helpful if you have connectivity issues or firewall problems. You can use pre-installed plugins by either putting the plugins in the same directory as the terraform binary or by setting the plugin-dir flag.

-plugin-dir

plugin-dir is a terraform init option that specifies the directory containing plugin binaries. It overrides default search paths and prevents automatic plugin installation. Example: terraform init -plugin-dir=/path/to/plugin/directory.

The -plugin-dir option does not update the binaries in the specified directory. If you want to update the plugins, you can do so manually by downloading the latest version of the plugin and replacing the old version in the specified directory.

-lockfile

The lockfile option sets a dependency lockfile mode for terraform init. Valid lockfile modes are readonly and upgrade. Example: terraform init -lockfile=readonly will initialize the working directory and suppress any changes to the lockfile, but verify checksums against the information already recorded in the lockfile.

If you do not use the -lockfile option, Terraform will create or update the lockfile during initialization. This means that if there are any changes to the dependencies, such as new versions of providers or modules, Terraform will update the lockfile to reflect these changes

You can use the -lockfile option when you want to have more control over the dependencies used by Terraform. For example, if you are working in a team and want to ensure that everyone is using the same versions of providers and modules, you can use the [-lockfile=readonly](https://www.terraform.io/cli/commands/init) option to prevent changes to the lockfile.