« Back to Index

Terraform: Fastly C@E Service

View original Gist on GitHub

Tags: #terraform #fastly #compute #serverless #edge

1. README.md

This is a dirty hack to get a locally changed file to be depended on by an external resource provided by the Fastly Terraform provider.

i.e. get Terraform to actual compile the wasm binary that would otherwise be manually produced using fastly compute build.

NOTE: The example is for an older version of the Fastly Terraform provider, a more recent version (e.g. 5.4.0 at the time of writing) uses a different setup (shown below) so you’ll need to tweak the older examples.

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

data "fastly_package_hash" "example" {
  filename = "./pkg/testing-fastly-tf-cli-deploys.tar.gz"
}

resource "fastly_service_compute" "testing-fastly-tf-cli-deploys" {
  name = "testing-fastly-tf-cli-deploys"

  domain {
    name = "testing-fastly-tf-cli-deploys.edgecompute.app"
  }

  package {
    filename         = "./pkg/testing-fastly-tf-cli-deploys.tar.gz"
    source_code_hash = data.fastly_package_hash.example.hash
  }

  force_destroy = true
}

main.tf

# https://www.terraform.io/docs/language/settings/index.html
terraform {
  # https://www.terraform.io/docs/language/providers/requirements.html
  required_providers {
    # https://registry.terraform.io/providers/fastly/fastly
    fastly = {
      source  = "fastly/fastly"
      version = "0.27.0"
    }
    # https://registry.terraform.io/providers/hashicorp/null
    null = {
      source = "hashicorp/null"
      version = "3.1.0"
    }
    # https://registry.terraform.io/providers/hashicorp/local
    local = {
      source = "hashicorp/local"
      version = "2.1.0"
    }
  }
}

# https://registry.terraform.io/providers/fastly/fastly/latest/docs/resources/service_compute
resource "fastly_service_compute" "test_service" {
  name = "compute_dictionary"

  domain {
    name    = "integralist-computehack.edgecompute.app"
    comment = "test-domain"
  }

  package {
    filename         = data.local_file.package_name.filename
    source_code_hash = sha512(data.local_file.package_name.content)
    
    # if I was just working with a standard pre-built package (i.e. a package I manually compiled) I'd use...
    #
    # filename         = "package-built-locally-via-cli.tar.gz"
    # source_code_hash = filesha512("package-built-locally-via-cli.tar.gz")
  }

  backend {
    address = "127.0.0.1"
    name    = "originless"
    port    = 80
  }

  force_destroy = true
}


# https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource
resource "null_resource" "build_package" {
  triggers = {
    package_name = "package.tar.gz"
  }
  
  # https://www.terraform.io/docs/language/resources/provisioners/local-exec.html
  provisioner "local-exec" {
    command = "fastly compute build" 
  }
}

# https://registry.terraform.io/providers/hashicorp/local/latest/docs/data-sources/file
data "local_file" "package_name" {
  filename = null_resource.build_package.triggers.package_name
}