My design is that api will trigger first lambda function, this function then send sns and return, sns triggers second lambda function. Now I want that api get the response from the second lambda function.
Here is the flow: The api get the request from the user and then trigger the first lambda function, the first lambda function creates a sns and return. Now the api is at the lambda function stage and still waiting for the response from the second lambda. sns triggers the second lambda function; the second lambda function return some result and pass it to the api. api gets the response and send it back to user.
I know there is a way using sdk to get the second lambda function and set event type to make it async. But here I want to use sns, is it possible?
Need some help/advices. Thanks in advance!
You need something to share the lambda_func_2's return with lambda_func_1, the api gateway request context only return when you call callback
on func1, you can not save or send the request contex
to another lb_func.
My solution for this case is use Dynamodb (or every database) to share the f2's result.
F1 send data to sns, the date include a key like transactionID
(uuid or timestamp). Then "wait" until F1 receive the result in table (ex: tbl_f2_result) and execute callback
function with the result. Maybe query with transactionID
until you receive data or only try 10 times (with time out 2s for one time, in worst case you will wait 20 seconds)
F2 has been trigged by SNS, do somthing with data include the transactionID
then insert the result (success or not, error message ...) to result table(tbl_f2_result
) with transactionID
=> result
, callback finish F2.
transactionID
is index key of table :D
You have to increase F1's timeout - Default is 6 seconds.