« Back to Index

Fastly: Terraform Workspace example

View original Gist on GitHub

Tags: #fastly #terraform #hcl #workspace

1. README.md

The Terraform Workspace feature only provides state isolation and is designed for testing out changes (synonymous with git branch).

HashiCorp provides guidelines on when to use the Terraform Workspace.

terraform init
terraform workspace new stage
terraform workspace new prod
terraform workspace list
terraform workspace select stage

NOTE: you can’t terraform workspace delete default so best to just ignore it, or treat it as your ‘production’ instead of creating a prod workspace.

main.tf

terraform {
  required_providers {
    fastly = {
      source  = "fastly/fastly"
      version = "0.27.0"
    }
  }
}

variable "backend" {
  type    = string
  default = "httpbin.org"
}

resource "fastly_service_v1" "service" {
  name = "Example Service"

  domain {
    name = "www.example.com"
  }

  backend {
    address = "${terraform.workspace}.${var.backend}"
    name    = "${terraform.workspace == "prod" ? "www" : "staging"}-httpbin"
  }

  force_destroy = true
}