Back to Mastra

Reference: CloudflareDeployer | Deployer

docs/src/content/en/reference/deployer/cloudflare.mdx

2025-12-182.3 KB
Original Source

CloudflareDeployer

The CloudflareDeployer bundles your Mastra server and generates a wrangler.jsonc file conforming to Cloudflare's wrangler configuration. Cloudflare deploys this as a Cloudflare Worker.

Installation

To use CloudflareDeployer, install the @mastra/deployer-cloudflare package:

bash
npm install @mastra/deployer-cloudflare@latest

Usage example

Import CloudflareDeployer and set it as the deployer in your Mastra configuration:

typescript
import { Mastra } from '@mastra/core'
import { CloudflareDeployer } from '@mastra/deployer-cloudflare'

export const mastra = new Mastra({
  deployer: new CloudflareDeployer({
    name: 'your-project-name',
    routes: [
      {
        pattern: 'example.com/*',
        zone_name: 'example.com',
        custom_domain: true,
      },
    ],
    vars: {
      NODE_ENV: 'production',
    },
    d1_databases: [
      {
        binding: 'DB',
        database_name: 'my-database',
        database_id: 'd1-database-id',
        preview_database_id: 'your-preview-database-id',
      },
    ],
    kv_namespaces: [
      {
        binding: 'CACHE',
        id: 'kv-namespace-id',
      },
    ],
  }),
})

Constructor options

The CloudflareDeployer constructor accepts the same configuration options as wrangler.jsonc. See the Wrangler configuration documentation for all available options.

Secrets

Environment variables from your .env file are not written to wrangler.jsonc. This prevents secrets from being committed to source control.

To make your .env secrets available to your Cloudflare Worker, upload them as Cloudflare Secrets:

bash
npx wrangler secret bulk .env

Use vars in the CloudflareDeployer constructor only for non-sensitive configuration values like NODE_ENV.

Build output

After running mastra build, the deployer generates a wrangler.jsonc file conforming to Cloudflare's wrangler configuration. It points to files inside .mastra/output so you need to run mastra build before deploying with Wrangler.