Terraform Tutorials - Table of Content (Beginner to Advanced, 60+ Hands-on Guides for AWS and Google Cloud)


This page is the index of every Terraform tutorial I have written on jhooq.com since 2021 - more than sixty of them. I have grouped them in the order in which I would learn Terraform myself if I were starting today - install it, create one EC2 instance, then variables, loops, modules, state, and finally the AWS and Google Cloud production topics.

All the posts are kept in sync with the current Terraform release (1.16 at the time of this update) and the current AWS provider (v6). Every post also has the matching YouTube video embedded, if you prefer to watch.

Table of Content

  1. Getting started with Terraform
  2. Variables, expressions and functions
  3. Resources, meta-arguments, data sources and provisioners
  4. Modules
  5. Terraform state - remote backend, locking, import and removal
  6. Terraform on AWS - IAM, security, secrets and real projects
  7. Terraform on Google Cloud
  8. Terragrunt, testing and on-premises
  9. Fixing the common Terraform and AWS provider errors


1. Getting started with Terraform

Start here if you have never written a .tf file. By the end of these posts you will have Terraform installed, credentials set up the right way, and one real server running in the cloud.

  1. Install Terraform on Ubuntu, CentOS, macOS, Windows, Fedora, Red Hat and Solaris
  2. Terraform create EC2 Instance on AWS - your first main.tf, init, plan, apply, destroy
  3. How to setup a Virtual Machine on Google Cloud Platform using Terraform
  4. Terraform and AWS credentials handling - the right ways to authenticate (no keys in code)
  5. How to split your Terraform main.tf into multiple files

2. Variables, expressions and functions

Everything about getting values into and out of your configuration, and transforming them along the way.

  1. Terraform variables explained - string, number, bool, list, map, object, validation, sensitive, precedence
  2. What is variables.tf and terraform.tfvars?
  3. How to use Terraform locals?
  4. How to use Terraform output values?
  5. Terraform count, for_each and for expressions - loop over lists, sets and maps
  6. How to use Terraform dynamic blocks?
  7. Terraform template - templatefile() and .tftpl, a complete guide
  8. Managing strings in Terraform - format, join, split, replace, regex
  9. Understanding Terraform escape sequences
  10. Mastering the Terraform lookup function for dynamic keys
  11. How to use a Terraform variable within a variable
  12. How to handle a null value with a default value in Terraform
  13. How to load input data from a file in Terraform (file, jsondecode, yamldecode, csvdecode)


3. Resources, meta-arguments, data sources and provisioners

How resources depend on each other, how to look up existing infrastructure, and how to run scripts on the machines you create.

  1. How to use Terraform resource meta-arguments? (count, for_each, depends_on, provider, lifecycle)
  2. How to use the terraform depends_on meta-argument?
  3. How to use Terraform data sources?
  4. In Terraform how to skip creation of a resource if it already exists?
  5. Terraform null_resource explained - triggers, local-exec, remote-exec, and terraform_data
  6. What is a Terraform provisioner? (file, local-exec, remote-exec)
  7. What is user_data in Terraform? (cloud-init startup scripts for EC2)
  8. Secure AWS EC2s and GCP VMs with Terraform SSH keys
  9. How to generate an SSH key in Terraform using tls_private_key?
  10. Copy files to EC2 and an S3 bucket using Terraform
  11. How to run a specific Terraform resource, module or target? (-target)
  12. Is terraform destroy needed before terraform apply?

4. Modules

Modules are how you stop copy-pasting the same forty lines into every project.

  1. How do Terraform modules work? (root module, child module, registry modules)
  2. How to reference a resource created by a Terraform module?
  3. Terraform - use module output variables as inputs for another module


5. Terraform state - remote backend, locking, import and removal

The state file is the one thing you must get right before working in a team. Read these in order.

  1. Managing Terraform state - local vs remote state, S3 backend, terraform_remote_state
  2. Terraform state locking using DynamoDB (and S3 native locking)
  3. Why you should not store the Terraform state file (.tfstate) inside a Git repository
  4. How to use workspaces in Terraform?
  5. How to import an existing resource using terraform import (and the import block)
  6. How to remove a resource from Terraform state? (state rm and the removed block)

6. Terraform on AWS - IAM, security, secrets and real projects

Once the basics are done, these are the posts people actually use at work.

  1. Terraform AWS IAM - create IAM users, roles and policies with aws_iam_policy_document
  2. Securing sensitive data in Terraform (sensitive variables, ephemeral values, state)
  3. Securing AWS secrets using HashiCorp Vault with Terraform
  4. Boost your AWS security with Terraform - a step-by-step guide
  5. Terraform and AWS multi-account setup (assume role, provider aliases)
  6. Terraform - a detailed guide on setting up an ALB (Application Load Balancer) and SSL
  7. Terraform guide - Docker containers and AWS ECR (Elastic Container Registry)
  8. Cloud-nuke - how to nuke AWS resources and save AWS infrastructure cost


7. Terraform on Google Cloud

The Google Cloud side of the house - from a single VM to the networking patterns every GCP organisation ends up needing, each with gcloud and Terraform versions of the steps.

  1. How to setup a Virtual Machine on Google Cloud Platform using Terraform
  2. Deploy a Next.js SaaS on Google Cloud Run with Terraform
  3. GCP VPC Peering - connect two VPC networks step by step
  4. GCP cross-project VPC Peering
  5. GCP Shared VPC - host and service projects
  6. GCP Private Service Connect
  7. GCP organization setup with Cloud Identity

8. Terragrunt, testing and on-premises

  1. How to use Terragrunt? (keep your Terraform DRY across environments)
  2. Testing Infrastructure as Code with Terraform (validate, plan, terraform test, Terratest)
  3. Can Terraform be used to provision on-premises infrastructure? (vSphere, Proxmox, Kubernetes, Docker)

9. Fixing the common Terraform and AWS provider errors

Every one of these came from my own terminal or from a reader's comment. Each post has the full error text, the cause and the fix.

  1. Error configuring Terraform AWS Provider - error validating provider credentials, sts GetCallerIdentity SignatureDoesNotMatch
  2. Error configuring S3 Backend - no valid credential sources for S3 Backend found
  3. Error acquiring the state lock - ConditionalCheckFailedException
  4. Value for undeclared variable - a variable named "..." was assigned on the command line
  5. Your query returned no results. Please change your search criteria and try again (aws_ami data source)
  6. Error creating EC2 Subnet - InvalidSubnet.Range, the CIDR is invalid
  7. InvalidParameter - security group and subnet belong to different networks
  8. Error creating IAM Role - MalformedPolicyDocument, has prohibited field Resource
  9. private-dns-enabled cannot be set because there is already a conflicting DNS domain
  10. Invalid function argument - provider credentials file, no file exists (Google provider)
  11. Error creating service account - googleapi Error 403, IAM API has not been used in project

If you are looking for the same kind of index for my other series - Kubernetes and Helm, Ansible and Docker each have one. And the official reference for everything above is the Terraform language documentation together with the AWS provider documentation on the Terraform Registry.