Terraform Init

terraform init performs the following tasks:

  • Initializes the backend and configures it based on the settings in the configuration file
  • Initializes the state of the configuration in the backend.
  • Downloads the providers required by the configuration and installs them locally; Load required provider plugins
  • Initializes the modules required by the configuration and installs them locally; Retrieves the source code for all referenced modules
  • Cache modules from the public Terraform Module Registry on disk in .terraform subdirectory

What terraform init does not do?

  • Does not validate variables during the init command.
  • Does not create any sample configuration file like sample.tf or main.tf file

Step: 1 Defining the resource configuration file:

The above code is written in HashiCorp Configuration Language (HCL) and creates an Amazon Web Services (AWS) Virtual Private Cloud (VPC) with a CIDR block of 10.0.0.0/16.

The resource block has two parts: the resource type and the resource name. In this case, the resource type is “aws_vpc” and the resource name is “bairav-vpc”. The “aws_vpc” resource type is a part of the AWS provider for Terraform, and it is used to create a VPC.

The “cidr_block” argument specifies the IP address range for the VPC. The CIDR block 10.0.0.0/16 provides a private IP address range with a total of 65,536 IP addresses. This is a common IP address range used for private networks.

This code creates a VPC, but it does not create any subnets, route tables, or other resources. It is the foundation for creating a private network in AWS.

Step: 2 Running the terraform init command

The above output is the result of running the “terraform init” command. This command initializes a Terraform working directory and downloads any necessary provider plugins.

The first section of the output shows that Terraform is initializing the backend. The backend is the location where Terraform saves the state of the infrastructure it manages. This can be a local file or a remote storage service such as Amazon S3. In this case, it is “local” on disk.

The second section of the output shows that Terraform is initializing the provider plugins. Provider plugins are used to interact with various infrastructure services, such as AWS, Azure, or Google Cloud Platform. In this case, Terraform is initializing the “hashicorp/aws” provider plugin.

The third section of the output shows that Terraform has found the latest version of the “hashicorp/aws” provider plugin and is installing version 4.55.0. This version is signed by HashiCorp, the company that develops and maintains Terraform.

Once the initialization process is complete, Terraform is ready to use the AWS provider to manage the infrastructure defined in the Terraform configuration files.

Step:3 Output of “terraform init”

a. Importance of .terraform.lock.hcl file:

The .terraform.lock.hcl file is created by Terraform during the initialization process and records the specific versions of the provider plugins that were selected for the current configuration. This ensures that the same provider plugin versions are used consistently across different Terraform environments, which can help prevent compatibility issues and other errors.

Note: By including the .terraform.lock.hcl file in version control, you can ensure that other members of your team are using the same provider plugin versions when they run Terraform commands on the same infrastructure. This can help ensure consistency and avoid unexpected changes to the infrastructure.

b. “.terraform” folder

As a part of this initialization process, Terraform creates a hidden directory called .terraform in the project directory.

The .terraform directory contains cached copies of the downloaded provider plugins and modules, as well as any lock files used to track dependencies.

In general, the directory structure of .terraform looks something like this:

.terraform/
├── providers/
│   ├── registry.terraform.io/
│   │   └── hashicorp/
│   │       ├── aws/
│   │       └── null/
│   
├── modules/
│   ├── module1/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   ├── module2/
│   │   ├── main.tf
│   │   ├── variables.tf
│   │   └── outputs.tf
│   └── ...

The providers/ directory contains provider plugins that have been downloaded and cached by Terraform.

The modules/ directory contains cached copies of any modules that have been downloaded, including any nested modules within them.

For example, let’s look at the below folder structure created for the resource configuration of AWS VPC which we tried earlier.

The .terraform/providers/ directory contains the downloaded and installed provider plugins for your Terraform project. The directory structure of this folder is based on the naming conventions used by Terraform to identify the specific provider and version of the plugin.

In the path provided, the directory is .terraform/providers/registry.terraform.io/hashicorp/aws/4.55.0/windows_386.

Let’s break this down:

  • registry.terraform.io is the default module registry used by Terraform to download provider plugins from the public Terraform registry.
  • hashicorp is the name of the provider, in this case, the AWS provider developed by HashiCorp.
  • aws is the specific name of the provider. In this case, it’s the AWS provider.
  • 4.55.0 is the version of the AWS provider that Terraform has downloaded and installed.
  • windows_386 is the target operating system and architecture for the provider plugin. In this case, it’s the Windows 32-bit architecture.

The files within this folder are the actual provider plugin files that Terraform has downloaded and installed for use in your project. These files are specific to the provider, version, and architecture, and are used by Terraform to communicate with the AWS API and manage AWS resources.

Note: It’s generally safe to ignore the .terraform directory and leave it in place, as it’s used by Terraform to optimize subsequent runs of terraform apply or terraform plan. However, if you want to remove it, you can simply delete the directory and run terraform init again to regenerate it.

Note: Below are the questions that may appear in the “HashiCorp Certified: Terraform Associate”. The questions are collected from various sources and the answers are best to my knowledge and might differ from the actuals.


Which of the following is not an action performed by terraform init?

a. Create a sample main.tf fileInit doesn’t perform this action
b. Initialize a configured backend
c. Retrieve the source code for all referenced modules
d. Load required provider plugins

Explanation: Answer: A

Terraform init initializes a new or existing Terraform working directory by creating an initial state file, downloading provider modules, and preparing the backend for use with the configuration. It doesn’t create a sample main.tf file.

————————————————————————————————————

What command does Terraform require the first time you run it within a configuration directory?

  • A. terraform import
  • B. terraform init 
  • C. terraform plan
  • D. terraform workspace

————————————————————————————————————

Terraform Init prepares the state storage, retrieves modules, and retrieves plugins. True or False.
Answer: True

———————————————————————————————————–

You just upgraded the version of a provider in an existing Terraform project. What do you need to do to install the new provider?

  • A. Run terraform apply -upgrade
  • B. Run terraform init -upgrade
  • C. Run terraform refresh
  • D. Upgrade your version of Terraform

You do not need to upgrade Terraform to install a new provider version. To install the new provider version, you should run terraform init. This will download the new provider plugin and make it available for use in your Terraform configuration. So the correct answer is B. Run terraform init -upgrade.

terraform apply and terraform refresh does not install provider plugins. terraform apply -upgrade is used to upgrade the resource configuration in the state file to match the current configuration in the Terraform code.

——————————————————————————————————————–

As a developer, you want to ensure your plugins are up to date with the latest versions. Which Terraform command should you use?

  • A. terraform init -upgrade
  • B. terraform apply -upgrade
  • C. terraform refresh -upgrade
  • D. terraform providers -upgrade

The correct Terraform command to update the provider plugins to their latest versions is terraform init -upgrade.

——————————————————————————————————————

Which task does terraform init not perform?

  • A. Sources all providers present in the configuration and ensures they are downloaded and available locally
  • B. Connects to the backend
  • C. Sources any modules and copies the configuration locally
  • D. Validates all required variables are present

——————————————————————————————————————

When you initialize Terraform, where does it cache modules from the public Terraform Module Registry?

  • A. On disk in the /tmp directory
  • B. In memory
  • C. On disk in the .terraform sub-directory
  • D. They are not cached

——————————————————————————————————————

error: Content is protected !!