问题
I am writing an AWS Lambda that is invoked via HTTP (i.e. the API Gateway integration).
I wish to use that API as a GitHub webhook. When the webhook/lambda is called I want the HTTP response to be sent right away and the lambda to keep executing (because it can take some time).
This is similar to the Event invocation type when invoking a lambda:
- Event: fire and forget
- RequestResponse: wait for the response
Is that possible to use that invocation type with the HTTP integration?
Note: I am using the serverless.com framework.
回答1:
As @michael-sqlbot pointed out you can get this behavior by using the X-Amz-Invocation-Type:Event Header. Getting this setup is a little screwy and the linked documentation is accurate but a little outdated (in my opinion).
- The Path Override needs to be:
/2015-03-31/functions/<ARN TO YOUR LAMBDA>/invocations
- The Execution Role needs to be able to invoke your lambda.
- When initially setting up the Integration Request you cannot add headers so you have to save it, then come back and add headers. From there you can hard code the X-Amz-Invocation-Type to Event by putting the string
'Event'
in theMapped from
field (as pictured below)
Or... you could also achieve this by having your lambda that's wired up to the API Gateway (we'll call it lambda A) invoke another lambda (lambda B) using the Event
invocation type. This way A doesn't care about the response of B and can return a successful response to the API Gateway within several hundred milliseconds (assuming you aren't doing too much else). Then Lambda B can continue running for however long is necessary (as long as it's under the 5 minute lambda limitation or your configured timeout).
来源:https://stackoverflow.com/questions/50272459/how-can-i-set-an-aws-lambda-to-be-invoked-asynchronously-through-http-api-gatewa