Cloudflare Workers - Blue / Green Deployments
This repo contains a minimal example from a proof of concept using the Serverless framework to enable blue / green deployments for Cloudflare Workers.
Configuration
The configuration is handled by dotenv
so the first thing you need to do is add a .env file to the root of the repo with the following environment variables:
Variable | Comment |
---|---|
CLOUDFLARE_ACCOUNT_ID | This should be set to your Cloudflare Account ID and can be found in the Overview page under the API section in the Cloudflare dashboard. |
CLOUDFLARE_ZONE_ID | This should be set to your Cloudflare Zone ID and can be found in the Overview page under the API section in the Cloudflare dashboard. |
CLOUDFLARE_AUTH_KEY | This should be set to your API key which you can find in the Cloudflare dashboard under the API section and then Get your API token -> API tokens tab -> Global API Key |
CLOUDFLARE_AUTH_EMAIL | This should be set to the email address you use to sign into your Cloudflare account. |
HOST | This should be set to the API host you want to use in your routes, i.e. api.somewhere.com. |
Note: You need to ensure you have the required DNS records and TLS certs configured. For example, if your HOST is api.somewhere.com, you need the following DNS A records for the somewhere.com domain to support api.somewhere.com, blue.api.somewhere.com and green.api.somewhere.com, your TLS cert should also have those domains as alternative names, or be a wildcard cert.
Installation
> npm install
Running
I've created some npm scripts to simplify the commands when wrapped with configuration. Replace somewhere.com with the host you configured in the .env file below:
Initial deploy to the default slot (blue):
> npm run deploy
> curl https://blue-api.somewhere.com/account
> "Processing account resource from blue slot..."
Activate the blue slot:
> npm run activate-slot:blue
> curl https://api.somewhere.com/account
> "Processing account resource from blue slot..."
Deploy to the other slot (green):
> npm run deploy
> curl https://green-api.somewhere.com/account
> "Processing account resource from green slot..."
Rotate to the other slot:
> npm run rotate
> curl https://api.somewhere.com/account
> "Processing account resource from green slot..."
Remove all slots/workers:
> npm run remove