Skip to content

faisikhan/Terraform-by-DevOpsMolvi

Repository files navigation

Terraform - Infrastructure Provisioning Tool

Terraform is an open source infrastructure as a code tool to automatically build your infrastructure.

Terraform loads all files ending in .tf or .tf.json in the working directory. List of Terraform providers: https://registry.terraform.io/browse/providers

How to Install Terraform

  1. Update the system and install the following packages using the below command:

sudo apt-get update && sudo apt-get install -y gnupg software-properties-common curl

  1. Add the HashiCorp GPG Key

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -

  1. Add the offical HashiCorp Repository for Linux

sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

  1. Update the system after adding the key

sudo apt-get update

  1. Install Terraform

sudo apt-get install terraform

Important Terraform Commands:

terraform init ==========> Initializes your working directory for terraform. Your terraform files must be there.

image

terraform validate ==========> Checks if the configuration is valid.

image

terraform plan ==========> It generates an overview of how terraform will execute the plan. It does not actually build the infrastructure, simply shows a layout.

image

terraform fmt ==========> Properly formats the terraform configuration file.

terraform apply ==========> Apply the changes and our infrastructure starts building. image

terraform init --reconfigure [If you did some changes in main.tf or added any additional file, and you want to reflect those changes]

==================================================

terraform.tfstate file stores the current state of your infrastructure components, on your local machine.

terraform init -upgrade ==========> When we upgrade the version of Terraform provider. The error looks similar to the following:

Could not retrieve the list of available versions for provider hashicorp/google: locked provider registry.terraform.io/hashicorp/google 3.90.1 does not match configured version constraint ~> 4.41.0; │ must use terraform init -upgrade to allow selection of new versions

I have updated the current version 3.90.1 to the latest available 4.41.0.

terraform apply -auto-approve ==========> It will build the infrastructure without asking "yes".