How to Trigger AWS Lambda from an API Gateway GET Request (Step-by-Step)
Exposing a Lambda function as an HTTP endpoint is one of the most common serverless patterns — yet misconfigured integrations, missing permissions, and route confusion are the top reasons developers hit 500 errors on day one. This guide walks you through creating an API Gateway HTTP API with a GET route that invokes a Lambda function, covering every layer from IAM to testing.
TL;DR
| Step | What You Do | Key Detail |
|---|---|---|
| 1 | Create the Lambda function | Return a valid HTTP response object |
| 2 | Create an HTTP API in API Gateway | Use HTTP API (v2), not REST API (v1) |
| 3 | Create a GET route | e.g., GET /hello |
| 4 | Attach Lambda integration | AWS_PROXY integration type |
| 5 | Grant invoke permission | Resource-based policy on Lambda |
| 6 | Deploy & test | Use the auto-generated invoke URL |
Architecture Overview
Before diving into code, understand the request flow. API Gateway acts as the front door — it receives the HTTP GET request, routes it to the correct integration, and proxies the event payload to Lambda.
- Client sends an HTTP GET request to the API Gateway invoke URL.
- API Gateway (HTTP API) matches the request to the
GET /helloroute. - The route's Lambda Proxy Integration forwards the full event context to the Lambda function.
- Lambda executes and returns a structured response object.
- API Gateway translates the Lambda response back into an HTTP response to the client.
Analogy: Think of API Gateway as a hotel concierge. The client (guest) makes a request at the front desk. The concierge (API Gateway) routes it to the right department (Lambda). The department does the work and hands the result back through the concierge — the guest never interacts with the back office directly.
Step 1: Create the Lambda Function
Your Lambda function must return a response object that API Gateway's proxy integration understands. The required fields are statusCode and body.
🔽 Python Lambda Handler (Click to expand)
import json
def lambda_handler(event, context):
# 'event' contains the full HTTP request context
query_params = event.get("queryStringParameters") or {}
name = query_params.get("name", "World")
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": f"Hello, {name}!"
})
}
Deploy via AWS CLI:
# 1. Package the function
zip function.zip lambda_function.py
# 2. Create the Lambda function
aws lambda create-function \
--function-name hello-api \
--runtime python3.12 \
--role arn:aws:iam::123456789012:role/lambda-basic-execution-role \
--handler lambda_function.lambda_handler \
--zip-file fileb://function.zip \
--region us-east-1
IAM Role requirement: The execution role (lambda-basic-execution-role) must have at minimum the AWSLambdaBasicExecutionRole managed policy attached, which grants CloudWatch Logs write access.
Step 2: Create the HTTP API
API Gateway offers two API types: REST API (v1) and HTTP API (v2). For simple Lambda proxy integrations, HTTP API is recommended — it is lower latency and lower cost.
aws apigatewayv2 create-api \
--name hello-http-api \
--protocol-type HTTP \
--region us-east-1
Note the ApiId returned in the response — you will need it in subsequent steps. Example: abc1234xyz.
Step 3: Create the Lambda Integration
The integration connects the API route to your Lambda function. HTTP API uses AWS_PROXY as the integration type for Lambda, which passes the full request context as the event payload.
aws apigatewayv2 create-integration \
--api-id abc1234xyz \
--integration-type AWS_PROXY \
--integration-uri arn:aws:lambda:us-east-1:123456789012:function:hello-api \
--payload-format-version 2.0 \
--region us-east-1
Note the IntegrationId returned (e.g., xyz9876abc).
Payload Format Version:
2.0— Recommended for HTTP APIs. Simpler event structure, supports Lambda response format 2.0 (no need to wrap body in JSON string if using the simplified format).1.0— Compatible with REST API event format. Use this if migrating from REST API.
Step 4: Create the GET Route
A route maps an HTTP method + path to an integration. Here we map GET /hello to the Lambda integration created above.
aws apigatewayv2 create-route \
--api-id abc1234xyz \
--route-key "GET /hello" \
--target "integrations/xyz9876abc" \
--region us-east-1
Step 5: Grant API Gateway Permission to Invoke Lambda
This is the most commonly missed step. API Gateway must have explicit permission to invoke your Lambda function via a resource-based policy on the Lambda function itself.
aws lambda add-permission \
--function-name hello-api \
--statement-id allow-apigw-invoke \
--action lambda:InvokeFunction \
--principal apigateway.amazonaws.com \
--source-arn "arn:aws:execute-api:us-east-1:123456789012:abc1234xyz/*/*/hello" \
--region us-east-1
Source ARN breakdown:
| Segment | Value | Meaning |
|---|---|---|
| Service | execute-api | API Gateway execution service |
| Region | us-east-1 | Region of the API |
| Account | 123456789012 | Your AWS Account ID |
| API ID | abc1234xyz | Your HTTP API ID |
| Stage/Method/Path | */*/* or */GET/hello | Wildcard or specific route |
Least Privilege Tip: Use */GET/hello instead of */*/* to restrict the permission to only the specific route and method.
Step 6: Deploy the API (Create a Stage)
HTTP APIs require a stage to be deployed. The $default stage is auto-created with auto-deploy enabled when using the console, but via CLI you must create it explicitly.
aws apigatewayv2 create-stage \
--api-id abc1234xyz \
--stage-name "$default" \
--auto-deploy \
--region us-east-1
Step 7: Test the Endpoint
Retrieve the invoke URL and test with curl:
# Get the invoke URL
aws apigatewayv2 get-api \
--api-id abc1234xyz \
--region us-east-1 \
--query "ApiEndpoint" \
--output text
# Output: https://abc1234xyz.execute-api.us-east-1.amazonaws.com
# Test the GET route
curl "https://abc1234xyz.execute-api.us-east-1.amazonaws.com/hello?name=Engineer"
# Expected response:
# {"message": "Hello, Engineer!"}
Request Flow: Sequence Diagram
- Client sends
GET /hello?name=Engineerto the API Gateway endpoint. - API Gateway matches the route and checks the Lambda integration.
- API Gateway invokes the Lambda function, passing the full HTTP event as the payload.
- Lambda executes the handler and returns a structured response object.
- API Gateway translates the response and returns HTTP 200 with the JSON body to the client.
Common Errors & Fixes
| Error | Root Cause | Fix |
|---|---|---|
500 Internal Server Error | Lambda response missing statusCode | Ensure handler returns {"statusCode": 200, "body": "..."} |
403 Forbidden | Missing resource-based policy on Lambda | Run aws lambda add-permission (Step 5) |
404 Not Found | Route key mismatch or stage not deployed | Verify route key and deploy the $default stage |
Integration not found | Wrong IntegrationId in route target | Prefix target with integrations/ |
Glossary
| Term | Definition |
|---|---|
| HTTP API (v2) | The newer, lower-cost API Gateway offering optimized for Lambda and HTTP backends. |
| AWS_PROXY Integration | Passes the raw HTTP request as a structured event to Lambda; Lambda controls the full response. |
| Payload Format Version | Defines the schema of the event object sent to Lambda (1.0 = REST API format, 2.0 = HTTP API format). |
| Resource-Based Policy | A policy attached to a Lambda function that grants external services (like API Gateway) permission to invoke it. |
| Stage | A named reference to a deployment of your API (e.g., $default, prod). |
Next Steps
- Add JWT Authorizers to secure your route: HTTP API JWT Authorizer Docs
- Enable CloudWatch access logging on the stage for observability.
- Use AWS SAM or Terraform to manage this infrastructure as code for repeatable deployments.
- Official reference: AWS API Gateway HTTP API Developer Guide
Related Posts
- 📄 Lambda Proxy Integration vs. Standard Integration in API Gateway: A Deep Dive
- 📄 Fixing API Gateway CORS Errors: Enable CORS in the Console & Required Lambda Response Headers
- 📄 Eliminating Lambda Cold Starts: A Deep Dive into Provisioned Concurrency & SnapStart
- 📄 Fixing AWS Lambda Timeout: How to Increase It and What the Maximum Limit Is
- 📄 Lambda 403 on S3: Diagnosing & Fixing Execution Role Permissions
Comments
Post a Comment