terraform workflow - ghdrako/doc_snipets GitHub Wiki

Terraform life cycle works with terraform init, terraform plan, terraform apply, and terraform destroy.

Terraform init

reasons to run the terraform init:

  • When you have added a new module, provider, or provisioner
  • When you have updated the required version of a module, provider, or provisioner
  • When you want to change or migrate the configured backend
terraform init -h

Options:

  • -backend=true: Configures the backend for this configuration.
  • -backend-config=path: This can either be a path to a Hashicorp Configuration File (HCL) file with key/value assignments (same format as terraform.tfvars) or a key=value format. This is merged with what is in the configuration file, which can be specified multiple times. The backend type must be in the configuration itself.
  • -from-module=SOURCE: Copies the contents of a given module into the target directory before initialization.
  • -lock=true: Locks the state file when locking is supported.
  • -no-color: If specified, the output won't contain any color.
  • -plugin-dir: Directory containing plugin binaries. This overrides all default search paths for plugins and prevents the automatic installation of plugins. This flag can be used multiple times.
  • -reconfigure: Reconfigures the backend, ignoring any saved configuration.
  • -upgrade=false: If installing modules (-get) or plugins (-get-plugins), ignores previously downloaded objects and installs the latest version allowed within configured constraints.
  • -verify-plugins=true: Verifies the authenticity and integrity of automatically downloaded plugins.
# provider AzureRM - Azure Resource Manager
terraform {
  required_version = ">= 1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.55.0"
    }
  }
}

provider "azurerm" {
  features {}
}

Terraform will not be able to download third-party plugins. These can instead be manually installed in the user plugins directory, located at ~/.terraform.d/plugins on Linux/Mac operating systems and %APPDATA%\terraform.d\plugins on Windows.

Information about third-party plugins, you can refer to https://www.hashicorp.com/blog/automatic-installation-of-third-party-providers-with-terraform-0-13.

terraform init  # in current directory create .teraform directory ans structure and download provider
terraform init -backend-config='C:\provider' 

Remote backand

# set the remote backend as Azure Blob storage
terraform {
  required_version = ">= 1.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.55.0"
    }
  }
  backend "azurerm" {
    resource_group_name  = "terraform-rg"
    storage_account_name = "terraformstatestg"
    container_name       = "tfstate-container"
    key                  = "tfstate"
  }
}

provider "azurerm" {
  features {}
}

Terraform validate

You can run the terraform validate command explicitly, otherwise validation of the configuration file will be done implicitly during the execution of the terraform plan or terraform apply commands, so it is not mandatory to run this command.


Terraform plan

When the terraform plan command is being run, Terraform performs a refresh in the backend (unless you have explicitly disabled it), and Terraform then determines which actions it needs to perform to meet the desired state you have defined in the configuration files.

terraform plan -var "nb_webapp=5"   # assign variable value overreade that defined in terraform files  

Terraform apply

terraform apply -auto-approve

Automate approve and execute plan.


Terraform destroy