Skip to content

Instantly share code, notes, and snippets.

@SarahFrench
Last active September 11, 2022 18:38
Show Gist options
  • Save SarahFrench/2d0ab79d1c6eee179b0fafd952bb4289 to your computer and use it in GitHub Desktop.
Save SarahFrench/2d0ab79d1c6eee179b0fafd952bb4289 to your computer and use it in GitHub Desktop.

Terraform configurations ranging from implicit to (more) explicit

Directory contents

For all the below, the directory before running terraform init contains only main.tf

my-terraform-project/
├─ main.tf

Config #1 : Using all the default behaviours

This is sufficient as a Terraform config for GCP, assuming you're supplying some environment variables:

  • Authentication, e.g. via a GOOGLE_CREDENTIALS environment variable.
  • Default values used for provisioning resources : GOOGLE_PROJECT, GOOGLE_REGION, GOOGLE_ZONE environment variables
# main.tf

resource "google_storage_bucket" "my-bucket" {
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
  }

Config #2 : Avoid defaulting to latest version of provider (adding version constraints)

# main.tf

terraform {
    required_providers {
        google = {
            version = "~>4.1.0"
        }
    }
}

resource "google_storage_bucket" "my-bucket" {
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
  }

Config #3: Avoid default of looking for the preferred local name (explicitly deciding how Terraform chooses which provider to use for a resource)

# main.tf

terraform {
    required_providers {
        # The official Google provider is using the preferred local name here
        # We could change it to hashicorp-google, but then all resources will need a provider meta-argument
        google = {
            source = hashicorp/google
            version = "~>4.1.0"
        }
        my-org-google = {
            source = my-org/google
            version = "=1.3.0"
        }
    }
}

resource "google_storage_bucket" "my-bucket" {
  # No provider argument needed here
  # We are knowingly using Terraform's default behaviour to look for a provider with a local name matching
  # the first word in the resource type at the top of this resource block
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
}

resource "google_storage_bucket" "bucket-made-with-my-provider" {
  provider      = my-org-google # local name from `required_providers` block 
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
    age = 3
    }
    action {
      type = "Delete"
    }
}

Config #3.5 : Exploring the concept of alias versus local name

# main.tf

terraform {
    required_providers {
        google = {
            source = hashicorp/google
            version = "~>4.1.0"
        }
        my-org-google = {
            source = my-org/google
            version = "=1.3.0"
        }
    }
}

# This is the default provider configuration
provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
  zone        = "us-central1-c"
}

provider "google" {
  alias       = "europe" # alias needed!
  project     = "my-project-id"
  region      = "europe-west2"
  zone        = "europe-west2-a"
}

resource "google_storage_bucket" "my-US-bucket" {
  # No provider argument needed here
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
}

resource "google_storage_bucket" "my-EU-bucket" {
  provider      = google.europe # <local name>.<alias>
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
}

resource "google_storage_bucket" "bucket-made-with-my-provider" {
  provider      = my-org-google
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
    age = 3
    }
    action {
      type = "Delete"
    }
}

Config #4 : Avoiding using the public Registry by default (setting the hostname in registry source address)

Note: the config below requires the terraform CLI to be configured with credentials to access the specific private registry.

There's a section in the documentation describing how to configure credentials for the CLI here.

# main.tf

terraform {
    required_providers {
        # The official Google provider is using the preferred local name here
        # We could change it to hashicorp-google, but then all resources will need a `provider` meta-argument
        google = {
            source = hashicorp/google
            version = "~>4.1.0"
        }
        my-org-google = {
            source = "app.terraform.io/my-org/google" # Using a private Registry hosted in Terraform Cloud
            version = "=1.3.0"
        }
    }
}

# This is the default provider configuration
provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
  zone        = "us-central1-c"
}

provider "google" {
  alias       = "europe" # alias needed!
  project     = "my-project-id"
  region      = "europe-west2"
  zone        = "europe-west2-a"
}

resource "google_storage_bucket" "my-US-bucket" {
  # No provider argument needed here
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
}

resource "google_storage_bucket" "my-EU-bucket" {
  provider      = google.europe
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
      age = 3
    }
    action {
      type = "Delete"
    }
}

resource "google_storage_bucket" "bucket-made-with-my-provider" {
  provider      = my-org-google
  name          = "auto-expiring-bucket"
  location      = "US"
  force_destroy = true

  lifecycle_rule {
    condition {
    age = 3
    }
    action {
      type = "Delete"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment