Bootstrap FreeKB - Terraform - Initialize Terraform using the terraform init command
Terraform - Initialize Terraform using the terraform init command

Updated:   |  Terraform articles

terraform init is almost always the first Terraform command you will issue after creating .tf files, to initialize Terraform. Let's say you have the following files on your Terraform server.

├── provider.tf
├── required_providers.tf

 

Let's say required_provider.tf has the following to initialize the Amazon Web Services (AWS) provider.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

And provider.tf could have something like this.

provider "aws" {
  access_key = var.access_key
  secret_key = var.secret_key
}

 

The terraform init command should return something like this.

]$ terraform init

Initializing the backend...
Initializing modules...
- child in child

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.8.0...
- Installed hashicorp/aws v5.8.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

 

The terraform init command will also create a couple hidden files in the directory of your main root module (main.tf), .terraform (directory) and .terraform.lock.hcl (file).

]$ ls -lisa
total 28
456191 4 drwxr-sr-x. 4 john.doe john.doe 4096 Jul 18 04:58 .
460959 4 drwxr-sr-x. 4 john.doe john.doe 4096 Jul 18 04:48 ..
486500 4 -rw-r--r--. 1 john.doe john.doe  190 Jul 18 04:49 provider.tf
486500 4 -rw-r--r--. 1 john.doe john.doe  190 Jul 18 04:49 reqired_providers.tf
455334 4 drwxr-sr-x. 4 john.doe john.doe 4096 Jul 18 04:58 .terraform
488109 4 -rw-r--r--. 1 john.doe john.doe 1376 Jul 18 04:58 .terraform.lock.hcl

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9905cf in the box below so that we can be sure you are a human.