Troubleshooting and Optimization
What you must know
Separate the observability tools clearly. CloudWatch holds metrics, logs, alarms, dashboards, Logs Insights queries, and metric filters. CloudTrail records API activity for auditing. X-Ray traces request paths with segments and subsegments and answers where latency lives. VPC Flow Logs capture network-level accepts and rejects. AWS Health reports service events.
Performance tuning starts with the right lever. Lambda memory also sets CPU share, so a slower function is often fixed by raising memory; cold starts improve with provisioned concurrency or SnapStart. Initialize SDK clients and database connections outside the handler so they are reused across invocations. DynamoDB throttling usually means a hot partition or insufficient capacity, not just low provisioned RCUs, and adaptive capacity helps but does not fix bad key design. S3 scales request rates per prefix, so spread keys and use multipart upload for large objects. API Gateway caches responses and has a 29-second integration timeout for REST APIs, which shows up as a 504 when a Lambda runs too long.
Traps
Confusing CloudWatch with CloudTrail; assuming X-Ray stores logs; retrying immediately on throttling; blaming capacity when the real cause is a hot partition; and misreading a 504 as a code error.
Rule of thumb
On any throttling error (HTTP 429, ProvisionedThroughputExceededException, SlowDown), retry with exponential backoff and jitter, cap the attempts, and send poison messages to a DLQ. Confirm with an X-Ray subsegment before optimizing anything else.