Skip to content

Terraform

Basic actions

Initialize Terraform:

terraform init
# To use a file to describe backend configuration
terraform init -backend-config=vars/dev.backend.tfvars

Check what will be modfied:

terraform plan -out=tfplan .
terraform plan -var-file=vars/dev.terraform.tfvars

Apply modifications:

terraform apply -var-file=vars/dev.terraform.tfvars -auto-approve

Get the list of existing ressources:

terraform state list

Apply modifications on a specific ressource:

terraform apply -target="aws_eks_node_group.eks-worker[\"node-group\"]" -var-file=vars/dev.terraform.tfvars -auto-approve

Delete Terraform ressources:

terraform destroy -force

Exposing the content of a file as a viarable

locals {
  config = yamldecode(file("./config.yaml"))
}

Customize Terraform Error Messages

resource "aws_instance" "web" {
  # ...

  provisioner "local-exec" {
    command    = "echo The server's IP address is ${self.private_ip}"
    on_failure = fail("Custom error message")
  }
}

Terraform Cloud

Generate user token and put it in the ~/.terraformrc file like that:

credentials "app.terraform.io" {
  token = "$TF_CLOUD_TOKEN"
}

To debug

Set TF_LOG env variable:

export TF_LOG=TRACE

Test the output of an internal Terraform function:

# Drops you in an interactive shell
terraform console

Bonus

Format all your file following the same pattern:

terraform fmt

Visualizes the resource dependencies:

terraform graph | dot -Tsvg > graph.svg

Install autocompletion:

terraform -install-autocomplete

If you use TF in a CI system, set this env variable to adjusts TFs output to avoid suggesting specific commands to run next:

TF_IN_AUTOMATION: "true"