What's the easiest way to reuse the same lambda fu...
# help
a
What's the easiest way to reuse the same lambda function while still using the Api construct? The use-case I have is that I have a single graphql lambda and the graphql spec should have POST, GET, and OPTIONS pointing to the same lambda. Also: we can't use ANY as we'd like OPTIONS to be unauthenticated (this is default browser behaviour we can't really change).
s
Something like this?
Copy code
// Create the HTTP API
    const api = new sst.Api(this, "Api", {
      routes: {
        "GET      /notes": "src/myLambda.main",
        "POST     /notes": "src/myLambda.main",
        "OPTIONS  /notes": "src/myLambda.main"
      }
    });
a
Yeah, that's what I have now. It creates 3 lambdas and deploys the code 3 times. Not a huge issue, but you'll have separate concurrency + separate cold starts when it's fundamentally the same lambda.
Ah, maybe if I just define the function outside / first then I can pass it in 3 times. I can give that a try.
s
Was just going to suggest that, using the Function construct directly: https://docs.serverless-stack.com/constructs/Function
a
I just won't get the niceness of
defaultFunctionProps
Yeah, it doesn't even let me define
defaultFunctionProps
(even when I'd expect it to apply to the other ones that the API construct creates):
Copy code
Error: Cannot define defaultFunctionProps when a Function is passed in to the routes
    at Api.addRoute (<redacted>/node_modules/@serverless-stack/resources/src/Api.ts:494:15)
    at <redacted>/node_modules/@serverless-stack/resources/src/Api.ts:401:23
    at Array.forEach (<anonymous>)
    at Api.addRoutes (<redacted>/node_modules/@serverless-stack/resources/src/Api.ts:399:25)
    at new Api (<redacted>/node_modules/@serverless-stack/resources/src/Api.ts:171:10)
    at new ApiStack (<redacted>/lib/ApiStack.ts:44:17)
    at Object.main (<redacted>/lib/index.ts:9:3)
    at Object.<anonymous> (<redacted>/.build/run.js:64:16)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
I guess
defaultFunctionProps
only applying to a subset of functions is probably more confusing than just throwing an error.
A workaround I can do is define all my lambdas outside of the
Api
construct and do the defaulting myself, something like:
Copy code
const defaultFunctionProps: sst.FunctionProps = {
      reservedConcurrentExecutions: env.lambda.concurrencyLimit,
      timeout: env.lambda.timeoutSeconds,
      runtime: lambda.Runtime.NODEJS_14_X,
    };

    const graphqlLambda = new sst.Function(this, 'graphql-api', {
      ...defaultFunctionProps,
      handler: 'services/graphql-api/src/main.handler',
      environment: {
        ...databaseEnvironmentVariables,
      },
    });
m
@Akos there is a discussion open regarding this. https://github.com/serverless-stack/serverless-stack/discussions/175
f
@Akos Throwing some ideas out there.. 1. Ignore
defaultFunctionProps
when passing in an `sst.Function`; or 2. Allow multiple routes handled by the same function
Copy code
routes: {
  "GET  /notes": "src/notes.main",
  "POST /notes": sst.ApiRoute.INHERIT_ABOVE,
  "GET  /users": "src/users.main",
}
I just made that up `sst.ApiRoute.INHERIT_ABOVE`… need to do some research to see what’s more natural
Btw, could u upvote the discussion Mike mentioned.. that would help us prioritize
a
Thanks, I voted and left a comment with my idea of how this could work.