Secrets Store Secrets

Hi what is the right way to add secret store secrets when using a stateless setup (i.e. getRandom)? Since they are async I don't see how I can pass them in on container startup.
1 Reply
dhruvrajan
dhruvrajanOP3d ago
I'm using the following worker spec to pass requests through to my backend (fastapi in the container)
import { Container, getRandom } from "@cloudflare/containers";
import { env } from "cloudflare:workers";
import { Hono } from "hono";

export class MyContainer extends Container<Env> {
// Port the container listens on (default: 8080)
defaultPort = 8080;
// Time before container sleeps due to inactivity (default: 30s)
sleepAfter = "2m";
// Environment variables passed to the container
envVars = {
MESSAGE: "I was passed in via the container class!",
};

// Optional lifecycle hooks
override onStart() {
console.log("Container successfully started");)
}

override onStop() {
console.log("Container successfully shut down");
}

override onError(error: unknown) {
console.log("Container error:", error);
}
}

// Create Hono app with proper typing for Cloudflare Workers
const app = new Hono<{
Bindings: Env;
}>();

// Load balance requests across multiple containers
app.get("*", async (c) => {
const container = await getRandom(c.env.MY_CONTAINER, 3);
return await container.fetch(c.req.raw);
});

export default app;
import { Container, getRandom } from "@cloudflare/containers";
import { env } from "cloudflare:workers";
import { Hono } from "hono";

export class MyContainer extends Container<Env> {
// Port the container listens on (default: 8080)
defaultPort = 8080;
// Time before container sleeps due to inactivity (default: 30s)
sleepAfter = "2m";
// Environment variables passed to the container
envVars = {
MESSAGE: "I was passed in via the container class!",
};

// Optional lifecycle hooks
override onStart() {
console.log("Container successfully started");)
}

override onStop() {
console.log("Container successfully shut down");
}

override onError(error: unknown) {
console.log("Container error:", error);
}
}

// Create Hono app with proper typing for Cloudflare Workers
const app = new Hono<{
Bindings: Env;
}>();

// Load balance requests across multiple containers
app.get("*", async (c) => {
const container = await getRandom(c.env.MY_CONTAINER, 3);
return await container.fetch(c.req.raw);
});

export default app;
I'd like to pass in, say, OPENAI_API_KEY secret from the secrets store. I assume I have to use env.OPENAPI_KEY.get() and somehow asynchronously set this ideally before container start so it can be passed in on startup. How do I do this>

Did you find this page helpful?