leilani
leilani
CDCloudflare Developers
Created by leilani on 9/15/2023 in #workers-help
Getting service bindings to work
Hi there, I'm trying to get one worker to call another worker. Both workers are on the same account. Say that I have worker Apple and worker Banana. I want a method within Apple to call Banana. In the Apple wrangler.toml, I have the service binding:
services = [
{ binding = "banana", service = "banana", environment = "production" }
]
services = [
{ binding = "banana", service = "banana", environment = "production" }
]
I deployed both workers, Apple and Banana. Within my entrypoint worker.ts file for the Apple worker, I assign the env variables to an exported variable that other code files can access within the Apple directory. Such as:
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
sharedEnv.env = env;

return await router
.handle(request, env, ctx)
.catch((err) => error(500, err.stack))
.then(corsify);
},
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
sharedEnv.env = env;

return await router
.handle(request, env, ctx)
.catch((err) => error(500, err.stack))
.then(corsify);
},
In my interface declaration for my sharedEnv variable that is of type Env, I make sure to put Banana as a type Fetcher:
interface Env {
RODEO_EMAIL: Fetcher
}
interface Env {
RODEO_EMAIL: Fetcher
}
Within a method in my Apple worker, I am trying to call Banana with the following code:
const emailResponse = await sharedEnv.env.banana.fetch(new Request('invite/peel', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"hello": "world"
})
}));
const emailResponse = await sharedEnv.env.banana.fetch(new Request('invite/peel', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"hello": "world"
})
}));
After deploying both and setting wrangler tail to tail the logs of both workers, I see that something errors out in my fetch call to Banana from the Apple worker. But, when I peek into the logs for Banana, there are no logs written. Am I missing something that is causing Apple to not successfully reach Banana? Your help is much appreciated!!
3 replies