scubbo
scubbo

Reputation: 5847

How to invoke Lambda asyncronously from API Gateway?

As per here, I believe that setting a header of X-Amz-Invocation-Type: Event should set my Lambda invocation to be asynchronous.

However, by putting a import time;time.sleep(5000) at the beginning of my Lambda function, and sending requests to my API Gateway, I observe that:

$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id <resource-id> \
    --http-method POST | jq -r '.requestParameters'
{
  "integration.request.header.X-Amz-Invocation-Type": "'Event'"
}
$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id <resource-id> \
    --http-method POST | jq -r '.uri'
arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
$ curl https://<api-id>.execute-api.us-east-1.amazonaws.com/LATEST/<path> -d '{}'
{"message": "Endpoint request timed out"}

[here I removed the sleep from my lambda function and made it return immediately]

$ curl https://<api-id>.execute-api.us-east-1.amazonaws.com/LATEST/<path> -d '{}'
{"body": "Request OK", "headers": {"Content-Type": "application/json"}, "statusCode": 200}

Assuming that the documentation is accurate, my best guess is that I've misconfigured the Integration somehow - perhaps the "'Event'" is incorrect and it should be "Event"? I'm pretty sure the single-quotes are required, however, to demarcate a static value (as opposed to value parsed from the request). In particular, I suspect that my responseParameters are not right:

$ aws apigateway get-integration --rest-api-id <api-id> \
    --resource-id dzv1zj \
    --http-method POST | jq -r '.integrationResponses'
{
  "200": {
    "responseTemplates": {
      "application/json": null
    },
    "statusCode": "200"
  }
}

Should null there be some VTL that staticly returns a 200 OK?

As for alternatives: I see that invoke-async is deprecated. I'd really rather not go to the overhead of going API Gateway -> SNS -> Lambda.

EDIT: Here are the logs from calling the API via the "Test" option on the console:

Execution log for request test-request

Wed Mar 07 17:24:57 UTC 2018 : Starting execution for request: test-invoke-request
Wed Mar 07 17:24:57 UTC 2018 : HTTP Method: POST, Resource Path: /<path>
Wed Mar 07 17:24:57 UTC 2018 : Method request path: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request query string: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request headers: {}
Wed Mar 07 17:24:57 UTC 2018 : Method request body before transformations: {"abc":"def"}
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request headers: {X-Amz-Date=20180307T172457Z, x-amzn-apigateway-api-id=<api-id>, Accept=application/json, User-Agent=AmazonAPIGateway_<api-id>, Host=lambda.us-east-1.amazonaws.com, X-Amz-Content-Sha256=2c3fbda5f48b04e39d3a87f89e5bd00b48b6e5e3c4a093de65de0a87b8cc8b3b, X-Amzn-Trace-Id=Root=1-5aa02069-8670eb5d98dbc4ade9df03d8, x-amzn-lambda-integration-tag=test-request, Authorization=**********************************************************************************************************************************************************************************************************************************************************************************************************************************************bfe3de, X-Amz-Source-Arn=arn:aws:execute-api:us-east-1:<account-id>:<api-id>/null/POST/<path>, X-Amz-Invocation-Type=Event, X-Amz-Security-Token=[REDACTED] [TRUNCATED]
Wed Mar 07 17:24:57 UTC 2018 : Endpoint request body after transformations: {"abc":"def"}
Wed Mar 07 17:24:57 UTC 2018 : Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:<account-id>:function:[...]Lambda-4HOA0ZSFAYCI/invocations
Wed Mar 07 17:24:57 UTC 2018 : Received response. Integration latency: 43 ms
Wed Mar 07 17:24:57 UTC 2018 : Endpoint response body before transformations: 
Wed Mar 07 17:24:57 UTC 2018 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=75501cbf-222c-11e8-a1fc-2b19f19a9429, Content-Length=0, Date=Wed, 07 Mar 2018 17:24:57 GMT, X-Amzn-Trace-Id=root=1-5aa02069-8670eb5d98dbc4ade9df03d8;sampled=0}
Wed Mar 07 17:24:57 UTC 2018 : Method response body after transformations: 
Wed Mar 07 17:24:57 UTC 2018 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5aa02069-8670eb5d98dbc4ade9df03d8, Content-Type=application/json}
Wed Mar 07 17:24:57 UTC 2018 : Successfully completed execution
Wed Mar 07 17:24:57 UTC 2018 : Method completed with status: 200

Upvotes: 2

Views: 1292

Answers (2)

hashier
hashier

Reputation: 4750

I found scubbo's answer but it didn't enlighten me until much later, so I post the same, but with a screenshot for any future lost soul

enter image description here

Upvotes: 1

scubbo
scubbo

Reputation: 5847

Now I feel stupid - I needed to deploy my API. That seems like the API Gateway equivalent of "Have you tried turning it off and turning it on again?" :)

Upvotes: 1

Related Questions