DEV Community

Klee Thomas
Klee Thomas

Posted on

Config as code for Cloudflare Workers

In the previous post I went through adding a Cloudflare worker via the Cloudflare console. This is a great way to get started by we want to scale it up to something that can be a bit more repeatable. For this we need to move to a config as code style solution.

In this post I'll go through deploying the same worker code first from my local machine. Once that's working I'll move on to a what I really want, Continuous Integration and Continuous Deployment via Github Actions.

The first thing to do is to make sure Cloudflare is in a clean state after what I did in the previous post. This means I can avoid issues with overwriting existing rules.

Delete the old worker

Cloudflare provides a great tool [wrangler]( to help with managing workers using a config as code approach. This is installed as a global npm module.

Start by installing wrangler globally:

npm i @cloudflare/wrangler -g

Generate a project

Once wrangler is installed it can be used to scaffold out a sample project by running:

npx @cloudflare/wrangler generate deploy-sample [](

There are a range of sample projects available at for when you want to try your hand at something more complex than this example.

Adding the code

The template comes with some sample code in the index.js file. I'm going to copy in the sample code I used in the previous post. That code can be found in this Github Gist.

Configure wrangler

Before wrangler can interact with on my behalf I need to provide some configuration so that it knows what it's meant to be working against. This configuration is almost entirely in the wrangler.toml file. The API Key is stored elsewhere, I'll cover that in a bit.

The generated config file will look like this:

name = "helloworld"
type = "javascript"

account_id = ""
zone_id = ""

route = ""
Enter fullscreen mode Exit fullscreen mode

I've renamed mine to foo-stage-auth and left the type as javascript.

The account_id and zone_id need to be retrieved from the Cloudflare console. They can be found on the right hand side of the account overview page, towards the bottom.

Screen shot showing where account and zone id are found

The route is where you want this worker to run on your site. For my case I've set it to "*". Now my config looks like this:

name = "foo-stage-auth"
type = "javascript"

zone_id = "not really a secret but still obsfucated"
account_id = "not a secret but sill obsfucated"
route = "*"
Enter fullscreen mode Exit fullscreen mode

Authorising the local machine

The command to set up authorisation for wrangler is wrangler login. It will open a browser and ask you to authorise wrangler to act on your behalf. If it's unable to open a browser it will give you a link you can go to instead. The output will look like this:

$ wrangler login
Allow Wrangler to open a page in your browser? [y/n]
💁  Opened a link in your default browser:
Enter fullscreen mode Exit fullscreen mode

The request window will look like this

Cloudflare authorisation screen showing the permissions requested by Wrangler

Cloudflare screen showing permissions have been granted to Wrangler

Deploying from the local machine

With the local wrangler instance now the worker can be deployed. The command to deploy is: wrangler publish. The output will look like this:

> wrangler publish

✨  No build command specified, skipping build.
✨  Successfully published your script to* => created
Enter fullscreen mode Exit fullscreen mode

With this the worker is deployed and it can be seen in the Cloudflare console.

Cloudflare console showing the foo-stage-auth sample worker deployed

Using config as code is a big step up from deploying through the console, but it's not really good enough. In the next post I'll go through moving from deploying manually from one machine to deploying via continuous deployment with Github actions.

The code for this can be found on Github at this tag

Discussion (0)