How to set KV environment variables in the dashboard?

I'm trying to use a Cloudflare Pages worker which I have stored in the functions folder of my Pages repository,. This particular worker needs to access an KV-namespace - and in this instance, I'd like to set it through the dasboard but when I set environment variables, it doesn't seem like my Workers is able to pick them up...
15 Replies
Sia
SiaOPβ€’12mo ago
https://developers.cloudflare.com/workers/configuration/bindings/ - this is the docs I am following, under Configuration
Bindings Β· Cloudflare Workers docs
Interact with storage and other resources on the Cloudflare Developer Platform.
Chaika
Chaikaβ€’12mo ago
in the functions folder of my Pages repository,
If it's under the Functions folder, it's a function, not a worker. Functions are technically Workers under the hood, but that's far abstracted from you. Worker Config Docs will not apply to Functions
Sia
SiaOPβ€’12mo ago
@Chaika Oki, but how do I get the Functions to pick up the binding? A wrangler.toml file?
Chaika
Chaikaβ€’12mo ago
If you're trying to use pages dev (local dev) with your Function and a binding, there's docs on how to use those bindings locally here: https://developers.cloudflare.com/pages/platform/functions/bindings/
Bindings Β· Cloudflare Pages docs
A binding enables your Pages Functions to interact with resources on the Cloudflare developer platform. Use bindings to integrate your Pages Functions …
Sia
SiaOPβ€’12mo ago
@Chaika Just going to prod directly, no dev. Would a wrangler.toml inside the functions folder (not in the root of the functions folder but inside the actual functions folder) do?
name = "submit"

# ...

kv_namespaces = [
{ binding = "messages", id = "06779da6940b431db6e566b4846d64db" }
]
name = "submit"

# ...

kv_namespaces = [
{ binding = "messages", id = "06779da6940b431db6e566b4846d64db" }
]
Chaika
Chaikaβ€’12mo ago
oh, then you'd just set it in your Pages Project
In Account Home, select Workers & Pages. Select your Pages project > Settings > Functions > KV namespace bindings > Add binding. Choose whether you would like to set up the binding in your Production or Preview environment. Give your binding a name under Variable name. Under KV namespace, select your desired namespace. You must repeat steps 5 and 6 for both the Production and Preview environments. Redeploy your project for the binding to take effect.
no, wrangler.toml isn't used for Pages prod at all. In some cases dev uses it (which is confusing), but all production/live functions configuration has to be done in the dashboard
Sia
SiaOPβ€’12mo ago
@Chaika ace! Just found it.
Chaika
Chaikaβ€’12mo ago
nice! Yea it's a fair bit confusing right now, CF has been talking about merging Workers & Pages for a bit which would be nice (although not exactly sure what that would look like), right now it can be confusing. Functions are Workers with some of Pages magic on top like file based routing and such, and only support local dev (wrangler pages dev) and for deployments have to be configured entirely in the dash
Sia
SiaOPβ€’12mo ago
I need to learn this anyway, some day I hope to work for CF πŸ™‚ Actually got quite far in an interview process but was a very senior management role and I probably feel better working closer to the code or the community 😁 @Chaika this work like a charm, I'm very thankful.
Chaika
Chaikaβ€’12mo ago
Nice! Well best of luck with that, whenever we get Convergence it'll be nice and help with a lot of these differences
geomaster
geomasterβ€’12mo ago
What's Convergence?
Sia
SiaOPβ€’12mo ago
Chaika is referencing the convergence of Pages Functions with Cloudflare Workers. They are both Workers under the hood, with Functions having some additional Pages magic, which confuses things a bit when trying to learn the platform.
Chaika
Chaikaβ€’12mo ago
Convergence is a bit bigger then that, it'd be Pages and Workers together entirely: https://blog.cloudflare.com/pages-and-workers-are-converging-into-one-experience/ but yea you get the gist
The Cloudflare Blog
Bringing a unified developer experience to Cloudflare Workers and P...
Today we’re excited to announce that over the next year we will be working to bring together the best traits and attributes you know and love from each product into one powerful platform!
Chaika
Chaikaβ€’12mo ago
Pages has lots of very cool things like Git integration, preview deployments, free static asset serving, etc, but lacks all of the Worker Features, remote dev, and newer products like Logpush support. Workers has a Github Action you can use but no preview deployments outside of making other Workers for testing/staging (Which is what Wrangler Environment does), lots of trade offs
Sia
SiaOPβ€’12mo ago
This is very useful, yes, I was a bit confused as I was mixing Workers and Pages Functions, I thought they were the same, yet there are a bit different ways and especially Pages is so neat to integrate with Github. Either way, I'm on CF for the long run. Keep advocating where I work that we don't really need AWS or GCP. I find AWS in particular complicates things unnecessarily, and straight out lacks some of the most important elements wrt retail websites (bot protection etc).
Want results from more Discord servers?
Add your server