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

`cloudflare_workers_custom_domain` requires environment however when deploying workers assets `environment` does not exist

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

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)

cloudflare_workers_custom_domain

Terraform configuration files

+ resource "cloudflare_workers_custom_domain" "worker-dev-worker-domain" {
                                      + account_id  = "******"
                                      + environment = "production"
                                      + hostname    = "static-site.example.com"
                                      + id          = (known after apply)
                                      + service     = "static-site"
                                      + zone_id     = "*******"
                                      + zone_name   = (known after apply)
                                    }

Link to debug output

Panic output

                            │ Error: failed to make http request
                            │ 
                            │   with cloudflare_workers_custom_domain.static-site,
                            │   90:       }
                            │ 
                            │ PUT
                            │ "https://api.cloudflare.com/client/v4/accounts/******/workers/domains":
                            │ 404 Not Found {
                            │   "result": null,
                            │   "success": false,
                            │   "errors": [
                            │     {
                            │       "code": 10092,
                            │       "message": "This environment does not exist on this Worker."
                            │     }
                            │   ],
                            │   "messages": []
                            │ }
                            │ 
                            ╵

Expected output

Custom domain on worker should be created successfully

Actual output

                            │ Error: failed to make http request
                            │ 
                            │   with cloudflare_workers_custom_domain.static-site,
                            │   90:       }
                            │ 
                            │ PUT
                            │ "https://api.cloudflare.com/client/v4/accounts/******/workers/domains":
                            │ 404 Not Found {
                            │   "result": null,
                            │   "success": false,
                            │   "errors": [
                            │     {
                            │       "code": 10092,
                            │       "message": "This environment does not exist on this Worker."
                            │     }
                            │   ],
                            │   "messages": []
                            │ }
                            │ 
                            ╵

Steps to reproduce

  1. Create a cloudflare worker assets using the steps here https://developers.cloudflare.com/workers/static-assets/get-started/#deploy-a-static-site. We use Terraform local exec to run npx wrangler deploy

  2. Use cloudflare_workers_custom_domain to create static site custom domain record

Additional factoids

No response

References

No response

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

Hey there! Thank you for raising this issue! In order for the maintainers to fully diagnose you issue we need the full, unmodified output from TF_LOG=DEBUG which contains important context for debugging.

Also was this reproducible on v5.5?

vaishakdinesh avatar May 28 '25 23:05 vaishakdinesh

I had the same error. You are likely missing environment = "production in your service binding.

{
      type        = "service",
      name        = "WORKER_SELF_REFERENCE",
      environment = "production"
      service     = local.worker_name
    },

alex-all3dp avatar May 30 '25 08:05 alex-all3dp

Never mind, I actually still see that error after running apply, even with that env in the binding and the cloudflare_workers_custom_domain

My setup:

resource "cloudflare_workers_script" "name" {
 ...
 bindings = [
    {
      type        = "r2_bucket"
      name        = "NEXT_INC_CACHE_R2_BUCKET"
      bucket_name = local.r2_bucket_name
    },
    {
      type        = "service",
      name        = "WORKER_SELF_REFERENCE",
      environment = "production"
      service     = local.worker_name
    },
    {
      type       = "durable_object_namespace"
      name       = "NEXT_CACHE_DO_QUEUE"
      class_name = local.do_queue_class
    },
    {
      type       = "durable_object_namespace"
      name       = "NEXT_TAG_CACHE_DO_SHARDED"
      class_name = local.do_sharded_class
    },
  ...
  ]
}

resource "cloudflare_workers_custom_domain" "craftcloud_worker_domain" {
  account_id  = module.cloudflare.cloudflare_account_id
  zone_id     = module.cloudflare.zone_id
  hostname    = local.app_domain
  service     = local.worker_name
  environment = "production"
}

alex-all3dp avatar May 30 '25 08:05 alex-all3dp

Cloudflare Worker "environments" are confusing. I was stuck on this, but I manually created a custom domain for a test deployment in the dashboard, and then used this API endpoint to discover the right service and environment values: https://developers.cloudflare.com/api/resources/workers/subresources/domains/methods/get/

FTR, I haven't created an explicit WORKER_SELF_REFERENCE binding.

ghost avatar Jun 22 '25 23:06 ghost

I have opened an issue internally to track this.

jhutchings1 avatar Jun 23 '25 21:06 jhutchings1

This issue hasn't been updated in a while. If it's still reproducing, please comment to let us know. Thank you!

github-actions[bot] avatar Aug 23 '25 16:08 github-actions[bot]

I'm still having this problem. Any updates on this?

Zollerboy1 avatar Oct 13 '25 14:10 Zollerboy1