sanity
sanity

Reputation: 35792

Can I initiate a long-running AWS Lambda process via the Amazon API gateway asynchronously?

I need to initiate a long-running AWS Lambda job (several minutes) that will write its output to S3. It will be written in Java (actually Kotlin, but that shouldn't matter).

I'm investigating the Amazon API Gateway as the interface between this and other AWS Lambda jobs and an iOS mobile app that will talk to it.

Is it possible to asynchronously invoke such long-running Lambda processes, either directly from the API Gateway, or from another Lambda function?

Upvotes: 4

Views: 2039

Answers (2)

Ka Hou Ieong
Ka Hou Ieong

Reputation: 6510

API Gateway supports this case, but you cannot setup your integration from the console. You have to use the CLI tools or SDK to do so.

Here is a hint to lead you to there.

aws apigateway put-integration \
    --rest-api-id <value> \
    --resource-id <value> \
    --http-method POST \
    --type AWS \
    --integration-http-method POST \
    --uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:123456789012:function:Hello_World/invocations?Qualifier=$LATEST \
    --credentials arn:aws:iam::123456789012:role/lambda \
    --request-parameters integration.request.header.X-Amz-Invocation-Type='Event'

Upvotes: 1

ataylor
ataylor

Reputation: 66109

I don't think you can invoke a Lambda asynchronously directly through the API Gateway, but you can invoke the lambda from another lambda through the API. In Java, you'd use AWSLambdaClient.invoke() with an InvocationType of Event.

Be careful using Lambda for long running jobs: there is a hard 300 second limit on Lambda invocations. I'd recommend having a Lambda invoke code in a persistent resource like EC2 for long running backend processes.

Upvotes: 5

Related Questions