Hello everyone I was wondering if you can call a r...
# help
u
Hello everyone I was wondering if you can call a route from another route in your api and how to do it without knowing the base url ? (until you put a custom domain)
a
I can only think of
Lambda.invoke
by knowing the lambda name
But, wouldn't just importing the service you need fit the case? Esbuild will throw away stuff you don't need anyways
k
@Uncharted
Copy code
export function FuncStack({ stack, app }: StackContext) {
  const fn = new Function(stack, `myFunc`, {
    handler: 'lambda.handler',
    srcPath: 'backend'
  });
  return fn;
}

export function ApiStack({ stack, app }: StackContext) {
  var fn = use(FuncStack);
  var api = new Api(stack, 'api', {
    routes: {
      'GET /': fn
    }
  });
  stack.addOutputs({ apiUrl: api.url });
  fn.addEnvironment("API-url", api.url) ;  // <-- set API URL in environment var
  return api;
}
However, there are some limits to consider. API Gateway allows max 30s for HTTP and 29s for websocket requests. Calling another endpoint can increase the probability of a timeout. In cases where you are likely to exceed that limit, FunctionURLs or
lambda.invoke
can be used. Both have max 900s (but if you are calling from a Lambda that's triggered by API GW, the 29/30s limit applies anyway.
u
ah I see you can reinject the api url after its creation but lambda invoke is nice too
d
I am with Arpad’s second thought: just share the applicable code.