Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

Cloudflare C# Starter

A minimal Cloudflare C# Pulumi program

Before you begin#

For this walkthrough, you'll need:

Create a new project#

This starter creates a Workers KV namespace in your Cloudflare account and exports the namespace’s ID.

Terminal window
mkdir my-project; cd my-project
pulumi new cloudflare-csharp

pulumi 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:

KeyDescriptionDefault
accountIdThe Cloudflare account ID to deploy into—
You can change values like these later with pulumi config set :
Terminal window
pulumi config set accountId <value>

Review the code#

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • Program.cs contains the program code that declares your cloud resources.

Take a moment to open them up and have a look. Here’s what’s in Program.cs:

Program.cs
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Pulumi;
using Cloudflare = Pulumi.Cloudflare;
return await Deployment.RunAsync(() =>
{
// Import the program's configuration settings.
var config = new Config();
var accountId = config.Require("accountId");
var name = Regex.Replace(
$"{Deployment.Instance.ProjectName}-{Deployment.Instance.StackName}".ToLowerInvariant(),
"[^a-z0-9-]+", "-").Trim('-');
// Create a Cloudflare resource (Workers KV namespace).
var ns = new Cloudflare.WorkersKvNamespace("my-namespace", new()
{
AccountId = accountId,
Title = name,
});
// Export the ID of the namespace.
return new Dictionary<string, object?>
{
["namespaceId"] = ns.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.

Terminal window
pulumi up

When 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:

Terminal window
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)
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:

Terminal window
export CLOUDFLARE_API_TOKEN="<YOUR_API_TOKEN>"

Still stuck? Let us know.

Customize the project#

Store a value in the namespace. In Program.cs, add this below the namespace:

new Cloudflare.WorkersKv("greeting", new()
{
AccountId = accountId,
NamespaceId = ns.Id,
KeyName = "greeting",
Value = "Hello, Pulumi!\n",
});

Deploy again. The preview shows one key to create:

Terminal window
pulumi up

Then read the value back from the Cloudflare API. It prints Hello, Pulumi!:

Terminal window
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/greeting

Cleaning up#

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure platform for humans and agents.