Cloudflare YAML Starter
A minimal Cloudflare YAML Pulumi program
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- 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-yamlmkdir my-project; cd my-projectpulumi new cloudflare-yamlpulumi 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 |
|---|---|---|
| accountId | The Cloudflare account ID to deploy into | — |
pulumi config set
:
pulumi config set accountId <value>pulumi config set accountId "<value>"Review the code#
When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the resources it declares.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.
Take a moment to open them up and have a look. Here’s what’s in Pulumi.yaml:
name: my-projectdescription: A minimal Cloudflare YAML Pulumi programruntime: yamlconfig: accountId: type: string
variables:
name: fn::invoke: function: std:trim arguments: input: fn::invoke: function: std:replace arguments: text: fn::invoke: function: std:lower arguments: input: ${pulumi.project}-${pulumi.stack} return: result search: /[^a-z0-9-]+/ replace: "-" return: result cutset: "-" return: result
resources:
# Create a Cloudflare resource (Workers KV namespace). namespace: type: cloudflare:index/workersKvNamespace:WorkersKvNamespace properties: accountId: ${accountId} title: ${name}
outputs:
# Export the ID of the namespace. namespaceId: ${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 accountId)/storage/kv/namespaces/$(pulumi stack output namespaceId)curl.exe -H "Authorization: Bearer $env:CLOUDFLARE_API_TOKEN" "https://api.cloudflare.com/client/v4/accounts/$(pulumi config get accountId)/storage/kv/namespaces/$(pulumi stack output namespaceId)"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 Pulumi.yaml, add this below the namespace:
resources: # ... greeting: type: cloudflare:index/workersKv:WorkersKv properties: accountId: ${accountId} namespaceId: ${namespace.id} keyName: 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 accountId)/storage/kv/namespaces/$(pulumi stack output namespaceId)/values/greetingcurl.exe -H "Authorization: Bearer $env:CLOUDFLARE_API_TOKEN" "https://api.cloudflare.com/client/v4/accounts/$(pulumi config get accountId)/storage/kv/namespaces/$(pulumi stack output namespaceId)/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