I'm confused about the intended function of Cloudflare's Worker Environments.
In the CloudFlare dashboard, a worker has an environment dropdown, which defaults to "Production". I thought that by leveraging the environments in my Wrangler file, I would have a single worker, but with multiple environments. However, what ended up happening was I just had two workers, with the environment added at the end (my-worker-dev and my-worker-prod). Each of these workers has 1 environment (the Production environment).
I'm not sure if I'm doing something wrong or just misunderstanding the intended behavior.
Can someone help me understand the difference between how wrangler just adds a different name and the "Environment" dropdown within a single worker/service?
My wrangler.toml file
name = "my-worker"
type = "javascript"
account_id = "<redacted>"
workers_dev = true
compatibility_date = "2021-12-10"
[env.dev]
vars = { ENVIRONMENT = "dev" }
kv_namespaces = [
{ binding = "TASKS", id = "<redacted>", preview_id = "<redacted>" }
]
[env.prod]
vars = { ENVIRONMENT = "prod" }
kv_namespaces = [
{ binding = "TASKS", id = "<redacted>", preview_id = "<redacted>" },
]
[build]
command = "npm install && npm run build"
[build.upload]
format = "modules"
dir = "dist"
main = "./worker.mjs"
I think there is currently some disconnect / confusion between the meaning of "environments" as defined in the new Dashboard functionality, and the pre-existing wrangler "environment" support.
For the Dashboard / Web UI, you define a "Service" which has multiple workers grouped under it (one per environment). This allows "promoting" a worker from one environment to another (essentially copying the script, but having separate variables and routes).
There is separate documentation for this functionality - https://developers.cloudflare.com/workers/learning/using-services#service-environments
Wrangler "environments", as you've seen, work differently. Simply creating one top-level "production" worker / service (named for the environment). The good news is (according to the docs above) it sounds like Cloudflare will be updating wrangler to support the new Dashboard type environments:
As of January 2022, the dashboard is the only way to interact with Workers Service environments. Support in Wrangler is coming in v2.1