Cloudflare Workers KV

Detailed information on the Cloudflare Workers KV state store component

Create a Dapr component

To setup a Cloudflare Workers KV state store, create a component of type state.cloudflare.workerskv. See this guide on how to create and apply a state store configuration.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. spec:
  6. type: state.cloudflare.workerskv
  7. version: v1
  8. # Increase the initTimeout if Dapr is managing the Worker for you
  9. initTimeout: "120s"
  10. metadata:
  11. # ID of the Workers KV namespace (required)
  12. - name: kvNamespaceID
  13. value: ""
  14. # Name of the Worker (required)
  15. - name: workerName
  16. value: ""
  17. # PEM-encoded private Ed25519 key (required)
  18. - name: key
  19. value: |
  20. -----BEGIN PRIVATE KEY-----
  21. MC4CAQ...
  22. -----END PRIVATE KEY-----
  23. # Cloudflare account ID (required to have Dapr manage the Worker)
  24. - name: cfAccountID
  25. value: ""
  26. # API token for Cloudflare (required to have Dapr manage the Worker)
  27. - name: cfAPIToken
  28. value: ""
  29. # URL of the Worker (required if the Worker has been pre-created outside of Dapr)
  30. - name: workerUrl
  31. value: ""

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

FieldRequiredDetailsExample
kvNamespaceIDYID of the pre-created Workers KV namespace“123456789abcdef8b5588f3d134f74ac”
workerNameYName of the Worker to connect to“mydaprkv”
keyYEd25519 private key, PEM-encodedSee example above
cfAccountIDY/NCloudflare account ID. Required to have Dapr manage the worker.“456789abcdef8b5588f3d134f74ac”def
cfAPITokenY/NAPI token for Cloudflare. Required to have Dapr manage the Worker.“secret-key”
workerUrlY/NURL of the Worker. Required if the Worker has been pre-provisioned outside of Dapr.https://mydaprkv.mydomain.workers.dev

When you configure Dapr to create your Worker for you, you may need to set a longer value for the initTimeout property of the component, to allow enough time for the Worker script to be deployed. For example: initTimeout: "120s"

Create a Workers KV namespace

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 Cloudflare dashboard
    Make note of the “ID” of the Workers KV namespace that you can see in the dashboard. This is a hex string (for example 123456789abcdef8b5588f3d134f74ac)–not the name you used when you created it!

  • Using the Wrangler CLI:

    1. # Authenticate if needed with `npx wrangler login` first
    2. wrangler kv:namespace create <NAME>

    The output contains the ID of the namespace, for example:

    1. { binding = "<NAME>", id = "123456789abcdef8b5588f3d134f74ac" }

Configuring the Worker

Because Cloudflare Workers KV namespaces can only be accessed by scripts running on Workers, Dapr needs to maintain a Worker to communicate with the Workers KV storage.

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:

  • workerName: Name of the Worker script. This will be the first part of the URL of your Worker. For example, if the “workers.dev” domain configured for your Cloudflare account is mydomain.workers.dev and you set workerName to mydaprkv, the Worker that Dapr deploys will be available at https://mydaprkv.mydomain.workers.dev.
  • 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 after dash.cloudflare.com. For example, if the URL is https://dash.cloudflare.com/456789abcdef8b5588f3d134f74acdef, the value for cfAccountID is 456789abcdef8b5588f3d134f74acdef.
  • cfAPIToken: API token with permission to create and edit Workers and Workers KV namespaces. You can create it from the “API Tokens” page in the “My Profile” section in the Cloudflare dashboard:
    1. Click on “Create token”.
    2. Select the “Edit Cloudflare Workers” template.
    3. 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.

  1. Create a new folder where you’ll place the source code of the Worker, for example: daprworker.
  2. If you haven’t already, authenticate with Wrangler (the Cloudflare Workers CLI) using: npx wrangler login.
  3. Inside the newly-created folder, create a new wrangler.toml file with the contents below, filling in the missing information as appropriate:
  1. # Name of your Worker, for example "mydaprkv"
  2. name = ""
  3. # Do not change these options
  4. main = "worker.js"
  5. compatibility_date = "2022-12-09"
  6. usage_model = "bundled"
  7. [vars]
  8. # Set this to the **public** part of the Ed25519 key, PEM-encoded (with newlines replaced with `\n`).
  9. # Example:
  10. # PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMCowB...=\n-----END PUBLIC KEY-----
  11. PUBLIC_KEY = ""
  12. # Set this to the name of your Worker (same as the value of the "name" property above), for example "mydaprkv".
  13. TOKEN_AUDIENCE = ""
  14. [[kv_namespaces]]
  15. # Set the next two values to the ID (not name) of your KV namespace, for example "123456789abcdef8b5588f3d134f74ac".
  16. # Note that they will both be set to the same value.
  17. binding = ""
  18. id = ""

Note: see the next section for how to generate an Ed25519 key pair. Make sure you use the public part of the key when deploying a Worker!

  1. Copy the (pre-compiled and minified) code of the Worker in the worker.js file. You can do that with this command:
  1. # Set this to the version of Dapr that you're using
  2. DAPR_VERSION="release-1.10"
  3. curl -LfO "https://raw.githubusercontent.com/dapr/components-contrib/${DAPR_VERSION}/internal/component/cloudflare/workers/code/worker.js"
  1. Deploy the Worker using Wrangler:
  1. npx wrangler publish

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 the name property in the wrangler.toml file.
  • workerUrl: URL of the deployed Worker. The npx wrangler command will show the full URL to you, for example https://mydaprkv.mydomain.workers.dev.

Generate an Ed25519 key pair

All Cloudflare Workers listen on the public Internet, so Dapr needs to use additional authentication and data protection measures to ensure that no other person or application can communicate with your Worker (and thus, with your Worker KV namespace). These include industry-standard measures such as:

  • All requests made by Dapr to the Worker are authenticated via a bearer token (technically, a JWT) which is signed with an Ed25519 key.
  • All communications between Dapr and your Worker happen over an encrypted connection, using TLS (HTTPS).
  • 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.

Support for generating Ed25519 keys is available since OpenSSL 1.1.0, so the commands below will not work if you’re using an older version of OpenSSL.

Note for Mac users: on macOS, the “openssl” binary that is shipped by Apple is actually based on LibreSSL, which as of writing doesn’t support Ed25519 keys. If you’re using macOS, either use the step CLI, or install OpenSSL 3.0 from Homebrew using brew install openssl@3 then replacing openssl in the commands below with $(brew --prefix)/opt/openssl@3/bin/openssl.

You can generate a new Ed25519 key pair with OpenSSL using:

  1. openssl genpkey -algorithm ed25519 -out private.pem
  2. openssl pkey -in private.pem -pubout -out public.pem

On macOS, using openssl@3 from Homebrew:

  1. $(brew --prefix)/opt/openssl@3/bin/openssl genpkey -algorithm ed25519 -out private.pem
  2. $(brew --prefix)/opt/openssl@3/bin/openssl pkey -in private.pem -pubout -out public.pem

If you don’t have the step CLI already, install it following the official instructions.

Next, you can generate a new Ed25519 key pair with the step CLI using:

  1. step crypto keypair \
  2. public.pem private.pem \
  3. --kty OKP --curve Ed25519 \
  4. --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 the key property of the component’s metadata.
  • public.pem 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

Protect the private part of your key and treat it as a secret value!

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.

Last modified January 9, 2023: [1.10] Docs for Cloudflare components (#3032) (20c44654)