5

I want to use API Gateway to send a message to SQS which then needs to trigger Lambda. After calculations are finished within Lambda, I need to pass the results back to API Gateway. In other words, something like this:

Get request --> Gateway API --> SQS --> Lambda --> (back to the same SQS?) -->  Gateway API

I have setup all the necessary permissions meaning that I can call Gateway API and send message to SQS which then sends that to Lambda (I can see in the Cloudwatch that Lambda received the message). However, I cannot get the Lambda response back to Gateway API...

Does anybody has some advice/tutorial/blog post about that? I have watched various youtube videos and searched posts on SO but didn't find solution for my problem.

4
  • 1
    Your flow is asyn from SQS -> Lambda. If you need to get response, Gateway API -> Lambda so you can get response from Lambda. Commented Feb 24, 2021 at 15:56
  • 1
    If you want API Gateway to wait for the response, why are you using SQS? Commented Feb 24, 2021 at 16:03
  • @NghiaDo So let me see if understand. I have to have then two methods inside my API gateway, one is POST to my SQS and another one is GET from Lambda? I need to call API Gateway twice? Commented Feb 24, 2021 at 16:04
  • @MarkB Because I have been advised to use SQS in case of many requests per second (for example potentially >500 in 1 second). Should I do that at all? Commented Feb 24, 2021 at 16:05

1 Answer 1

10

AWS Lambda can handle a large number of concurrent invocations. The default is 1000 (one thousand) and can be increased via a support ticket to "Hundreds of thousands".

If you want to use SQS to smoothen intermittent request spikes, then the Lambda function invocations will be asynchronous with respect to the caller's/client's API Gateway call and you need to use other means to feedback the Lambda invocation result to the API Gateway caller/client.

One such possibility can be a callback URL that your Lambda will invoke on the caller's/client's side once it has processed the invocation. Or you can store the lambda invocation result somewhere (such as S3 or DynamoDB) and the caller/client can use polling to periodically ask for the invocation result (check whether it is ready and if so, retrieve it).

Either way, once you use SQS to decouple API Gateway invocations from the processing of those invocations by your Lambda function via SQS messages, then the processing of the Lambda invocations will be asynchronous to the API Gateway caller/client request. So, the HTTP request of the API Gateway caller/client will return right away without waiting for the Lambda invocation result.

Sign up to request clarification or add additional context in comments.

1 Comment

Great answer, this clarifies things a lot. So if I can ensure that Lambda handles 100s of requests per second in synchronous manner (FIFO) than I don't need to use SQS. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.