Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for cloudflare_worker_script resources being module syntax #1417

Closed
mekanoe opened this issue Jan 31, 2022 · 8 comments · Fixed by #1865
Closed

Support for cloudflare_worker_script resources being module syntax #1417

mekanoe opened this issue Jan 31, 2022 · 8 comments · Fixed by #1865
Labels
kind/enhancement Categorizes issue or PR as related to improving an existing feature. triage/accepted Indicates an issue or PR is ready to be actively worked on. workflow/pending-upstream-library Indicates an issue or PR requires changes from an upstream library.
Milestone

Comments

@mekanoe
Copy link

mekanoe commented Jan 31, 2022

Current Terraform and Cloudflare provider version

$ terraform -v
Terraform v1.1.4
on linux_amd64
+ provider registry.terraform.io/cloudflare/cloudflare v3.8.0

Description

Similar to the proposed upstream change (cloudflare/cloudflare-go#794), cloudflare_worker_script resource needs to be aware of Worker module syntax. The quick tl;dr: Workers only accepts module-based workers when uploaded with Content-Type: application/javascript+module header.

It seems most worth it to add a module boolean parameter, and pass through through to cloudflare-go.

Attempting to upload a valid worker without this sort of change results in a response:

{
  "code": 10021,
  "message": "Uncaught SyntaxError: Unexpected token 'export'\n  at line 1\n"
}

Use cases

Uploading workers scripts based on ES Modules, documented here: https://developers.cloudflare.com/workers/learning/migrating-to-module-workers

Potential Terraform configuration

resource "cloudflare_worker_script" "hello" {
  name    = "hello"
  content = "export default { async fetch() { return new Response('hello-world') } }"
  module  = true
}

References

@mekanoe mekanoe added kind/enhancement Categorizes issue or PR as related to improving an existing feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 31, 2022
@jacobbednarz jacobbednarz added triage/accepted Indicates an issue or PR is ready to be actively worked on. workflow/pending-upstream-library Indicates an issue or PR requires changes from an upstream library. and removed needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Jan 31, 2022
@sodabrew
Copy link

sodabrew commented Apr 12, 2022

My $0.02 suggestion:

Instead of:

resource "cloudflare_worker_script" "hello" {
  name    = "hello"
  content = "export default { async fetch() { return new Response('hello-world') } }"
  module  = true   # If false, then type worker is implied
}

How about:

resource "cloudflare_worker_script" "hello" {
  name    = "hello"
  content = "export default { async fetch() { return new Response('hello-world') } }"
  type    = "module" # default is "worker"
}

@moritonal
Copy link

moritonal commented Jul 1, 2022

Currently I have a fairy hacky start of a solution where I have to use npx wrangler publish --outdir dist --dry-run to build the compiled script, then use content = file("dist/index.js") in Terraform, only to get Uncaught SyntaxError: Unexpected token 'export' which actually means that the provider didn't add a header during upload.

What I've learnt is that this only fails when you've outputted a moduled Worker. If instead you build your worker using the old addEventListener method as shown here, then it works fine.

@js0cha
Copy link
Contributor

js0cha commented Jul 20, 2022

Hello ,

I am currently blocked by this. I am implementing the service worker bindings like instructed in the official docs but it fails in my terraform (Unexpected token 'export'). Do we have some trick to bypass this stupid error ? I am not using wrangler

@dotansimha
Copy link

Same here. Seems like the actual change the needs to be done is in the Content-Type.

Sending a regular worker code (event handler)

image

Content-Type is set to application/javascript

Sending a module-based worker

image

Content-Type is set to application/javascript+module

@dotansimha
Copy link

It seems like cloudflare/cloudflare-go#794 fixed this issue, and we might be able to use it here soon?

@sodabrew
Copy link

sodabrew commented Aug 18, 2022

@dotansimha it's halfway there -- uploading a module script works but downloading it returns a raw multipart/form-data body, which would put the work onto the caller to parse in order to compare the terraform state and determine whether the resource has changed. See cloudflare/cloudflare-go#1040 for my personal first effort at a solution, but there isn't an official direction on this yet, nor on how to handle a multiscript bundle.

@sodabrew
Copy link

cloudflare/cloudflare-go#1040 is landed! With cloudflare-go 0.49.0, it will be possible to both push a module-format worker script and also download one, enabling the terraform state refresh and comparison steps.

Multiple-script workers are still an open issue that isn't fully explored, but I probably won't be pushing through solving that.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

This functionality has been released in v3.23.0 of the Terraform Cloudflare Provider.

Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Categorizes issue or PR as related to improving an existing feature. triage/accepted Indicates an issue or PR is ready to be actively worked on. workflow/pending-upstream-library Indicates an issue or PR requires changes from an upstream library.
Projects
None yet
6 participants