How to Integrate API Gateway with Lambda in AWS
To integrate
API Gateway with Lambda, create an API Gateway REST API or HTTP API and configure a method or route to trigger your Lambda function. This setup allows API Gateway to invoke Lambda when an HTTP request is received, enabling serverless backend logic.Syntax
The integration involves these parts:
- API Gateway API: Defines the HTTP endpoint.
- Resource and Method (REST API) or Route (HTTP API): Specifies the URL path and HTTP method (GET, POST, etc.).
- Integration: Connects the method or route to the Lambda function.
- Lambda Function: The backend code executed on request.
API Gateway forwards the request to Lambda and returns Lambda's response to the client.
bash
aws apigateway create-rest-api --name "MyAPI" aws apigateway create-resource --rest-api-id <api-id> --parent-id <root-id> --path-part "mypath" aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --authorization-type NONE aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda-arn>/invocations aws lambda add-permission --function-name <lambda-name> --statement-id apigateway-access --action lambda:InvokeFunction --principal apigateway.amazonaws.com --source-arn arn:aws:execute-api:<region>:<account-id>:<api-id>/*/GET/mypath
Example
This example shows how to create a simple HTTP API in AWS Console that triggers a Lambda function on GET requests to /hello.
The Lambda function returns a JSON greeting.
python
import json def lambda_handler(event, context): return { 'statusCode': 200, 'headers': {'Content-Type': 'application/json'}, 'body': json.dumps({'message': 'Hello from Lambda!'}) }
Output
{"message": "Hello from Lambda!"}
Common Pitfalls
- Missing Lambda permission: API Gateway must have permission to invoke Lambda. Use
lambda add-permission. - Wrong integration type: Use
AWS_PROXYfor Lambda proxy integration to pass request details. - Incorrect URI format: The Lambda ARN in API Gateway integration URI must be exact.
- Not deploying API: After changes, deploy the API to a stage to make it live.
bash
Wrong: aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --type AWS --integration-http-method POST --uri arn:aws:lambda:<region>:<account-id>:function:<lambda-name> Right: aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --type AWS_PROXY --integration-http-method POST --uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:<account-id>:function:<lambda-name>/invocations
Quick Reference
Remember these key steps:
- Create API Gateway API and resource.
- Set method and integration type to
AWS_PROXY. - Use correct Lambda ARN format in integration URI.
- Grant API Gateway permission to invoke Lambda.
- Deploy API to a stage before testing.
Key Takeaways
Use AWS_PROXY integration type for full Lambda request and response handling.
Grant API Gateway permission to invoke your Lambda function explicitly.
Deploy your API Gateway changes to a stage to make them active.
Ensure the Lambda ARN in the integration URI is correctly formatted.
Test your API endpoint after deployment to verify Lambda integration.