43

Using AWS CLI

aws --version
aws-cli/1.11.21 Python/2.7.12 Darwin/15.3.0 botocore/1.4.78

Creating a POST method for API Gateway as explained at https://github.com/arun-gupta/serverless/tree/master/aws/microservice#post-method. This method can be invoked successfully using test-invoke-method and AWS Console.

Creating a GET method using AWS CLI https://github.com/arun-gupta/serverless/tree/master/aws/microservice#get-method. Invoking this method using test-invoke-method and AWS Console gives the following error:

{
    "status": 500,
    "body": "{\"message\": \"Internal server error\"}",
    "log": "Execution log for request test-request\nThu Dec 29 00:58:56 UTC 2016 : Starting execution for request: test-invoke-request\nThu Dec 29 00:58:56 UTC 2016 : HTTP Method: GET, Resource Path: /books\nThu Dec 29 00:58:56 UTC 2016 : Method request path: {}\nThu Dec 29 00:58:56 UTC 2016 : Method request query string: {}\nThu Dec 29 00:58:56 UTC 2016 : Method request headers: {}\nThu Dec 29 00:58:56 UTC 2016 : Method request body before transformations: \nThu Dec 29 00:58:56 UTC 2016 : Endpoint request URI: https://lambda.us-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-1:598307997273:function:MicroserviceGetAll/invocations\nThu Dec 29 00:58:56 UTC 2016 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=******************************************************************************************************************************************************************************************************************************************************************************************************482377, X-Amz-Date=20161229T005856Z, x-amzn-apigateway-api-id=sofl9ilki7, X-Amz-Source-Arn=arn:aws:execute-api:us-west-1:598307997273:sofl9ilki7/null/GET/books, Accept=application/json, User-Agent=AmazonAPIGateway_sofl9ilki7, Host=lambda.us-west-1.amazonaws.com, X-Amzn-Trace-Id=Root=1-58645fd0-7d733ae3c383f4378fcc0338}\nThu Dec 29 00:58:56 UTC 2016 : Endpoint request body after transformations: \nThu Dec 29 00:58:56 UTC 2016 : Endpoint response body before transformations: <AccessDeniedException>\n  <Message>Unable to determine service/operation name to be authorized</Message>\n</AccessDeniedException>\n\nThu Dec 29 00:58:56 UTC 2016 : Endpoint response headers: {x-amzn-RequestId=f95a8659-cd61-11e6-80f6-ddd6ce5b7e8b, Connection=keep-alive, Content-Length=130, Date=Thu, 29 Dec 2016 00:58:56 GMT}\nThu Dec 29 00:58:56 UTC 2016 : Lambda invocation failed with status: 403\nThu Dec 29 00:58:56 UTC 2016 : Execution failed due to configuration error: \nThu Dec 29 00:58:56 UTC 2016 : Method completed with status: 500\n",
    "latency": 39
}

The ARN identified in the error message is arn:aws:execute-api:us-west-1:598307997273:sofl9ilki7/null/GET/books. Wondering if null instead of test is causing this to fail?

Arun Gupta
  • 3,965
  • 5
  • 31
  • 39
  • 1
    I have been having some trouble with a similar issue. take a look at my post i just made today. Also i just upvoted your question to put you over 3000. happy 3000 my friend. – sao Nov 21 '19 at 23:52

4 Answers4

111

I think you are using "GET" for your Lambda function endpoint on your GET method as well. Please change it to use "POST" for the Lambda integration HTTP method.

Using GET for lambda integrations on AWS API Gateway may leave you wondering why POST integrations are working but GET integrations don't work. GET AWS_PROXY integrations will fail if GET is used as the method on the integration. POST should be used for the lambda integration, even if the OPEN API specification is for a get method.

Jordan Morris
  • 2,101
  • 2
  • 24
  • 41
Ka Hou Ieong
  • 5,835
  • 3
  • 20
  • 21
  • 2
    That did the trick, thanks much! Why `POST` integration method is required for a `GET` HTTP? – Arun Gupta Dec 31 '16 at 04:00
  • 4
    The integration is the API you want to proxy to, and the method is the API you are going to expose to your customer. Since Lambda defines their invocation API with a POST method, so you have to follow whatever they define the API. – Ka Hou Ieong Jan 03 '17 at 19:07
  • 2
    I'm confused by this. The AWS docs give an example of calling a lambda via GET: http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html and as noted on the AWS forum, it _is_ possible to make it work via the console alone: https://forums.aws.amazon.com/thread.jspa?threadID=209420. – devrobf Aug 10 '17 at 11:39
  • When you are doing integration settings from cli such as when you need a auto deploy script you will need to specify the --integration-http-method POST for GET methods. I dont believe this is documented anywhere in aws official docs. The aws cli command should look something like this: aws apigateway put-integration --rest-api-id xxxx --resource-id xxx --http-method GET --integration-http-method POST --type AWS_PROXY --uri arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/[lambda function arn]/invocations – Andy N Apr 30 '20 at 00:42
  • 1
    GET is your API Gateway Http method. POST is http method API Gateway is using to call Lambda service. That's why for your "GET" method you need to still set integration HTTP method to "POST". – Lukas Liesis Jul 24 '21 at 07:56
7

To invoke a lambda function the http method should be POST as per the lambda API:

Visit http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html.

Kishor Unnikrishnan
  • 1,928
  • 4
  • 21
  • 33
0

The null in the source ARN is because test-invoke does not have a stage to associate with your API. This is not usually a problem, unless the policy on your Lambda function is expecting an incompatible source ARN. Check the policy on your Lambda function and see if it includes a restriction on the source ARN. If it does, edit it to be compatible with the test-invoke ARN or deploy the API to a stage and call the method directly without test-invoke.

MikeD at AWS
  • 3,565
  • 16
  • 15
0

Note carefully API Gateway use POST method by to call Lambda, while allowing any method to be able to call it via x-amazon-apigateway-any-method.

vsk.rahul
  • 433
  • 1
  • 6
  • 11