# Exercise 2: Debug Five API Gateway Problems

For each symptom, identify the most likely cause (using `aws_api_gateway_theory.md`), how you would confirm it (which log field/metric), and the fix.

1. **Mobile users intermittently get `504 Endpoint request timed out`** on `POST /reports`. Lambda logs show the function finishing successfully after 34–40 seconds.
2. **Browser calls fail** with "No 'Access-Control-Allow-Origin' header" only for `PUT` and `DELETE`, while `GET` works. Lambda is never invoked for the failing calls.
3. **A partner's integration returns `429 Too Many Requests`** although they send only 5 req/s. At the same moment, an internal batch job is calling a different route of the same API at 9,800 req/s.
4. **`Latency` p99 is 1,900 ms but `IntegrationLatency` p99 is 140 ms** on an HTTP API with a Lambda authorizer.
5. **Uploading 15 MB CSV files fails with `413`**; customers want to upload files up to 200 MB.

## Deliverables

For each: **cause**, **how to confirm**, **fix**. Then write the JSON access-log format you would configure so that all five could be diagnosed from logs.

## Requirements Checklist

- [ ] 1: 29-second integration timeout; confirm with `integrationLatency` ≈ 29,000 and Lambda duration > 29 s; fix with async processing (202 + job status) or splitting work.
- [ ] 2: CORS preflight `OPTIONS` not configured/allowed for PUT/DELETE (non-simple requests) — confirm OPTIONS 4xx in access logs; fix CORS config (allowed methods/headers) on the API.
- [ ] 3: account/regional throttle shared across routes (token bucket 10k rps) exhausted by the batch job; fix with route/stage throttles, separate API or quota increase, and batching.
- [ ] 4: uncached Lambda authorizer (cold starts) adds latency outside the integration; fix with authorizer result caching (identity source + TTL), JWT authorizer, or provisioned concurrency.
- [ ] 5: 10 MB payload limit; fix with presigned S3 multipart uploads and processing via S3 events.
- [ ] The log format includes `requestId`, `routeKey`, `status`, `responseLatency`, `integrationLatency`, `integrationErrorMessage`, `authorizerError`, `errorMessage`, caller identity/key.
