Cloudflare HCL Starter
A minimal Cloudflare Pulumi HCL program
Before you begin#
For this walkthrough, you'll need:
- A current version of the Pulumi CLI (Pulumi HCL requires no additional runtime)
- An API token that can edit Workers KV
Create a new project#
This starter creates a Workers KV namespace in your Cloudflare account and exports the namespace’s ID.
mkdir my-project; cd my-projectpulumi new cloudflare-hclmkdir my-project; cd my-projectpulumi new cloudflare-hclpulumi new walks you through creating and configuring a new project and stack. A stack is an instance of a project and you can have many of them (dev, staging, prod), each with its own configuration settings.
You’ll also be asked for your Cloudflare account ID:
| Key | Description | Default |
|---|---|---|
| account_id | The Cloudflare account ID to deploy into | — |
pulumi config set
:
pulumi config set account_id <value>pulumi config set account_id "<value>"Review the code#
When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.main.tfcontains the program code that declares your cloud resources.
Take a moment to open them up and have a look. Here’s what’s in main.tf:
terraform { required_providers { cloudflare = { source = "pulumi/cloudflare" } }}
variable "account_id" { description = "The Cloudflare account ID to deploy into" type = string}
locals { name = trim(replace(lower("${pulumi.project}-${pulumi.stack}"), "/[^a-z0-9-]+/", "-"), "-")}
# Create a Cloudflare resource (Workers KV namespace)resource "cloudflare_workers_kv_namespace" "namespace" { account_id = var.account_id title = local.name}
# Export the ID of the namespaceoutput "namespace_id" { value = cloudflare_workers_kv_namespace.namespace.id}Deploy the project#
Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.
Namespace titles must be unique in an account, so the program titles the namespace after the project and stack: my-project-dev for the dev stack of my-project. Each stack gets its own namespace.
pulumi uppulumi upWhen it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the namespace’s ID. Read it any time with pulumi stack output <name>, or pass it to another command. With your API token in CLOUDFLARE_API_TOKEN, this one asks the Cloudflare API for the namespace and prints it, title included:
curl -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ https://api.cloudflare.com/client/v4/accounts/$(pulumi config get account_id)/storage/kv/namespaces/$(pulumi stack output namespace_id)curl.exe -H "Authorization: Bearer $env:CLOUDFLARE_API_TOKEN" "https://api.cloudflare.com/client/v4/accounts/$(pulumi config get account_id)/storage/kv/namespaces/$(pulumi stack output namespace_id)"Need help?
If pulumi up reports Authentication error, your API token is missing or invalid. Set CLOUDFLARE_API_TOKEN again, since it only lasts for the terminal session you set it in:
export CLOUDFLARE_API_TOKEN="<YOUR_API_TOKEN>"$env:CLOUDFLARE_API_TOKEN = "<YOUR_API_TOKEN>"Still stuck? Let us know.
Customize the project#
Store a value in the namespace. In main.tf, add this below the namespace:
resource "cloudflare_workers_kv" "greeting" { account_id = var.account_id namespace_id = cloudflare_workers_kv_namespace.namespace.id key_name = "greeting" value = "Hello, Pulumi!\n"}Deploy again. The preview shows one key to create:
pulumi uppulumi upThen read the value back from the Cloudflare API. It prints Hello, Pulumi!:
curl -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ https://api.cloudflare.com/client/v4/accounts/$(pulumi config get account_id)/storage/kv/namespaces/$(pulumi stack output namespace_id)/values/greetingcurl.exe -H "Authorization: Bearer $env:CLOUDFLARE_API_TOKEN" "https://api.cloudflare.com/client/v4/accounts/$(pulumi config get account_id)/storage/kv/namespaces/$(pulumi stack output namespace_id)/values/greeting"To see what else you can add, browse the Cloudflare provider in the Pulumi Registry.
Cleaning up#
You can destroy the stack and all of its infrastructure with pulumi destroy, then remove the stack with pulumi stack rm:
pulumi destroypulumi stack rmpulumi destroypulumi stack rm