Tutorial: deploy a Cloudflare Worker with MultiTool
This tutorial walks through rolling out a simple Cloudflare Worker. You’ll simulate user traffic to the API, and the MultiTool agent will automatically decide whether to promote or roll out based on the observed error rate.
You will:
- Create and package sample Worker code
- Deploy to Cloudflare
- Connect to MultiTool and run a canary deployment
Tools
-
Cloudflare Workers- to run sample server code
-
Cloudflare Workers Observability - to read metrics used by MultiTool during rollout
-
MultiTool- to automate safe deployments
Prerequisites
This tutorial is compatible with macOS and Linux systems. For Windows users, we recommend using Windows Subsystem for Linux (WSL) .
- A free MultiTool account
-
A Cloudflare account and token with read permission for Workers Observability and edit permission for Workers Scripts
-
Cloudflare Wrangler CLI installed
- Create an Cloudflare API Token with read permission for Workers Observability and edit permission for Workers Scripts
- Run
npx wrangler loginand follow the prompts to login to Cloudflare
-
MultiTool CLI installed- Run
multi loginto authenticate
Step 1: Create and package the Worker code
Create a new "Hello World" Cloudflare Worker
npm create cloudflare@latest -- multitool-quickstart --type hello-world --lang js --no-git -y true
Step 2: Create and package the Worker code
This tutorial simulates two versions of a Worker:
- A “healthy” version that always returns a
200HTTP status code - A “buggy” version that randomly fails with a
400HTTP status code 50% of the time
The rest of this tutorial will take place inside the multitool-quickstart directory that was created, so we'll need to enter that directory:
cd multitool-quickstart
Then, we'll overwrite the index.js file and add a new file for the buggy version:
Create the healthy version
This version always returns a 200 HTTP status code response.
cat << EOF > src/index.js
export default {
async fetch(request, env, ctx) {
return new Response('Hello World!', { status: 200 });
},
};
EOF
Create the buggy version
This version introduces a simulated bug by returning a 400 HTTP status code 50% of the time.
cat << EOF > src/index_errors.js
export default {
async fetch(request, env, ctx) {
const rand = Math.random();
return new Response(rand < 0.5 ? 'Bad Request' : 'Hello World!', { status: rand < 0.5 ? 400 : 200 });
},
};
EOF
Step 3: Create the Worker
Before we deploy the worker, we'll need to add your Cloudflare Account ID to your wrangler.jsonc file. To do that, create a new key called account_id:
...
"account_id": "MY_CLOUDFLARE_ACCOUNT_ID"
To get your Cloudflare Account ID, follow CloudFlare's instructions .
Next, we can deploy the Worker using Wrangler:
npx wrangler deploy
Store the URL, replacing MY_ACCOUNT_URL:
MY_URL="https://multitool-quickstart.[MY_ACCOUNT_URL].workers.dev"
Save the URL to a file for later:
cat << EOF > url.txt
$MY_URL
EOF
Step 4: Test that the Worker is accepting traffic
Before moving on, make sure the Worker returns the expected response.
curl $MY_URL
You should see:
Hello World!
Step 5: Connect the application to MultiTool
Now that the Worker is deployed and accessible via its URL, create the application in MultiTool.
From the MultiTool app:
- Create a workspace
- Create an application named
quickstart
After the application is set up, login to the MultiTool CLI, if needed:
multi login
Step 6: Add your configuration file
Now that your workspace and application are set up in the MultiTool app, you’ll create a MultiTool.toml file. This file tells the CLI how to deploy your application.
If you’ve been using the sample values from this tutorial, you can use the configuration below. Be sure to replace the placeholder values with your actual workspace name, and application name.
cat << EOF > MultiTool.toml
workspace = "MY_WORKSPACE_NAME"
application = "quickstart"
[config.cloudflare]
worker-name = "multitool-quickstart"
project-dir = "src/"
EOF
Step 7: Deploy healthy code and simulate stable traffic
Exiting the terminal before a CLI operation finishes can leave your deployment in a stuck state for a few minutes. Please wait for the operation to complete before closing the terminal. If you've already run into this issue, contact support@multitool.run and we’ll help resolve it.
To deploy the healthy version, use index.js as the main value in your wrangler.jsonc file:
...
"main": "src/index.js"
Then run MultiTool:
multi run --cloudflare-api-token $MY_CLOUDFLARE_TOKEN
In a separate terminal window, load the public URL from Step 6 to use in the next step:
MY_URL=$(cat url.txt)
Simulate traffic to the worker using one of these options:
Option A: Using curl
for i in $(seq 1 2000);do echo -n "Request $i completed with status: ";code=$(curl -s -o /dev/null -w "%{http_code}" "$MY_URL");echo "$code";sleep 1;done
Option B: Using Bombardier
bombardier -c 1 -n 2000 ${MY_URL}
As traffic hits the new version, MultiTool will evaluate its behavior and promote it to 100% traffic once it confirms stability.
Step 8: Deploy buggy code and simulate errors
To test a broken rollout, use the index_errors.ts file.
Update the main value in your wrangler.jsonc file to index_errors.js:
...
"main": "src/index_errors.js"
Then re-run MultiTool:
multi run --cloudflare-api-token $MY_CLOUDFLARE_TOKEN
In a separate terminal window, load the public URL from Step 6 to use in the next step:
MY_URL=$(cat url.txt)
Simulate traffic to the Worker using one of these options:
Option A: Using curl
for i in $(seq 1 2000);do echo -n "Request $i completed with status: ";code=$(curl -s -o /dev/null -w "%{http_code}" "$MY_URL");echo "$code";sleep 1;done
Option B: Using Bombardier
bombardier -c 1 -n 2000 ${MY_URL}
MultiTool will detect the increase in errors and automatically trigger a rollback.
Step 9: Cleanup
After you've tested MultiTool, be sure to clean up the demo resources created as part of this guide.
Delete the Worker:
npx wrangler delete multitool-quickstart