Routing HTTP requests to different Cloudflare Workers
TypeScriptRouting HTTP requests to different Cloudflare Workers can be achieved by creating Worker Routes that specify patterns for the URLs which the Cloudflare Workers should handle. Each route pattern is associated with a specific Worker script that will be triggered when an HTTP request matches the pattern.
In the context of Pulumi and Cloudflare, you'll want to use the
cloudflare.WorkerRoute
andcloudflare.WorkerScript
resources to accomplish this. TheWorkerScript
resource is used to define the script logic that you want to run on Cloudflare's edge, while theWorkerRoute
resource maps that script to a given URL pattern.Here's how you can structure the Pulumi program to route HTTP requests to different Cloudflare Workers:
- Define the Worker scripts - this is where you put the JavaScript or other compatible code that is going to run when the route is triggered.
- Create the routes with patterns - these patterns determine what incoming requests will trigger each worker.
Now, let's go ahead and see a Pulumi TypeScript example that sets up two Cloudflare Worker scripts and routes HTTP requests to them based on the URL pattern.
import * as cloudflare from "@pulumi/cloudflare"; // Replace these values with your account details const accountId = "your-cloudflare-account-id"; const zoneId = "your-cloudflare-zone-id"; // Worker script #1 content const workerScriptContentOne = ` addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }); async function handleRequest(request) { // Your logic here return new Response('Worker One', { status: 200 }) } `; // Worker script #1 deployment const workerScriptOne = new cloudflare.WorkerScript("worker-script-one", { accountId: accountId, content: workerScriptContentOne, name: "worker-one", // An identifier for your worker script }); // Worker route #1 pointing to the first worker script const workerRouteOne = new cloudflare.WorkerRoute("worker-route-one", { zoneId: zoneId, pattern: "example.com/route-one/*", // Replace with your own domain and route scriptName: workerScriptOne.name, }); // Worker script #2 content const workerScriptContentTwo = ` addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }); async function handleRequest(request) { // Your logic here return new Response('Worker Two', { status: 200 }) } `; // Worker script #2 deployment const workerScriptTwo = new cloudflare.WorkerScript("worker-script-two", { accountId: accountId, content: workerScriptContentTwo, name: "worker-two", // An identifier for your worker script }); // Worker route #2 pointing to the second worker script const workerRouteTwo = new cloudflare.WorkerRoute("worker-route-two", { zoneId: zoneId, pattern: "example.com/route-two/*", // Replace with your own domain and route scriptName: workerScriptTwo.name, });
In this example:
- We are importing the necessary
cloudflare
package from Pulumi to interact with Cloudflare resources. - The
accountId
andzoneId
should be replaced with your actual account and zone identifiers from Cloudflare. - Two workers (
workerScriptOne
andworkerScriptTwo
) are defined with simple starter scripts that respond with a basic message when they are triggered. - Two corresponding routes (
workerRouteOne
andworkerRouteTwo
) are created which match different patterns and direct traffic to the respective scripts.
You'll need to replace
"example.com/route-one/*"
and"example.com/route-two/*"
with the actual domain and route patterns you're working with. When properly configured, these routes will match against the patterns specified and direct incoming requests to the appropriate worker scripts.For more detailed information on these resources and possible configurations, you can refer to the official Pulumi documentation:
Remember to run
pulumi up
to deploy the scripts and routes to your Cloudflare account after setting up the Pulumi program.