Azure, GCP & Terraform IaC Quick Reference
Stop clicking through cloud portals. Define servers, networks, and storage in declarative Terraform HCL, and manage Microsoft Azure & Google Cloud (GCP) from your terminal.
1 Terraform Core Workflow
Terraform uses a predictable lifecycle. You write .tf files, initialize providers,
preview changes with plan, and execute with apply.
terraform init
terraform fmt -recursive
.tf files in the project.terraform validate
terraform plan -out=tfplan
+), updates (~), and destroys (-).terraform apply tfplan
terraform state list
terraform.tfstate.terraform destroy
yes confirmation.Remember the golden order: Init โ Fmt โ Validate โ
Plan โ Apply โ (Destroy) โ State.
Never run apply without reading the plan output first!
2 HCL Block Anatomy & State File
Terraform files use HCL (HashiCorp Configuration Language). Almost every configuration
is built from five core block types plus the terraform.tfstate tracking file.
๐ provider
Configures the cloud plugin (e.g., provider "azurerm" or
provider "google") with region/project settings.
๐งฑ resource
Declares a real cloud object to create and own (VMs, buckets, VPCs, databases).
๐ data
Read-only lookup of an existing resource you did not create in this config (e.g., latest Ubuntu AMI).
๐ฅ variable
Input parameters (var.env) so you can reuse the same code for dev, staging, and
prod.
๐ค output
Prints useful values after apply (e.g., the VM's public IP or load balancer DNS).
๐บ๏ธ terraform.tfstate
JSON map linking your HCL names to real cloud IDs. Store remotely (S3 / Azure Blob / GCS) in teams!
variable "location" {
default = "eastus"
}
resource "azurerm_resource_group" "app" {
name = "rg-shekharit-dev"
location = var.location
}
output "rg_id" {
value = azurerm_resource_group.app.id
}
3 Azure CLI & Resource Hierarchy
Azure organizes everything in a strict 4-level tree. Every resource must live inside a Resource Group, which lives inside a Subscription.
az login
az account list --output table
az group create --name rg-lab --location eastus
az vm create -g rg-lab -n web01 --image Ubuntu2204 --generate-ssh-keys
az storage account list --output table
az group delete --name rg-lab --yes --no-wait
4 GCP gcloud CLI Essentials
Google Cloud Platform uses the hierarchy Organization โ Folder โ Project โ Resource.
Almost every gcloud command operates inside your active Project.
gcloud auth login
gcloud config set project my-gcp-project-id
--project.gcloud compute instances list
gcloud storage ls gs://my-gcs-bucket/
gsutil ls to browse GCS buckets.gcloud storage cp ./app.zip gs://my-gcs-bucket/releases/
5 Danger Zone: IaC & Multi-Cloud Traps
terraform.tfstate or .tfvars Secrets to Git
terraform.tfstate stores database passwords, API keys, and private IPs in
plaintext JSON. Always add *.tfstate* and
*.tfvars (except example files) to .gitignore, and use an
encrypted remote backend.
terraform destroy Without Review or -target
terraform destroy tears down every resource in your state fileโincluding
production databases. If you only need to remove one resource, use
terraform plan -destroy -target=aws_instance.test first, or remove the block
from HCL and run terraform apply.
Changing firewall rules or VM sizes in the AWS/Azure/GCP console after Terraform created
them causes state drift. Next time someone runs terraform apply,
Terraform may overwrite or destroy your manual fix.
az group delete is an atomic nukeโit deletes every VM, disk, database, and
storage account inside that group. Double-check az account show and the
Resource Group name before confirming.