What is Terraform?

Imagine that you made the decision to build a house and made a list of the things you wanted. The raw materials (infrastructure) and the people who can take the raw materials and organize (configure) them to build a home will be the two most crucial items on the list. Terraform performs similar operations. The infrastructure is handled by it. Simply put, Terraform makes it possible for you to automate and control the infrastructure.

Architecture

It has two main components

  1. Core: It has two input sources

    • State: It keeps track of all the changes in an environment.

    • Configuration file: It consists of user-defined data of what should be the desired state. The task of the core is to determine how to reach the desired state provided in the configuration file using these two inputs. The core then generates, updates, and destroys the necessary data after comparing the current state to the desired state.

  2. Providers: Like AWS, Azure, Kubernetes ...etc. Terraform has more than 100 providers. After creating the execution based on the input file and state, the core engages providers to carry out the plan.

ybjPix6sM.jpg

Commands

General Commands

  1. terraform version: get the terraform version.

  2. terraform get -update=true: download and update root modules.

  3. terraform console: open up a terraform interactive terminal.

  4. terraform graph | dot -Tpng > graph.png: create a dot diagram of terraform dependencies.

  5. terraform fmt: format terraform code to HCL standards.

  6. terraform validate: validate terraform code syntax.

  7. terraform -install-autocomplete: enable tab autocompletion in the terminal.

  8. terraform providers: show information about provider requirements.

  9. terraform login: login in the terraform cloud.

  10. terraform logout: log out of the terraform cloud.

Workspaces

  1. terraform workspace list: list the available workspaces.

  2. terraform workspace new development: create a new workspace.

  3. terraform workspace select default: Select an existing workspace.

Initialize Terraform

  1. terraform init: initialize terraform in the current working directory.

  2. terraform init -get-plugins=false: skip plugin installation.

  3. terraform init -plugin-dir=PATH: force plugin installation from a directory.

  4. terraform init -upgrade: upgrades modules and plugins at initialization.

  5. terraform init -migrate-state -force-copy: update backend configuration.

  6. terraform init -backend=false: skip backend configuration.

  7. terraform init -backend-config=FILE: use a local backend configuration.

  8. terraform init -lock-timeout=120s: change state lock timeout(default is set to zero).

Plan Terraform

  1. terraform plan: produce a plan with the diff in code and state.

  2. terraform plan -out current.tfplan: output a plan file for reference during apply.

  3. terraform plan -destroy: output a plan to show the effect of terraform destroy.

  4. terraform plan -target=ADDRESS: target a specific resource for deployment.

Target option is also available for apply and destroy commands

Outputs

  1. terraform output: list available outputs.

  2. terraform output NAME: output a specific value.

Apply Terraform

  1. terraform apply: apply the current terraform code.

  2. terrraform apply current.tfplan: specify a previously generated plan to apply.

  3. terraform apply -auto-approve: enable auto approval or automation.

Destroy Terraform

  1. terraform destroy: destroy resources managed by terraform state.

  2. terraform destroy -auto-approve: enable auto approval or automation.

Manage Terraform State

  1. terraform state list: list all resources in terraform state.

  2. terraform state show ADDRESS: show details of a specific resource.

  3. terraform state mv SOURCE DESTINATION: track an existing resource in the state under a new name.

  4. terraform state import ADDRESS to: import a manually created resource into the state.

  5. terraform state pull > terraform.tfstate: pull state and save to a local file.

  6. terraform state push PATH: push the state to a remote location.

  7. terraform state replace-provider A B: replace a resource provider.

  8. terraform taint ADDRESS: taint a resource to force redeployment on apply.

  9. terraform untaint ADDRESS: untainted a previously tainted resource.

ย