Terraform Basics and Helpful Commands
Overview
These are just some quick recap notes and troubleshooting steps. There’s much more to terraform, but this is a quick basics overview, getting started guide and a short troubleshooting guide.
How does Terraform translate it’s resource definitions to the target cloud?
Terraform planning and applying
After applying, Terraform maintains a snapshot of the state of resources it provisioned. This is stored in a .tfstate
file and is used as the baseline for future plan
and apply
executions.
After a while of using Terraform, I realized, if I ever wanted to know if something had unintentionally changed in our infrastructure, I just needed to run plan
and see if Terraform intended to do anything.
If anything was changed intentionally, then it would have been in the source code and Terraform would not plan to do anything. However, if anyone changed any part of our AWS infrastructure manually, Terraform’s plan
would identify it and let us know.
In other words, if our AWS or GCP infrastructure drifted from its expected state, then Terraform’s plan
would detect it.
Local Setup with credentials (AWS , GCP Providers)
Google Provider (Download the json formatted key for your service account from Google’s Console. That’s what account.json refers to)
provider "google" {
credentials = file("account.json")
project = "my-project-id"
region = "us-central1"
}
AWS Provider
provider "aws" { version = "~> 2.0" region = "us-east-1" }
Validating, Planning and Applying
terraform validate terraform plan terraform apply
Formatting Code
terraform fmt
command is used to rewrite Terraform configuration files to a canonical format and style.
Targeting Specific Resources instead of ALL Resources in your TF module
terraform apply -target=google_storage_bucket.my_storage_bucket
Troubleshooting Debugging
Terraform apply can fail without giving a meaningful reason. To see the underlying reason, you have to enable TF_LOG = “debug”. On a windows machine, fire up a powershell prompt to do do. On a Linux box, a bash prompt will do the same.
From a powershell prompt, type $env:TF_LOG="config" Sometimes, your debug trace will show 'resource tainted'. To untaint tainted resources terraform untaint resourcename
String Concat
format("%s/%s",var.string,"string2")
Summary
These are just some quick recap notes and troubleshooting steps. There’s much more to terraform, but this is a quick basics overview, getting started guide and a short troubleshooting guide.
Need an experienced AWS/GCP/Azure Professional to help out with your Public Cloud Strategy? Set up a time with Anuj Varma.
Leave a Reply