Skip to main content

Overview

Environments allow you to deploy the same Worker code to different configurations for development, staging, and production. Each environment can have different bindings, settings, and routes.
Environments enable you to test changes in isolation before promoting to production, using the same codebase with environment-specific configuration.

Environment Types

Cloudflare Workers supports two environment models: Named environments are explicitly defined and grouped under a service name. This is the modern, recommended approach.
wrangler.json
Deployment:

Service Environments (Legacy)

The older model where each environment is treated as a separate service. This approach is deprecated but still supported.
Service environments are legacy. Use named environments with defined_environments for new projects.

Environment Configuration

Top-Level Configuration

Settings defined at the top level apply to the default environment:
wrangler.json
Deploy with: wrangler deploy

Environment-Specific Overrides

Override any top-level setting per environment:
wrangler.json

Configurable Properties

These settings can be overridden per environment:
array
Routes that trigger this Worker (usually set for production only)
object
Environment variables (non-sensitive configuration)
array
KV namespace bindings (use separate namespaces per environment)
array
D1 database bindings
array
R2 bucket bindings
object
Durable Object bindings
array
Service bindings to other Workers
array
Analytics Engine bindings
string
Compatibility date for runtime behavior
array
Compatibility flags for specific features
object
Resource limits (CPU time, etc.)
object
Smart Placement configuration

Service Tags

When using named environments, Cloudflare automatically applies service tags to group environments together in the dashboard.

How Tags Work

wrangler.json
Deployments receive these tags:
  • Service tag: service:my-service
  • Environment tag: env:production or env:staging
These tags enable:
  • Grouped view in Cloudflare dashboard
  • Filtering and searching by service
  • Tracking deployments across environments
If no top-level name is defined, environments won’t be grouped. Always include a service name when using named environments.

Environment Patterns

Development, Staging, Production

wrangler.json

Preview Environments

Use separate preview bindings for local development:
wrangler.json
When running wrangler dev, the preview ID is used instead of production.

Feature Branches

Deploy feature branches to separate environments:
Add environments dynamically in CI/CD:
wrangler.json

Local Development

Development Server

Environment Selection

By default, wrangler dev uses:
  1. Top-level configuration
  2. preview_id for bindings when available
  3. Local mocks for KV, D1, etc.
With --env, it uses:
  1. Environment-specific overrides
  2. Merged with top-level config
  3. Preview or remote bindings

CI/CD Integration

GitHub Actions Example

.github/workflows/deploy.yml

Environment Variables in CI

Store secrets in CI/CD platform (GitHub Secrets, GitLab CI/CD variables):

Migration Guide

From Service to Named Environments

Before (Service Environments):
After (Named Environments):
Key changes:
  • Add defined_environments array
  • Remove name from environment config (inherited from top level)
  • All environments now grouped under service name

Best Practices

Use Named Environments

Always use defined_environments for new projects to benefit from dashboard grouping and better organization.

Isolate Resources

Use completely separate KV/D1/R2 resources for each environment to prevent data leakage.

Match Compatibility

Keep compatibility_date the same across environments unless testing runtime upgrades.

Test Before Production

Always deploy to staging first, verify functionality, then promote to production.

Troubleshooting

Bindings Not Found

Problem: Worker throws “binding not found” errors Solution: Check that environment-specific bindings are correctly defined:
Verify all bindings are listed in the output.

Wrong Environment Deployed

Problem: Changes appear in wrong environment Solution: Always specify --env flag:

Service Tags Missing

Problem: Environments not grouped in dashboard Solution: Ensure top-level name and defined_environments are set: