How to make an API Gateway trigger call a lambda asynchronously? - amazon-web-services

I setup my lambda in AWS in the following way:
There is an API Gateway trigger which triggers a Lambda, which in turn is supposed to write to SQS.
The last part doesn't happen. From what I've read, this is because API Gateway invokes lambdas synchronously by default since calling it async-ly sacrifices the ability to return a response from the lambda.
I have also read that nonetheless, it is possible to configure API Gateway to call lambas async: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html
However, it is unclear how to do this when the API Gateway is created by Lambda, as is the case with triggers. There is no clear way of adding additional headers to them.
Is it possible make it do an async invocation?

After digging into it, it seems that API Gateway does not (yet) support async invocations for lambdas in any way for HTTP APIs, it is possible REST APIs.
Step 1: Create a trigger using the REST configuration
Step 2: Disable proxy integration
Async is explicitly not supported for proxies.
Step 3: Add magic header to make lambda async
Step 4: Confirm
To confirm if it worked, look into your destination (in my case SQS) to confirm.
Additionally, your API (when called via API Gateway) should now return an empty page.

Related

Chain/Redirect AWS API Gateway custom authorizer without explicit invoke

Is there any way of chaining custom authorizers in API Gateway without making an explicit invoke to another lambda?
Imagine like having an authorizerA that is in charge of the validation of the token of the request. Once, that authorizer validates that information, it will pass the request to an authorizerB that will validate something else. But authorizerA just "returns" to the API Gateway that the authorization request must be sent to another lambda or just fail if the validation of authorizerA fails and so on for multiple authorizers.
Is there any other approach that does not involve an invoke (that cost twice the waiting time for 2 authorizers or a lot for multiple authorizers)?
I haven't used step functions, but could this be a case for it?
The short answer is yes, step function is your ally. You will need a lambda function to attach as an authorizer to your API Gateway. This lambda function will invoke the step function where all the magic should happen and then return the result back to the lambda authorizer so it can be returned to the API Gateway.

How to create a usage plan for AWS HTTP API?

I am using AWS API Gateway with HTTP API which invokes a lambda function. However HTTP API doesn't include USAGE feature. According to my requirement I need to create a usage for a client depending on the status code of the response sent back by the lambda. Since I cant access the response sent by lambda in API Gateway, I am looking for an custom solution. I am planing to use STEP function.
For example:
Instead of API Gateway directly invoking a lambda function it can call a STEP function where I can execute LambdaA. next it would trigger LambdaB with response from LambdaA as input to LambdaB in a sequential manner. I don't know If this is the right approach
I would like to know what is best way of solving this problem...thanks in advance

Can AWS SQS Service trigger API Gateway Proxy?

I working on a project and I need to trigger a proxy API whenever a message is received by the AWS SQS Service. I went through couple of articles and found that API to SQS is possible but did not find anything related to SQS triggering API Gateway. Can somebody please guide.
It is not possible to invoke a Lambda function asynchronously with a proxy integration. You can do this with Lambda non-proxy integration.
Set up asynchronous invocation of the backend Lambda function - https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html
Another workaround is to use another function that is invoked synchronously by the API Gateway API and have that function invoke the function that is part of the Spring boo application asynchronously using the Lambda Invoke API or SDK equivalent.
https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
For instance, you want Function A to be invoked asynchronously. To achieve this you would have the API Gateway API invoke Function B synchronously and have Function B invoke Function A asynchronously with the Invoke API.
Step Functions also cant be used as a workaround to invoke the REST APIs as this would not help. The Lambda functions are still invoked by the API Gateway API so you would still run into the limitation of a proxy integrations not being able to invoke Lambda asynchronously. You then decided that ECS would better suit your case as Lambda is not a good fit.

AWS Service Proxy to SQS

We are exposing a AWS Api Gateway which needs to act as proxy and push body as message to AWS SQS. Our API gateway body can be array of object which we need to parse and send each object to SQS as separate message. Is there any way to achieve this without using Lambda ?
To answer your question, for something as simple as parsing a message and posting to SQS, I don't believe you need Lambda, no. Lambdas are designed for serverless-architecture. That is, when you don't have a server and you still want to run code. In this case, you do have a server behind your API Gateway, so you don't need Lambda (unless you want fancy branching error handling). You can use your API Gateway directly, yep. Here's a code review I found:
https://dzone.com/articles/creating-aws-service-proxy-for-amazon-sqs

Handing back a response to API Gateway from Lambda

I'm using API Gateway-to-Lambda for a few micro-services but in at least one case the service will take 20-30 seconds to complete so in cases like this I'd like to pass back an immediate response to the client, something like:
status: 200
message: {
progressId: 1234
}
and then allow the Lambda Function to continue on (and periodically updating the "processId" somewhere that is accessible to a client. The problem is that if you call context.succeed(), context.fail(), or context.done() that apparently stops the lambda function from further execution and yet it's the only way I know to flush the stdout buffer back to the API Gateway.
This has led me to a second approach which I haven't yet try to tackle (and for simplicity sake would love to avoid) which involves API Gateway calling a "Responder" Lambda function that then asynchronously fires off the Microservice and then immediately responds to the API Gateway.
I've tried to illustrate these two options in sketch format below. I'd love to hear how anyone's been able to solve this problem.
Currently API Gateway requires that the AWS Lambda integration is synchronous. If you desire asynchronous invocation of your Lambda function, you have 2 options:
Invoking the Lambda asynchrously, either with an AWS integration calling InvokeAsync on Lambda, or using an intermediate service such as SNS or Kinesis to trigger the Lambda function.
You're #2 diagram, using a synchronous Lambda invoke to initiate the asynchronous invoke.
As of Apr/2016 is it is possible to create async Lambda execution through API Gateway by using AWS Service Proxy. See http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html
You can send the X-Amz-Invocation-Type header, it supports async calls through the Event value
You can optionally request asynchronous execution by specifying Event as the InvocationType
http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax
Also, if you can't send it via your micro-service, you can configure this header to be passed by default through the Method Execution -> Integration Request -> HTTP Headers in your API Gateway Resource
This worked for me on a micro-service -> API Gateway -> Lambda scenario, like the mentioned on the question.