Injecting Environment Variables into a Worker

I am currently developing a slackbot using Cloudflare workers with webpack and serverless. What is the best practice for injecting env variables into the worker?


1 Like

You can use KV (key/value) for that.

What’s the workflow. is it

Inject env variables into KV store and then deploy the worker?

Is it possible to use serverless for that or do we need to do it out of process.

You keep any setting you need in KV and deploy your worker. Your worker can access KV. It is recommended to use KV for reading although your worker can write, writes take a while to get synced across network and you cannot count on that for atomic operations.

thanks for the timely response

1 Like

So basically no env vars?

How do people normally cope with different environments? I’ve got 2 testing environments and live and there’s no clear way to cope with this. Ultimately want to push the worker code through a CI/CD process.

@jima We actually have a full tutorial on building a Slackbot that could help Would love feedback on if that tutorial helps

@matt_dez Especially if you are planning to integrate with CI, I recommend using our CLI tool wrangler. Then you could use a custom web pack config to inject the variables. Though using KV another possible solution.