SAA-C03 Intermediate 35 min

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.

AWS LambdaAmazon API GatewayAmazon CloudWatch
Progress: 0 / 6 steps
Step 1
Create the Lambda Function

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

Tip: A Lambda function is just code + a runtime + permissions (execution role). AWS manages everything underneath.
Step 2
Add the API Code

Replace the default code in the inline editor (lambda_function.py) and Deploy:

python
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}!"})
    }
Tip: Always click Deploy after editing — forgetting to deploy is the #1 Lambda beginner mistake.
Step 3
Create the HTTP API

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

Tip: HTTP APIs are cheaper and simpler than REST APIs. REST APIs add features like request validation and API keys.
Step 4
Test the API

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

bash
# Or from a terminal:
curl "https://YOUR_INVOKE_URL/hello?name=AWS"

# Expected response:
# {"message": "Hello, AWS!"}
Tip: If you get a permission error, API Gateway may need to be re-deployed or the integration re-selected so it grants invoke rights to Lambda.
Step 5
Observe Logs and Metrics

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)

Tip: The first request after a pause shows init duration — the cold start. Subsequent warm invocations skip it.
Step 6
Clean Up

1. API Gateway → hello-api → Delete
2. Lambda → hello-api → Actions → Delete

Delete the /aws/lambda/hello-api CloudWatch log group too.

Tip: Idle Lambda functions and HTTP APIs cost essentially nothing, but cleaning up avoids clutter and surprise log-storage charges.