5

I'm trying to invoke a AWS Lambda function asynchronous from the AWS API Gateway.

I have a long running (2-3min) Lambda function and I want to invoke this Lambda function asynchronous from a HTTP Post request. I configured the API Gateway as a Lambda Proxy Integration (because I want to pass the body unmodified to the function) This is working fine, but after 30s I get a 504 due the API Gateway execution time restriction.

But I can't manage to call the function async. According to the AWS docs it should be possible if I set the haeder "X-Amz-Invocation-Type", but this doesn't make any difference.

Does anybody know if it is possible to invoke a function async and using the proxy integration?

1 Answer 1

7

AWS says it's possible if you set the X-Amz-Invocation-Type header to Event, but I ran into the same necessity a few months ago and this did not work for me, so I am not sure this is still the case or if it was just me who misconfigured it. Maybe you are missing the same thing as me back then: I did not add an InvocationType header on the Integration Request as the docs suggests, so this very likely is the case for you, but still, I can't guarantee it works)

The documentation says:

Configure Lambda asynchronous invocation in the API Gateway console

In Integration Request, add an X-Amz-Invocation-Type header.

In Method Request, add an InvocationType header and map it to the X-Amz-Invocation-Type header in the Integration Request with either a static value of 'Event' or the header mapping expression of method.request.header.InvocationType. For the latter, the client must include the InvocationType:Event header when making a request to the API method.

If this works, then you are good to go.

What I did back then, however, was to create an intermediate Lambda which literally acted as proxy to the actual Lambda.

There are a wide range of options to execute your function asynchronously, but you will need two Lambda functions regardless.

One option is to invoke another function (which will actually execute the task you want) asynchronously via the function invoked by API Gateway.

const params = {
        FunctionName: 'YOUR_FUNCTIONS_NAME',
        InvocationType: 'Event',
        Payload: JSON.parse(event.body) // this is the event coming from API Gateway
    };
    await lambda.invoke(params).promise(); // await here is only going to wait for the HTTP request to be successful. Once the 2nd Lambda is invoked, it will return immediately

Another option is to put a message in SQS and configure a trigger for your Lambda to be invoked when there's a new message in the SQS queue. Same thing applies for a SNS notification.

Other options include Kinesis, DynamoDB Streams, etc. but the idea is the same: the function invoked via API Gateway must be nothing but a proxy to the other Lambda. How this proxy is going to work (be it sending a message to SQS, SNS, invoking the other function asynchronously directly, etc.) does not matter, what matters is the concept to get around API Gateway's 30 seconds request limit.

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

4 Comments

It wasn't you... a Lambda proxy integration can't be invoked asynchronously because API Gateway expects the proxy response format... but with an async invocation, there's no response returned.
good to know, @Michael-sqlbot. Would you say that the documentation is misleading or I am still missing something?
I ended up using a non-proxy integration with a request/response mapping. I think @Michael-sqlbot is right, the proxy integration won't work async.
Is it possible to set X-Amz-Invocation-Type on a websocket API? I can only see the HTTP Headers option for REST APIs.

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.