Hello everyone! Does anyone know how to call one l...
# sst
i
Hello everyone! Does anyone know how to call one lambda from within another using serverless stack? I suppose aws-sdk is not suitable approach because it requires to supply
config
where we have to hardcode lambda”s name. But the issue is I use serverless stack stages (dev/prod) and all lambdas have unique hash suffix. I don’t know if this is a good idea if I search for a specific lambda and use a condition smth like:
Copy code
if (env === 'prod') {
  return 'prod-lambda-adah32h34'
} else {
  return 'dev-lambda-asdh3424jh'
}
t
generally calling one lambda from another suggests something isn't structured right
If you need to do this you can pass it in as an env variable
Copy code
environment: {
  LAMBDA_TO_CALL: func.functionArn
}
i
@thdxr Makes sense. I tried smth like this
Copy code
const COUNTRIES_LAMBDA = apiStack.api.getFunction('GET /settings/country-setup')?.functionName

if (COUNTRIES_LAMBDA) {
  app.addDefaultFunctionEnv({
    COUNTRIES_LAMBDA
  })
}
but the output of this looks like: `${Token[TOKEN.680]}`and not like:
stage-projectName-lambdaName
t
yeah don't worry once it gets deployed the token will be resolved correctly
it doesn't know the value at synth time
i
@thdxr I have found in the docs ----- You can also use
addDefaultFunctionPermissions
,
addDefaultFunctionEnv
, and
addDefaultFunctionLayers
to progressively add more permissions, environment variables, and layers to the defaults. These can be called multiple times and from anywhere. However, they only affect the functions that are created after the call. ----- but the issue is I need the name of a lambda in the scope of one stack. This lambda is a “sibling” of another one
@thdxr is there another way to get access to the
sst app
from the lambda or….another way how to extract lambda”s name in the scope of one API STACK? Or…split the stacks somehow so I can run API stack where my function is located, then I can add
env var
and after then add another lambda which can read this
var
?
r
Could you write an event or queue message to trigger an invocation in the other lambda? Or must it be synchronous? If sync, step functions?
t
@Ivan Roskoshnyi if you have things in seperate stacks you can share them across stack by exposing them as a field on the class
i
@Ross Coundon I hardly understand what you mean
@thdxr Well…I have only one API stack and I doubt I can use 2 API stacks in the scope of one application, right?
t
Oh you're saying you have 2 lambda functions in the same api and one needs to know baout the other?
I will say again, this isn't a good pattern which is why you're finding it tricky, lambda should generally not be calling each other
I'd recommend extracting common logic into library code that both can call into
You can try this if you still want to do this
Copy code
api.getFunction("func1").addEnvironment(api.getFunction("func2"))
i
@thdxr
Oh you're saying you have 2 lambda functions in the same api and one needs to know baout the other?
- correct. So…it is not a good idea and it is better to extract my code?
t
yeah correct
r
Does the lambda that does the calling need to receive a response from the lambda that's been called?
i
@Ross Coundon yes, it does
r
OK, then using an event-based mechanism might not be the way to go. Does it absolutely have to be in a separate lambda? Could the functionality be a part of the first lambda?
i
@Ross Coundon Well…I can move all the code in a separate file and import it from two lambdas of course…. But it would be much elegant if I simply call one lambda from another and get the response
@thdxr Thank you. Your approach is working
Copy code
api.getFunction("func1").addEnvironment(api.getFunction("func2"))