Install the Cloudflare Enforcer with Terraform
If your organization uses Cloudflare, you can use HUMAN’s Cloudflare Enforcer to protect against malicious behavior. The Cloudflare Enforcer is installed using a Cloudflare Worker, or a snippet of code, and is deployed to your content delivery network (CDN). The Enforcer dictates how traffic should be handled per your organization’s standards.
You can learn how to install the Cloudflare Enforcer using the Cloudflare Terraform provider with this guide.
For other installation options, see:
Prerequisites
- Terraform version 1.3 or higher. See Terraform’s documentation to install the latest version.
- On the machine running
terraform apply(not required whenuse_prebuilt = trueoruse_custom_worker = true):- Node Version Manager (nvm) installed on your device. See nvm’s GitHub repository to learn how to install it.
- The latest version of Node.js. After installing nvm, enter
nvm install stablein your CLI to install it. - Wrangler installed on your device. You can enter
npm install -g wranglerin your CLI to install it.
- Downloaded Cloudflare Terraform deployment files. Contact HUMAN support to get the files.
- Your Cloudflare account ID
- A Cloudflare API token with the following permissions:
- Workers Scripts: Edit
- Workers KV Storage: Edit
- Worker Routes: Edit
- Zone: Read (to resolve zone names to IDs)
- Your unique HUMAN information:
- Your Application ID. You can find this under Platform Settings > Applications > Overview in the HUMAN console. If you have multiple environments, you will also have multiple Application IDs, so be sure to choose the correct ID for the environment you want to install on.
- Your Server Token. You can find this under Platform Settings > Applications, then selecting an application and navigating to Application settings > Server token.
- Your Risk Cookie Key. You can find this under Bot Defender > Policies > Policy Settings > Policy Information.
File structure
The Terraform module uses the following files. Refer to the detailed file information for more on the highlighted files.
Detailed file information
Installation
First-time initialization
- Navigate to the Terraform
.gitignoredirectory:
- Create a new
terraform.tfvarsfile in your Terraform module directory, and ensureterraform.tfvarsor*.tfvarsis listed in.gitignoreso Git does not track it.
Never commit this file! Ensure to include it in .gitignore.
- Download the Terraform providers:
-
Commit the generated
.terraform.lock.hclto version control. This locks provider versions and ensures reproducible applies across machines and CI runs. -
If you have an existing Cloudflare Worker, KV namespace, or routes that were previously deployed, import them into the Terraform state:
- Preview the plan without making any changes:
Deployment
Standard deployment
CI/CD deployment
To deploy, run:
After deploying, Terraform will:
- Write
enforcer_configtoworker/src/config.json - Run
npm install, thenwrangler deploy --dry-runinsideworker/ - Create a KV namespace in your Cloudflare account
- Create (or update) the Worker and upload the built script
- Create the routes
When complete, you’ll see:
To see kv_namespace_id, run:
Update Enforcer configuration
You can update the Enforcer using the available configurations. To do so:
- Edit your
terraform.tfvarsfile with the appropriate configuration updates. - Run
terraform apply:
We recommend using use_prebuilt=true particularly when changing enforcer_config. When set to false, the null_resource rebuilds dist/index.js during terraform apply after the plan has already locked the file’s SHA-256. This causes a “provider inconsistency” error on the first run and requires a second terraform apply -var-file="terraform.tfvars" to complete. Using use_prebuilt = true avoids this by separating build from deploy.
Assuming only enforcer_config changed, Terraform will:
- Rewrite
worker/src/config.json. Changing the configuration hash always triggers a rebuild. - Re-run the wrangler build
- Upload a new Worker version and roll it out at 100%
No manual rebuild or Cloudflare console steps are needed.
Destroy
To remove all Cloudflare resources managed by this module, run:
The worker/dist/ directory and worker/node_modules/ are local build artifacts and aren’t removed by terraform destroy. Delete them manually if needed.
Detailed runtime flow
On subsequent runs, Terraform only rebuilds and re-uploads what actually changed. The build step is skipped unless enforcer_config, a Worker source file (src/*.ts), wrangler.toml, tsconfig.json, package.json, or package-lock.json has changed (all tracked via SHA-256 triggers).