Cloudflare TypeScript Starter
A minimal Cloudflare TypeScript 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-typescriptmkdir my-project; cd my-projectpulumi new cloudflare-typescriptpulumi 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 language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.index.tscontains the program code that declares your cloud resources.
Take a moment to open them up and have a look. Here’s what’s in index.ts:
import * as pulumi from "@pulumi/pulumi";import * as cloudflare from "@pulumi/cloudflare";
// Import the program's configuration settings.const config = new pulumi.Config();const accountId = config.require("accountId");
const name = `${pulumi.getProject()}-${pulumi.getStack()}` .toLowerCase() .replace(/[^a-z0-9-]+/g, "-") .replace(/^-+|-+$/g, "");
// Create a Cloudflare resource (Workers KV namespace).const namespace = new cloudflare.WorkersKvNamespace("my-namespace", { accountId: accountId, title: name,});
// Export the ID of the namespace.export const 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 index.ts, add this below the namespace:
new cloudflare.WorkersKv("greeting", { 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