Hi, is it possible to call a function both from st...
# help
s
Hi, is it possible to call a function both from step function and rest API? Please provide a full real example of step functions in sst.
t
Yes you should create the function separately and mount it at the an API path and then it can also be used in a step function
s
@thdxr thank you, can you provide a small code example.
t
It's a lot of code so it's going to be difficult for me to write it all for you especially because I don't know how your step function looks like. The just of it would be to define your function like this:
Copy code
const myFunc = new sst.Function(stack, "myFunc", {
  handler: "src/lambda.handler",
})
You can use that in the api routes
Copy code
routes: {
  "GET /": myFunc
}
And then in a sfn invoke
tasks.LambdaInvoke(stack, "invoke", { lambdaFunction: myFunc })
However I'm not sure this is a good idea. The payload to this function will be different depending on if it's coming from the API or from the sfn. I'd recommend making separate functions for this
s
you’d have different
event
object shapes in the Lambda function if it was called via REST API or Step Function. but it’s doable. if using TypeScript, you could create TypeScript predicates (e.g.
isRestPayload
and
isSFNPayload
) to expose the proper properties on
event
while working on your Lambda code