0%
AWS Developer Associate (DVA-C03) Exam Prep: Development, Security, Testing, Deployment and Troubleshooting
How the Exam Works
Domain 1: Development with AWS Services
Domain 2: Security
Domain 3: Testing and Deployment
New in DVA-C03: Containers and AI
Final Review and Mock Exams
Monitoring and Tracing: CloudWatch, X-Ray and CloudTrail
Monitoring and troubleshooting together make up a domain worth 22 percent of DVA-C03, up from 18 percent in DVA-C02, and monitoring knowledge appears in questions from every domain, because a well built application is one you can observe. Three services do most of the work. CloudWatch collects metrics and logs and turns them into alarms and dashboards. X-Ray traces individual requests across services to show where time is spent and where errors start. CloudTrail records the AWS API calls made in an account, so you can tell who changed what. This lesson covers each from a developer's view: what you instrument in code, what you configure, and which tool answers which question. The next lesson uses these tools to troubleshoot specific failures, so here the focus is on how they work.
Namespaces, metric names and data points
A CloudWatch metric is a time ordered set of data points. Each metric lives in a namespace, such as AWS/Lambda for the metrics Lambda publishes, or a custom namespace you choose, such as Orders/Checkout. Within a namespace, a metric has a name, such as Duration, and zero or more dimensions. Each data point has a timestamp, a value and optionally a unit, such as Milliseconds or Count. You do not create metrics in advance: the first data point published for a new name and dimension combination creates it.
AWS services publish many metrics automatically. Lambda publishes Invocations, Errors, Throttles, Duration, ConcurrentExecutions and, for stream sources, IteratorAge. API Gateway publishes Count, Latency, IntegrationLatency, 4XXError and 5XXError. DynamoDB publishes consumed capacity, throttled requests and latency per operation. SQS publishes ApproximateNumberOfMessagesVisible and ApproximateAgeOfOldestMessage. EC2 publishes CPU, network and disk operations at five minute intervals by default, or one minute with detailed monitoring, but not memory or disk space used, because those are inside the guest operating system. Knowing what is published for free tells you when a custom metric or the CloudWatch agent is required.
Dimensions and cardinality
A dimension is a name and value pair that identifies a metric more precisely, such as FunctionName=checkout or Environment=prod. Each unique combination of namespace, metric name and dimension values is a separate metric series, stored and billed separately. That has two consequences. First, you can only retrieve a metric using exactly the dimensions it was published with; CloudWatch does not aggregate across dimensions for custom metrics unless you publish the aggregate too or use metric math or a search expression. Second, a dimension with many possible values, such as a user ID or request ID, creates a new series for every value, which multiplies cost and makes the metric useless for alarming.
Good dimensions have a small, bounded set of values: service, operation, environment, Region, customer tier. Per request detail, such as user ID or order ID, belongs in logs or in X-Ray annotations, where you can search by it without creating thousands of metrics. A question that describes a team publishing a latency metric with the customer ID as a dimension and complaining about cost is pointing at cardinality.
Publishing custom metrics with PutMetricData
The direct way to publish a custom metric is the PutMetricData API, from the SDK or CLI. A call can include many data points for one namespace, each with dimensions, a unit and a value, or a statistic set with sum, count, minimum and maximum that you aggregated yourself. Batching values into one call, or sending statistic sets, reduces API calls, which matters because PutMetricData has request quotas and each call costs money.
The caller needs cloudwatch:PutMetricData permission. That action does not support resource level permissions, so you cannot restrict it to one namespace by resource ARN; you can restrict it with the cloudwatch:namespace condition key. Data points can be published with timestamps up to two weeks in the past and two hours in the future.
Standard and high resolution metrics
By default, custom metrics are standard resolution: CloudWatch stores them at one minute granularity. If you set StorageResolution to 1 when publishing, the metric is high resolution, stored at one second granularity, so you can see and alarm on spikes that last a few seconds. High resolution alarms can evaluate periods of 10 or 30 seconds, at a higher price. Use high resolution only where seconds matter, such as a trading or real time bidding path; for most application metrics, one minute is enough.
Retention depends on resolution, and CloudWatch rolls data up over time: one second data points are kept for three hours, one minute data points for fifteen days, five minute data points for 63 days and one hour data points for 15 months. After that, the data is gone. If a stem needs metrics for longer than 15 months, the answer is to export them, for example with a metric stream to Amazon Data Firehose, formerly Kinesis Data Firehose, and S3.
Statistics, periods and percentiles
When you read or alarm on a metric, you choose a statistic and a period. Statistics include Sum, Average, Minimum, Maximum, SampleCount and percentiles such as p90 and p99. The period is the aggregation window, such as one minute or five minutes. Average latency hides the slow tail: an API with p50 of 40 milliseconds and p99 of 4 seconds has an average that looks fine while one user in a hundred waits four seconds. For latency, alarm on a percentile; for error counts, use Sum; for utilisation, Average or Maximum depending on the question.
Metric math lets you compute new series from existing ones, for example an error rate as 100 times Errors divided by Invocations, and alarm on the result. That is usually better than alarming on raw error counts, which rise with traffic even when nothing is wrong.
The CloudWatch agent
For EC2 instances and on-premises servers, the unified CloudWatch agent collects system level metrics that the hypervisor cannot see, such as memory used, disk space used and swap, and ships log files to CloudWatch Logs. It is configured with a JSON file, often stored in Parameter Store so many instances share one configuration, and it needs an instance profile with permission to publish metrics and logs. Questions that ask how to monitor memory utilisation on EC2 always point at the agent. The agent can also receive StatsD and collectd metrics from applications on the instance.
Metric streams and getting data out
GetMetricData is the API for reading metrics programmatically: one call can retrieve many series and metric math expressions over a time range, which is what dashboards and tools use. GetMetricStatistics is the older single metric call. For continuous export, a metric stream sends metrics in near real time through Kinesis Data Firehose to S3, an HTTP endpoint or a third party observability tool, in JSON or OpenTelemetry format. A stem that asks how to send all CloudWatch metrics to an external monitoring product continuously, with minimal code, points at a metric stream rather than a scheduled function that polls GetMetricData.
Lambda metrics worth knowing by name
Several Lambda metrics appear in questions by name. Invocations counts attempts, including those that error, but not throttled ones. Errors counts invocations that ended in a function error, such as an unhandled exception or a timeout. Throttles counts invocations rejected because concurrency was exhausted. Duration is the time the handler ran, and a Duration close to the configured timeout means invocations are being cut off. ConcurrentExecutions shows how close you are to account or reserved concurrency limits. IteratorAge, for Kinesis and DynamoDB stream sources, is the age of the last record processed; a rising value means the function is falling behind. For asynchronous invocations, AsyncEventAge and AsyncEventsDropped show events waiting or dropped, and DeadLetterErrors counts failures to write to the dead letter queue.
Worked example: choosing what to publish
A checkout service team wants to know revenue per minute by channel, error rate by operation and latency by operation, and to investigate individual slow orders. Revenue and latency are values over time with bounded dimensions, channel and operation, so they are custom metrics, published with EMF from the function. Error rate is a metric math expression over error and request counts. The slow orders themselves are per request detail: the order ID goes into the log line and, if tracing is on, into an X-Ray annotation, never into a dimension. That split, bounded dimensions for metrics and identifiers for logs and traces, is the pattern most metric design questions are testing.
EC2 metrics come from outside the guest, so CPU and network are free but memory and disk space are not. Any stem about memory or disk utilisation on EC2 needs the CloudWatch agent or a custom metric.