Build a Serverless API with API Gateway and Lambda
Create a Lambda function, expose it through an HTTP API, inspect execution logs, and understand the serverless pay-per-invocation model.
1. AWS Console → Lambda → Create function
2. Author from scratch
3. Name: hello-api
4. Runtime: Python 3.12
5. Architecture: x86_64 → Create function
Replace the default code in the inline editor (lambda_function.py) and Deploy:
import json
def lambda_handler(event, context):
name = event.get("queryStringParameters", {}).get("name", "world")
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"message": f"Hello, {name}!"})
}
1. Go to API Gateway → Create API → HTTP API → Build
2. Integration: Lambda → select hello-api
3. API name: hello-api
4. Route: GET /hello → Next → Create
1. API Gateway → hello-api → copy the Invoke URL
2. Open in browser: https://YOUR_URL/hello
3. Now with a parameter: https://YOUR_URL/hello?name=AWS
4. You should see the JSON response
# Or from a terminal:
curl "https://YOUR_INVOKE_URL/hello?name=AWS"
# Expected response:
# {"message": "Hello, AWS!"}
1. Lambda → hello-api → Monitor tab → View logs in CloudWatch
2. Open the newest log stream — see the START/END/REPORT lines
3. Note: Duration, Billed Duration, Memory Used, Init duration (cold start)
1. API Gateway → hello-api → Delete
2. Lambda → hello-api → Actions → Delete
Delete the /aws/lambda/hello-api CloudWatch log group too.