This cheat sheet is possible thanks to Kodekloud’s course
Terraform uses HCL (HashiCorp Configuration Language) which is a domain-specific language (DSL) and a declarative language used to define and configure infrastructure resources
Check Terraform’s docs about HCL to know more
When including a new provider, be sure to re-run terraform init
terraform.tfstate
is the state file, which contains the complete record of the infrastructure created by Terraform and works as the single source of truth
Check this guide to create a remote state setup for the backend
terraform_remote_state
data source to retrieve the root module output values from some other Terraform configurationThere can be constraints in provider versions, so make sure you’re using a consistent version. To be sure of the version you can do the following example which finds a version between 1.2.0 and 2.0.0 except for 1.4.0
terraform {
required_providers {
local = {
source: "hashicorp/local"
version = "> 1.2.0, < 2.0.0, != 1.4.0"
}
}
}
You can group or isolate mechanism using alias
(namespace) for resources
// provider.tf
provider "aws" {
region = "ca-central-1"
alias = "central"
}
// main.tf
resource "aws_key_pair" "alpha" {
key_name = "alpha"
public_key = "..."
provider = aws.central // <provider_name>.<alias_name>
}
A list (or tuple) is a sequence of values which can be of any type mixed together
A set is a collection of unique values
You can change log level when doing terraform plan or apply using export TF_LOG=TRACE
info
, warning
, error
, debug
, trace
export TF_LOG_PATH=/tmp/terraform.log
Terraform Cloud is a platform that helps with managing your Terraform code and collaboration between developers which simplifies workflow and improves security
terraform destroy
usually takes more time than terraform apply
because destroy
happens in reverse dependency order where Terraform carefully untangles everything it built
It’s best practice to use prevent_destroy = true
on critical resources to prevent accidental deletion
If state file got corrupted, restore from backup or use terraform import
to bring existing resources back under management. And always enable versioning on remote state storage to prevent data loss
To migrate from one backend to another run terraform state pull
to save current state, update backend configuration, then terraform init -migrate-state
Store sensitive values or secrets in external systems like HashiCorp Vault or AWS Secrets Manager. Use sensitive = true
attribute and ensure remote state is encrypted at rest