Sub topic 4
Connecting a route to Lambda with proxy integration
This chapter assumes you know what a route is and can write a basic Lambda handler. It covers the link between the two: the integration.
What proxy integration does
An integration tells API Gateway what to call when a route matches. With Lambda proxy integration (integration type AWS_PROXY), API Gateway does no request mapping. It packs the whole HTTP request into one event, invokes the function, and turns the function's return value into the HTTP response. Your code decides the status code, headers and body.
One integration can serve many routes. Each route points at an integration through its target.
Wiring it up by hand
SAM creates these resources for you, as the SAM chapter shows. Building them once with the CLI shows what SAM generates. Set the variables first.
REGION="us-east-1"
ACCOUNT_ID="123456789012"
API_ID="abc123"
FUNCTION="ItemsFunction"
FUNCTION_ARN="arn:aws:lambda:$REGION:$ACCOUNT_ID:function:$FUNCTION"
INTEGRATION_ID=$(aws apigatewayv2 create-integration \
--api-id "$API_ID" \
--integration-type AWS_PROXY \
--integration-uri "$FUNCTION_ARN" \
--payload-format-version 2.0 \
--query IntegrationId --output text)
aws apigatewayv2 create-route \
--api-id "$API_ID" \
--route-key 'GET /items/{id}' \
--target "integrations/$INTEGRATION_ID"
aws lambda add-permission \
--function-name "$FUNCTION" \
--statement-id apigw-get-item \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:$REGION:$ACCOUNT_ID:$API_ID/*/*/items/{id}"The third command matters most. API Gateway calls your function as a service principal, so the function's resource-based policy must allow it. The --source-arn limits that permission to your API and route. The execution role of the function is a separate thing. It controls what the function can call, not who can call it.
What the integration controls
| Setting | Value or behavior |
|---|---|
| Payload format version | `2.0` is the version this tutorial uses. `1.0` exists for compatibility. |
| Timeout | Defaults to 30 seconds, which is also the maximum for HTTP APIs. |
| Invocation type | Synchronous. The client waits for the function to finish. |
Common failures
- 500 Internal Server Error, no useful body. This usually means one of three things. The function raised an unhandled exception, the function returned something API Gateway cannot interpret, or the invoke permission is missing. Check CloudWatch Logs for the function first. If there is no log entry at all, suspect the permission.
- 200 with an unexpected body. With version 2.0, if the function returns valid JSON without a
statusCode, API Gateway treats the whole value as the body of a 200 response. Always return the full response shape so this never surprises you. - 404 from API Gateway. No route matched. The request never reached Lambda.
Exercise
A route returns 500 Internal Server Error, and the function's log group has no new entries for the request. What is the most likely cause, and how do you fix it?
Show answer
API Gateway probably could not invoke the function because its resource-based policy lacks an apigateway.amazonaws.com invoke permission for this API. Add the permission with aws lambda add-permission, using a source ARN that covers the API and route.