I used an AWS CloudFormation template to create an Amazon API Gateway REST API with AWS Lambda integration. When I use the API method to invoke my Lambda function, I get an error message and a 500 status code.
Short description
If you have an API Gateway REST API with Lambda integration, then the API must use the POST HTTP method to invoke the backend Lambda function. If you use any other HTTP method for the backend integration request, such as ANY or GET, then invocation fails. API Gateway then returns an error message similar to the following example in Amazon CloudWatch Logs:
Mon Oct 14 14:08:49 UTC 2019 : Received response. Status: 403, Integration latency: 3 msMon Oct 14 14:08:49 UTC 2019 : Endpoint response headers: {Date=Mon, 14 Oct 2019 14:08:49 GMT, Content-Length=130, Connection=keep-alive, x-amzn-RequestId=abc1d2ef-34ab-56c7-de8f-90123a456789}
Mon Oct 14 14:08:49 UTC 2019 : Endpoint response body before transformations: <AccessDeniedException>
<Message>Unable to determine service/operation name to be authorized</Message>
</AccessDeniedException>
Mon Oct 14 14:08:49 UTC 2019 : Lambda invocation failed with status: 403. Lambda request id: abc1d2ef-34ab-56c7-de8f-90123a456789
Mon Oct 14 14:08:49 UTC 2019 : Execution failed due to configuration error:
Mon Oct 14 14:08:49 UTC 2019 : Method completed with status: 500
Note: You can still set up any HTTP method for the REST API frontend.
If you create a REST API with Lambda integration, then use one of the following methods to specify POST for the backend integration request:
Note: If you use the API Gateway console to configure a Lambda integration, then the backend integration request is automatically set to POST.
Resolution
Note: If you receive errors when you run AWS CLI commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
It's a best practice to manage all stack resources though AWS CloudFormation and not to make stack resource changes outside of CloudFormation.
The method that you use to change the HTTP method to POST for the backend integration request depends on the original template definition. The process to update an API Gateway deployed by CloudFormation is different than the process to update an API Gateway created by OpenAPI.
API Gateways created in CloudFormation
For API Gateways that CloudFormation created, you must update the stack template. If you defined the AWS::ApiGateway::Method resource in your CloudFormation template to create your method, then update the HttpMethod property to POST. For instructions, see Examples.
If your method uses an OpenAPI definition for the Body property of the AWS::ApiGateway::RestAPI resource, then set httpMethod in your API definition file to POST. For instructions, see x-amazon-apigateway-integration object. Also, see the example Swagger template on the aws-samples GitHub repository.
To update your API, run AWS CloudFormation stack updates with the edited template:
"x-amazon-apigateway-integration" : { "type" : "aws",
"httpMethod" : "POST"
API Gateways created outside of CloudFormation
Use the console to update the method
Complete the following steps:
- Open the API Gateway console.
- Choose your API.
- In Resources, choose the HTTP method that's integrated with Lambda.
- In Method Execution, choose Integration Request.
- In Integration Request, for HTTP method, edit the Lambda function name.
- When prompted to Add Permission to Lambda Function, choose OK.
- Deploy your API.
- (Optional) Test the HTTP method that's integrated with Lambda.
Note: Each time you select OK in the Add Permission to Lambda Function prompt, the console adds a new statement to the Lambda function's resource policy. Make sure that you don't exceed the resource policy size limit. For more information, see Function configuration, deployment, and execution and Set up an API integration request using the API Gateway console.
Use the AWS CLI to update the method
Complete the following steps:
-
To update the integration HTTP method to POST, run the put-integration command:
aws apigateway put-integration \--rest-api-id 1234123412 \
--resource-id a1b2c3 \
--http-method ANY \
--type AWS \
--integration-http-method POST \
--uri 'arn:aws:apigateway:us-west-2:lambda:path//2015-03-31/functions/arn:aws:lambda:us-east-1:123412341234:function:function_name/invocations
-
To deploy the configured resources for an API to an existing stage, run the create-deployment command:
aws apigateway create-deployment \--rest-api-id 1234123412 \
--stage-name dev \
--description 'Deployment to an existing dev stage'
Use an OpenAPI definition import to update the method
Complete the following steps:
- In your API definition file, set the httpMethod property value to POST. For instructions, see x-amazon-apigateway-integration object and the example Swagger template on the aws-samples GitHub repository.
- To update your API, import the edited API definition file into API Gateway. See Import an OpenAPI file to update an existing API definition.
Related information
AWS::ApiGateway::Method Integration
Tutorial: Create a calculator REST API with two AWS service integrations and one Lambda non-proxy integration
I defined my Lambda integration in API Gateway using a stage variable. Why do I get an "Internal server error" and a 500 status code when I invoke the API method?
How do I resolve HTTP 502 errors from API Gateway REST APIs with Lambda proxy integration?
How do I resolve "Invalid mapping expression specified" errors from API Gateway?