terraform-provider-cloudflare icon indicating copy to clipboard operation
terraform-provider-cloudflare copied to clipboard

cloudflare_workers_script Unable to deploy worker assets

Open hemalshah-gradientedge opened this issue 6 months ago • 2 comments
trafficstars

Confirmation

  • [x] This is a bug with an existing resource and is not a feature request or enhancement. Feature requests should be submitted with Cloudflare Support or your account team.
  • [x] I have searched the issue tracker and my issue isn't already found.
  • [x] I have replicated my issue using the latest version of the provider and it is still present.

Terraform and Cloudflare provider version

terraform v1.11.4 Cloudflare provider v5.4.0

Affected resource(s)

I a attempting to create static site using workers as defined here https://developers.cloudflare.com/workers/static-assets/get-started/#deploy-a-static-site. I am able to follow the steps successfully from the Cloudflare document in that link.

However when using cloudflare_workers_script I'm unable to do this and get errors. Would you be able to share the correct configuration to be able to successfully deploy a static site on workers?

Terraform configuration files

+ resource "cloudflare_workers_script" "static-site" {
                                      + account_id         = "***"
                                      + assets             = {}
                                      + bindings           = []
                                      + compatibility_date = "2025-05-22"
                                      + content            = "public"
                                      + created_on         = (known after apply)
                                      + etag               = (known after apply)
                                      + has_assets         = (known after apply)
                                      + has_modules        = (known after apply)
                                      + id                 = (known after apply)
                                      + logpush            = (known after apply)
                                      + modified_on        = (known after apply)
                                      + script_name        = "static-site"
                                      + startup_time_ms    = (known after apply)
                                      + usage_model        = (known after apply)
                                    }

Link to debug output

Panic output

                            │ Error: Value Conversion Error
                            │ 
                            │   on cdk.tf.json line 102, in resource.cloudflare_workers_script.static-site:
                            │  102:           }
                            │ 
                            │ An unexpected error was encountered trying to convert tftypes.Value into
                            │ workers_script.WorkersScriptMetadataAssetsConfigModel. This is always an
                            │ error in the provider. Please report the following to the provider developer:
                            │ 
                            │ error retrieving field names from struct tags: assets.config._headers:
                            │ invalid tfsdk tag, must only use lowercase letters, underscores, and numbers,
                            │ and must start with a letter

Expected output

Successfully create worker static site

Actual output

                            │ Error: Value Conversion Error
                            │ 
                            │   on cdk.tf.json line 102, in resource.cloudflare_workers_script.static-site:
                            │  102:           }
                            │ 
                            │ An unexpected error was encountered trying to convert tftypes.Value into
                            │ workers_script.WorkersScriptMetadataAssetsConfigModel. This is always an
                            │ error in the provider. Please report the following to the provider developer:
                            │ 
                            │ error retrieving field names from struct tags: assets.config._headers:
                            │ invalid tfsdk tag, must only use lowercase letters, underscores, and numbers,
                            │ and must start with a letter

Steps to reproduce

  1. configure with name and content of a static folder
  2. deploy

Additional factoids

No response

References

No response

hemalshah-gradientedge avatar May 22 '25 12:05 hemalshah-gradientedge

I'm not from Cloudflare. I've just tried to solve the same problem.

Static assets are handled separately from script code in the CF API. There's a separate process for uploading static assets, the TL;DR being

  1. Negotiate static asset upload in a series of API calls
  2. Get a "completion token" as a result
  3. Include the completion token when uploading the worker as normal

The cloudflare_workers_script resource only covers the final step. The docs above have some sample code for the whole process.

An annoying complication is that the static asset upload API doesn't involve any persistent entity (like an "asset bundle"). The completion token doubles as a reference to the uploaded assets and as an authentication token for accessing them, and it expires in an hour. I think I'll have to try to adapt the code from the docs to work as an external data source.

I can think of a few ways it could be implemented in the provider:

  • Building the static asset process into the cloudflare_workers_script resource as an implementation detail
  • Creating a separate data source for static asset upload completion tokens
  • Creating a separate resource instead, with the completion token made ephemeral

ghost avatar May 30 '25 01:05 ghost

I successfully deployed static assets using cloudflare_workers_script, but had to work around a couple more issues.

  1. Using an external data source to run a script to get the completion token worked. One "problem" is that assets are (idempotently) uploaded at plan time. A resource could be smarter.
  2. The invalid tfsdk tag issue still happens on apply. It's from the Terraform plugin SDK, which requires tfsdk tags to start with a lowercase letter. Forking the provider and removing the underscore from tfsdk (but not json) in these lines fixed it: https://github.com/cloudflare/terraform-provider-cloudflare/blob/c7df32b837d5d9204ef97a6dc3408ae770f97d58/internal/services/workers_script/model.go#L105-L106
  3. After that I had a serve_directly and run_worker_first must not be simultaneously specified error from the Cloudflare API on apply. Neither of these values were set in my original config, but the provider sets defaults. Whatever the config, they always both get sent because they're typed as plain boolean and can only be true or false (not missing). I just removed the (deprecated) serve_directly from my fork, but I guess the OpenAPI spec needs a oneOf or something.

I'd submit some PRs, but the fixes would need to be in the Stainless API code generator and the OpenAPI spec, which don't seem to be open.

ghost avatar Jun 02 '25 04:06 ghost

It is part of TF provider 5.8.2

rupalims avatar Aug 01 '25 23:08 rupalims