Cloudflare Workers KV
To setup a state store, create a component of type . See this guide on how to create and apply a state store configuration.
Warning
The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.
Spec metadata fields
To use this component, you must have a Workers KV namespace created in your Cloudflare account.
You can create a new Workers KV namespace in one of two ways:
Using the
Make note of the “ID” of the Workers KV namespace that you can see in the dashboard. This is a hex string (for example123456789abcdef8b5588f3d134f74ac
)–not the name you used when you created it!Using the Wrangler CLI:
# Authenticate if needed with `npx wrangler login` first
wrangler kv:namespace create <NAME>
The output contains the ID of the namespace, for example:
{ binding = "<NAME>", id = "123456789abcdef8b5588f3d134f74ac" }
Configuring the Worker
Dapr can manage the Worker for you automatically, or you can pre-provision a Worker yourself. Pre-provisioning the Worker is the only supported option when running on workerd.
Important
Use a separate Worker for each Dapr component. Do not use the same Worker script for different Cloudflare Workers KV state store components, and do not use the same Worker script for different Cloudflare components in Dapr (e.g. the Workers KV state store and the Queues binding).
If you want to let Dapr manage the Worker for you, you will need to provide these 3 metadata options:
cfAccountID
: ID of your Cloudflare account. You can find this in your browser’s URL bar after logging into the Cloudflare dashboard, with the ID being the hex string right afterdash.cloudflare.com
. For example, if the URL ishttps://dash.cloudflare.com/456789abcdef8b5588f3d134f74acdef
, the value forcfAccountID
is456789abcdef8b5588f3d134f74acdef
.cfAPIToken
: API token with permission to create and edit Workers and Workers KV namespaces. You can create it from the in the “My Profile” section in the Cloudflare dashboard:- Click on “Create token”.
- Select the “Edit Cloudflare Workers” template.
- Follow the on-screen instructions to generate a new API token.
When Dapr is configured to manage the Worker for you, when a Dapr Runtime is started it checks that the Worker exists and it’s up-to-date. If the Worker doesn’t exist, or if it’s using an outdated version, Dapr will create or upgrade it for you automatically.
If you’d rather not give Dapr permissions to deploy Worker scripts for you, you can manually provision a Worker for Dapr to use. Note that if you have multiple Dapr components that interact with Cloudflare services via a Worker, you will need to create a separate Worker for each one of them.
To manually provision a Worker script, you will need to have Node.js installed on your local machine.
- Create a new folder where you’ll place the source code of the Worker, for example:
daprworker
. - If you haven’t already, authenticate with Wrangler (the Cloudflare Workers CLI) using:
npx wrangler login
. - Inside the newly-created folder, create a new
wrangler.toml
file with the contents below, filling in the missing information as appropriate:
- Copy the (pre-compiled and minified) code of the Worker in the
worker.js
file. You can do that with this command:
# Set this to the version of Dapr that you're using
DAPR_VERSION="release-1.10"
curl -LfO "https://raw.githubusercontent.com/dapr/components-contrib/${DAPR_VERSION}/internal/component/cloudflare/workers/code/worker.js"
- Deploy the Worker using Wrangler:
Once your Worker has been deployed, you will need to initialize the component with these two metadata options:
workerName
: Name of the Worker script. This is the value you set in thename
property in thewrangler.toml
file.workerUrl
: URL of the deployed Worker. Thenpx wrangler command
will show the full URL to you, for examplehttps://mydaprkv.mydomain.workers.dev
.
- All requests made by Dapr to the Worker are authenticated via a bearer token (technically, a JWT) which is signed with an Ed25519 key.
- The bearer token is generated on each request and is valid for a brief period of time only (currently, one minute).
To let Dapr issue bearer tokens, and have your Worker validate them, you will need to generate a new Ed25519 key pair. Here are examples of generating the key pair using OpenSSL or the step CLI.
You can generate a new Ed25519 key pair with OpenSSL using:
If you don’t have the step CLI already, install it following the .
Next, you can generate a new Ed25519 key pair with the step CLI using:
step crypto keypair \
public.pem private.pem \
--kty OKP --curve Ed25519 \
--insecure --no-password
Regardless of how you generated your key pair, with the instructions above you’ll have two files:
private.pem
contains the private part of the key; use the contents of this file for thekey
property of the component’s metadata.- contains the public part of the key, which you’ll need only if you’re deploying a Worker manually (as per the instructions in the previoius section).
Warning
Additional notes
- Note that Cloudflare Workers KV doesn’t guarantee strong data consistency. Although changes are visible immediately (usually) for requests made to the same Cloudflare datacenter, it can take a certain amount of time (usually up to one minute) for changes to be replicated across all Cloudflare regions.
- This state store supports TTLs with Dapr, but the minimum value for the TTL is 1 minute.
- Read this guide for instructions on configuring state store components
- Documentation for Cloudflare Workers KV