fbpx

HashiCorp Terraform is a powerful Infrastructure as Code (IaC) tool for managing resources both in the cloud and on-premises. The first command that you run when working with a Terraform Project is the terraform init command (aka Terraform project initialization command). The Terraform Init command is a critical part of the Terraform workflow, and must be run before any other Terraform commands can be used. Regardless of your experience level with Terraform, you’ll need to be familiar with the terraform init command.

This article explains how to use the terraform init command, as well as what options are available when using it, with examples. Whether you’re a beginner looking to learn HashiCorp Terraform, or an experienced DevOps Engineer or Site Reliability Engineer (SRE), this article will help you gain further understanding of the Terraform Init command.

What is the Terraform Init Command?

The terraform init command is used to initialize a Terraform working directory; also known as a Terraform project. When the init command is run, it will automatically download and install any required Terraform providers and plugins used within the Terraform code. It will also set up the backend configured in the Terraform code for storing the Terraform state (.tfstate) file. This state file is where Terraform will store information about the infrastructure managed by the project, and it can be stored locally or in a remote / shared location like Azure Storage.

To run the Terraform Init command, you’ll need to first have Terraform installed, then navigate to the directory where the Terraform Project is located and run the following command:

terraform init

After running the terraform init command within the working directory, Terraform will have downloaded and installed any required providers and plugins, and set up the backend state file storage. This will get everything initialized for Terraform to be able to manage the infrastructure configured within the Terraform project code.

Terraform Init command output
Terraform Init command output

The Terraform providers and plugins that are downloaded when the init command is run are saved within a sub-directory of the working directory where the command was run. In other words, the downloads are saved within a sub-directory of the Terraform project. This sub-directory is named .terraform and depending on your operating system settings may be hidden from view. You don’t need to worry about committing this sub-directory into source control.

If you need help when running the terraform init command, you can add the -help option to have Terraform output help information about command usage to the terminal.

terraform init -help

What Init Command Options are Available?

When running the terraform init command it will take direction off the Terraform configuration defined in the code, but there are also several options that can be used on the terminal when running the init command.

Here’s a summary of the different, options available when running the terraform init comand:

  • -backend=true|false: Specifies whether to set up the backend for storing Terraform state. false will skip the backend configuration. By default, this option is set to true.
  • -backend-config=path: Specifies a file containing backend configuration values to use. This option can be used to configure the backend without embedding configuration values into the Terraform configuration files.
  • -get=true|false: Specifies whether to download and install modules. By default, this option is set to true so it will automatically download modules.
  • -input=true|false: Specifies whether to ask for input. By default, this option is set to true. false can be used for a more “quite” execution of the command.
  • -lock=true|false: Specifies whether to lock the state file. By default, this option is set to true. false could be used to disable locking the state files during state-related operations if necessary.
  • -lock-timeout=duration: Specifies the time to wait for a state lock. By default, this option is set to 0s (zero seconds) or “no wait”; which will cause failure if the lock is currently held by another process.
  • -upgrade: Install the latest module and provider versions allowed within configured constraints, overriding the default behavior of selecting exactly the version recorded in the dependency lock file.
  • -reconfigure: Reconfigure a backend, ignoring any saved configuration.

These options can be used to customize the behavior of the terraform init command, such as locking the state file, specifying the backend for storing the Terraform state file, or other options. The terraform init command will generally be used without any options, but options can be used depending on the specific use case a necessary.

Microsoft MVP

Chris Pietschmann is a Microsoft MVP, HashiCorp Ambassador, and Microsoft Certified Trainer (MCT) with 20+ years of experience designing and building Cloud & Enterprise systems. He has worked with companies of all sizes from startups to large enterprises. He has a passion for technology and sharing what he learns with others to help enable them to learn faster and be more productive.
HashiCorp Ambassador Microsoft Certified Trainer (MCT) Microsoft Certified: Azure Solutions Architect