Terraform Reference

Kip Landergren

(Updated: )

Contents#

Commands#

Main:

terraform init
terraform validate
terraform plan
terraform apply
terraform destroy

Others:

terraform console
terraform fmt
terraform get
terraform import
terraform output
terraform show
terraform state

Help:

terraform init -help

terraform fmt#

terraform init#

-upgrade

for when you are upgrading a provider version

terraform init -upgrade

terraform validate#

Operators#

~ resource will be updated in-place
- resource will be destroyed
+ resource will be created
-/+ resource will be destroyed and recreated
+/- resource will be created and then destroyed

Resources#

Terminology#

argument#
1.

An assignment of a value to a name within a block.

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}
block#
1.

Containers for other content—typically representing the configuration of an object.

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}
CFT (Cloud Foundation Toolkit)#
1.
child module#
1.

Modules configured with module blocks.

create before destroy#
1.

+/-. spreads through the dependency tree to all resources directly or indirectly depended on.

data source#
1.
destroy#
1.
expression#
1.

A representation of a value.

<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK LABEL>" {
  # Block body
  <IDENTIFIER> = <EXPRESSION> # Argument
}
Fabric FAST#
1.

Part of cloud-foundation-fabric, which is:

  • a collection of Terraform modules that can be extended and reused
  • intended to be forked and maintained

Fabric FAST pulls together lessons learned from Google’s professional services division who essentially consult with companies on how to properly set up IAM and orgs and infra within GCP.

  • FAST is not an acronym: it just represents moving fast
  • broken into stages:
    • 0 where the org is set up
    • 1 where VPC is set up
    • 2 where networking and the rest
  • each stage
    • generates files that are consumed by the downstream stage
    • has dedicated service accounts attached to perform the operations
      • the idea here is that it limits scope of an operation to an SA with only permissions to perform that operation: no way for that SA to accidentally change more than intended

Stages are not one-off operations:

Think of them as the locks in a canal where you place your changes. The metaphor is a bit tortured but like if you need to make a change that affects everyone you need to make it at 0 (the highest) which flows down to all the other locks. And they themselves may need to process it so they also run plan and if non-empty they run apply.

landing zone#
1.

The core accounts, permissions, and metadata established with the infrastructure provider, often set on “Day 0”.

meta-argument#
1.

Argument that affects how Terraform controls the resource.

module#
1.

Reusable configuration component that define configuable collections of infrastructure.

module composition#
1.

The process of keeping the module hierarchy as flat as possible.

Example:

module "network" {
  source = "./modules/aws-network"

  base_cidr_block = "10.0.0.0/8"
}

module "consul_cluster" {
  source = "./modules/aws-consul-cluster"

  vpc_id     = module.network.vpc_id
  subnet_ids = module.network.subnet_ids
}
output#
1.

Data designated for display or availability for query.

registry#
1.
resource#
1.

“Anything that you can create, modify or delete from an infrastructure perspective...”

resource graph#
1.

The dependency graph of resources Terraform uses to determine what needs to happen sequentially and what can be parallelized.

root module#
1.

The Terraform configuration files in the workspace’s root directory. (?? is not defined with module blocks ??)

speculative plan#
1.

Running terraform plan to see what changes may happen (without necessarily intending to run terraform apply).

state file#
1.

Terraform’s source of truth for the environment. Defaults to local .tfstate file but can be configured for remote storage and access.

workspace#
1.

A Terraform configuration captured into a single state file. Multiple workspaces may be captured in a single working directory. Think of workspaces as 1:1 with states.

Env var: TF_WORKSPACE.

terraform-study#

learn-terraform-gcp/main.tf#

terraform {
  required_providers {
    google = {
      # shorthand for registry.terraform.io/hashicorp/google
      source  = "hashicorp/google"
      version = "7.35.0"
    }
  }
}

provider "google" {
  project = var.project
  region  = var.region
  zone    = var.zone
}

# resource "<resource type>" "<resource name>"
resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}

# order of resource declaration does not matter
resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"
  tags         = ["web", "dev"]

  boot_disk {
    initialize_params {
      image = "cos-cloud/cos-stable"
    }
  }

  network_interface {
    # use of name creates dependency graph
    network = google_compute_network.vpc_network.name
    access_config {
    }
  }
}

learn-terraform-gcp/outputs.tf#

output "ip" {
  value = google_compute_instance.vm_instance.network_interface.0.network_ip
}

learn-terraform-gcp/variables.tf#

variable "project" {}

variable "region" {
  default = "us-central1"
}

variable "zone" {
  default = "us-central1-c"
}