Review the scope of API Gateway permission to invoke Lambda

Limit API Gateway permission to invoke Lambda to the required stage, method, and path.

Description

Restrict API Gateway’s permission to invoke Lambda with source_arn. Configure client authentication and authorization separately on the API method.

Potential impact

If the source is broad, another stage or route connected to the same function may invoke it.

Remediation

Append the required stage, HTTP method and resource path to the execution ARN. Verify that only the intended integration can invoke the function.

Examples

Both excerpts use the AllowAPIGatewayInvoke statement for the same function. Supply the referenced REST API and function.

Before

hcl
resource "aws_lambda_permission" "example" {
  statement_id  = "AllowAPIGatewayInvoke"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.example.function_name
  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_api_gateway_rest_api.example.execution_arn}/*/*"
}

After

hcl
resource "aws_lambda_permission" "example" {
  statement_id  = "AllowAPIGatewayInvoke"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.example.function_name
  principal     = "apigateway.amazonaws.com"
  source_arn    = "${aws_api_gateway_rest_api.example.execution_arn}/prod/GET/orders"
}

Replace /prod/GET/orders in the second ARN with the deployed stage, method and path.

References