workers icon indicating copy to clipboard operation
workers copied to clipboard

Error deploying with Terraform

Open imjasonh opened this issue 2 years ago • 1 comments

I'm trying to get this (Very helpful! Thank you!) package to integrate with a Terraform workflow, instead of deploying using wrangler publish.

Here's what I've managed to get so far:

resource "terraform_data" "build" {
  # Changes to *.go files requires re-building it.
  input = {
    # https://stackoverflow.com/questions/51138667/can-terraform-watch-a-directory-for-changes
    dir_sha1 = sha1(join("", [for f in fileset("${path.module}", "*.go") : filesha1("${path.module}/${f}")]))
  }

  provisioner "local-exec" {
    command = <<EOC
go run github.com/syumai/workers/cmd/[email protected] -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .
EOC
  }
}

resource "cloudflare_worker_script" "script" {
  depends_on = [terraform_data.build]
  account_id = var.cloudflare_account_id
  name       = var.name
  content    = file("${path.module}/build/worker.mjs")

  // │ Error: error creating worker script: Wasm and blob bindings are not supported with modules; use imports instead (10021)
  module = false

  webassembly_binding {
    name   = "WASM"
    module = filebase64("./build/app.wasm")
  }
}

Refs:

  • https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/worker_script
  • https://developer.hashicorp.com/terraform/language/resources/terraform-data
  • https://developer.hashicorp.com/terraform/language/resources/provisioners/local-exec

This basically says "run the script to build app.wasm, then deploy the worker script with it as a binding". There's some quirks I still need to work out with TF lifecycle to ensure it builds before trying to take the filebase64, but that's not the bug I'm currently hitting.

The bug I'm hitting is that, whether or not I use the local-exec trick, deploying the script fails with:

╷
│ Error: error creating worker script: Uncaught SyntaxError: Cannot use import statement outside a module
│   at worker.js:1
│  (10021)
│ 
│   with cloudflare_worker_script.script,
│   on main.tf line 18, in resource "cloudflare_worker_script" "script":
│   18: resource "cloudflare_worker_script" "script" {
│ 

Digging around, it might be the case that the TF provider just doesn't support WASM workers, even though it claims to: https://github.com/cloudflare/terraform-provider-cloudflare/issues/2039

I guess I'm opening this in case anybody's gotten this to work with this package, or understands the error message well enough to give any hints about what might be happening.

imjasonh avatar Dec 30 '23 15:12 imjasonh