CloudflareDeployer

The CloudflareDeployer deploys Mastra applications to Cloudflare Workers, handling configuration, environment variables, and route management. It extends the abstract Deployer class to provide Cloudflare-specific deployment functionality.

Usage Example

import { Mastra } from '@mastra/core';
import { CloudflareDeployer } from '@mastra/deployer-cloudflare';
 
const mastra = new Mastra({
  deployer: new CloudflareDeployer({
    scope: 'your-account-id',
    projectName: 'your-project-name',
    routes: [
      {
        pattern: 'example.com/*',
        zone_name: 'example.com',
        custom_domain: true,
      },
    ],
    workerNamespace: 'your-namespace',
    auth: {
      apiToken: 'your-api-token',
      apiEmail: 'your-email',
    },
  }),
  // ... other Mastra configuration options
});

Parameters

Constructor Parameters

scope:

string
Your Cloudflare account ID.

projectName?:

string
= 'mastra'
Name of your worker project.

routes?:

CFRoute[]
Array of route configurations for your worker.

workerNamespace?:

string
Namespace for your worker.

env?:

Record<string, any>
Environment variables to be included in the worker configuration.

auth:

object
Cloudflare authentication details.

auth Object

apiToken:

string
Your Cloudflare API token.

apiEmail?:

string
Your Cloudflare account email.

CFRoute Object

pattern:

string
URL pattern to match (e.g., 'example.com/*').

zone_name:

string
Domain zone name.

custom_domain?:

boolean
= false
Whether to use a custom domain.

Wrangler Configuration

The CloudflareDeployer automatically generates a wrangler.json configuration file with the following settings:

{
  "name": "your-project-name",
  "main": "./output/index.mjs",
  "compatibility_date": "2024-12-02",
  "compatibility_flags": ["nodejs_compat"],
  "observability": {
    "logs": {
      "enabled": true
    }
  },
  "vars": {
    // Environment variables from .env files and configuration
  },
  "routes": [
    // Route configurations if specified
  ]
}

Route Configuration

Routes can be configured to direct traffic to your worker based on URL patterns and domains:

const routes = [
  {
    pattern: 'api.example.com/*',
    zone_name: 'example.com',
    custom_domain: true,
  },
  {
    pattern: 'example.com/api/*',
    zone_name: 'example.com',
  },
];

Environment Variables

The CloudflareDeployer handles environment variables from multiple sources:

  1. Environment Files: Variables from .env.production and .env files.
  2. Configuration: Variables passed through the env parameter.