Finding and Verifying Optimisation Opportunities with AI Tools
This lesson explains how AI tools review code and telemetry for performance problems and wasted resources, and how to confirm every suggested optimisation with measurements.
Learning outcomes
After completing this lesson, you will be able to:
- Recognise common inefficient code patterns in AWS applications and their fixes.
- Explain how telemetry and AWS Lambda Power Tuning confirm a right-sizing suggestion.
- Describe how anomaly detection and investigations help find bottlenecks.
Code efficiency
Many performance problems in AWS applications come from a few recurring code patterns. An AI reviewer such as Kiro recognises them quickly, especially when a steering file lists the team's performance rules. The following table lists common patterns and the fix the reviewer should propose.
| Pattern | Effect | Improvement |
|---|---|---|
| SDK client or database connection created inside the handler | Extra latency on every invocation | Create it once during initialisation and reuse it |
| One API call per item in a loop | Many round trips and throttling | Batch operations such as BatchWriteItem or SendMessageBatch |
| Independent calls made one after another | Latency is the sum of all calls | Run them concurrently |
DynamoDB Scan with a filter | Reads and pays for the whole table | Query on the table or a secondary index |
| Whole S3 object read to use a small part | Data transfer and memory | Byte-range GetObject requests, or splitting data into smaller objects |
| Database connections opened by thousands of functions | Connection exhaustion | RDS Proxy |
Fix the pattern
Resource usage and right-sizing
AI tools are useful for reading large amounts of telemetry. Lambda writes a REPORT line for every invocation with its duration, billed duration, configured memory, maximum memory used and initialisation duration. A Logs Insights query, which the query generator can write from a plain-language description, summarises these values, and an AI assistant can suggest a memory setting from the result.
A suggestion is not a measurement. AWS Lambda Power Tuning, an open-source tool that runs as a Step Functions state machine, invokes the function at several memory settings and reports cost and duration for each, which confirms or rejects the suggestion. For containers, Container Insights reports CPU and memory utilisation per task, which shows whether task sizes are too large or too small.
Detecting bottlenecks
CloudWatch anomaly detection uses machine learning to learn each metric's normal pattern, including daily and weekly cycles, and alarms when the metric leaves the expected band, so a gradual latency regression is noticed without a fixed threshold. CloudWatch investigations then correlates the change with traces, logs, resource metrics and recent deployments, and X-Ray traces or Application Signals show which dependency adds the latency. The developer gives the findings, together with the relevant code, to Kiro to propose a fix.
Measuring every change
An optimisation that is not measured may move the bottleneck elsewhere, raise cost or change behaviour. The following steps keep AI-suggested optimisations honest.
-
Baseline
Record p50, p95 and p99 latency, error rate, throughput and cost under a repeatable load test.
-
Suggest
Give the AI tool the code and the telemetry, and ask for specific changes with the expected effect.
-
Apply in a test environment
Make one change at a time, so its effect can be attributed.
-
Measure again
Run the same load test and compare. Keep the change only if it improves the target metric without harming others.
Verify an optimisation
A slow order API
An order API's p95 latency has risen from 400 ms to 900 ms, and CloudWatch anomaly detection has raised an alarm.
An investigation shows the rise started with a release that added a call to a pricing service. The developer gives Kiro the handler, an X-Ray trace and the REPORT line summary. Kiro points out that the new release creates the pricing service's HTTP client inside the handler and calls the pricing and inventory services one after the other, and that the function's maximum memory used is close to its 512 MB setting.
The developer moves the client to initialisation, runs the two calls concurrently and runs Lambda Power Tuning, which shows that 1,024 MB costs about the same per request because duration halves. A repeat load test shows p95 at 350 ms. Each change was measured separately, so the team knows which one mattered most.
An AI assistant suggests doubling a function's memory. How should the developer decide?
Show answer
Measure it: run AWS Lambda Power Tuning or the same load test at both settings and compare duration and cost per request. Keep the change only if the numbers support it.
Exam Tips
Remember for the exam
- AI review finds patterns such as clients created in the handler or Scan instead of Query.
- Confirm an AI-suggested Lambda memory setting with AWS Lambda Power Tuning.
- Detect gradual regressions without fixed thresholds: CloudWatch anomaly detection.
- Measure before and after each change with the same load test.