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:
projectName?:
routes?:
workerNamespace?:
env?:
auth:
auth Object
apiToken:
apiEmail?:
CFRoute Object
pattern:
zone_name:
custom_domain?:
Environment Variables
The CloudflareDeployer handles environment variables from multiple sources:
- Environment Files: Variables from
.env.production
and.env
files. - Configuration: Variables passed through the
env
parameter.
Lint Mastra Project
Lint your Mastra project to make sure it’s fine to build
npx mastra lint
Build Mastra Project
To build your Mastra project for cloudflare deployment:
npx mastra build
The build process generates the following output structure in the `.mastra/output` directory:
.mastra/output/ ├── index.mjs # Main worker entry point ├── wrangler.json # Cloudflare Worker configuration └── assets/ # Static assets and dependencies
### Wrangler Configuration
The CloudflareDeployer automatically generates a `wrangler.json` configuration file with the following settings:
```json
{
"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",
},
];
Deployment Options
After building, you can deploy your Mastra application .mastra/output
to Cloudflare Workers using any of these methods:
-
Wrangler CLI: Deploy directly using Cloudflare’s official CLI tool
- Install the CLI:
npm install -g wrangler
- Navigate to the output directory:
cd .mastra/output
- Login to your Cloudflare account:
wrangler login
- Deploy to preview environment:
wrangler deploy
- For production deployment:
wrangler deploy --env production
- Install the CLI:
-
Cloudflare Dashboard: Upload the build output manually through the Cloudflare dashboard
You can also run
wrangler dev
in your output directory.mastra/output
to test your Mastra application locally.