Custom Metrics and CloudWatch Alarms
Learn to emit your own business and performance metrics from Lambda and to trigger alarms and notifications when thresholds are crossed.
Custom Metrics and CloudWatch Alarms is a free Serverless AWS Lambda Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Serverless AWS Lambda Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Default Metrics
Lambda publishes built-in metrics like invocations and errors, but your app has its own signals, such as orders processed or items failed. These are custom metrics.
Embedded Metric Format
The easiest way to emit metrics is the Embedded Metric Format (EMF): print structured JSON to logs and CloudWatch extracts metrics automatically.
{
"_aws": {
"CloudWatchMetrics": [{
"Namespace": "OrdersApp",
"Metrics": [{"Name": "OrdersProcessed", "Unit": "Count"}]
}]
},
"OrdersProcessed": 12
}Emitting EMF in Python
Build the EMF object and print it as JSON. No extra API call or latency is needed.
import json, time
def emit(count):
print(json.dumps({
'_aws': {
'Timestamp': int(time.time() * 1000),
'CloudWatchMetrics': [{
'Namespace': 'OrdersApp',
'Dimensions': [[]],
'Metrics': [{'Name': 'OrdersProcessed', 'Unit': 'Count'}]
}]
},
'OrdersProcessed': count
}))PutMetricData API
Alternatively call put_metric_data directly. This is synchronous and adds latency, so EMF is usually preferred for hot paths.
import boto3
cw = boto3.client('cloudwatch')
cw.put_metric_data(Namespace='OrdersApp',
MetricData=[{'MetricName': 'OrdersProcessed', 'Value': 1}])Dimensions Add Context
Dimensions are key-value tags that slice a metric, such as by region or customer tier. Keep dimension cardinality low to control cost.
What an Alarm Does
A CloudWatch alarm watches a metric and changes state when it breaches a threshold for a set number of periods, then triggers an action.
Creating an Alarm
This alarm fires when the error count exceeds 5 in a single one-minute period.
aws cloudwatch put-metric-alarm \
--alarm-name orders-errors \
--metric-name Errors \
--namespace AWS/Lambda \
--threshold 5 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--period 60Notifying with SNS
Point the alarm action at an SNS topic so it emails, texts, or pages your on-call when triggered.
Alarm States
Alarms have three states: OK, ALARM, and INSUFFICIENT_DATA. The last means the metric had no data points in the window.
Avoid Alarm Fatigue
Too many noisy alarms train people to ignore them. Alarm on symptoms users feel, like error rate and latency, not every minor metric.
Dashboards Tie It Together
Build a CloudWatch dashboard combining built-in and custom metrics so you see the whole system health at a glance during an incident.
Quick Check
Test your metrics knowledge.
Recap
You learned to emit custom metrics via EMF or PutMetricData, add dimensions, create threshold alarms wired to SNS, and avoid alarm fatigue with dashboards.
Frequently asked questions
Is the “Custom Metrics and CloudWatch Alarms” lesson free?
Yes — the full text of “Custom Metrics and CloudWatch Alarms” is free to read here on the web, and the Serverless AWS Lambda Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Serverless AWS Lambda Development course, upgrade to CoddyKit PRO.
What will I learn in “Custom Metrics and CloudWatch Alarms”?
Learn to emit your own business and performance metrics from Lambda and to trigger alarms and notifications when thresholds are crossed. You practise Serverless AWS Lambda Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Serverless AWS Lambda Development?
No prior experience is required. Serverless AWS Lambda Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Custom Metrics and CloudWatch Alarms” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Serverless AWS Lambda Development lesson?
Yes. Every Serverless AWS Lambda Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- CloudWatch Logs and Metrics
- Error Handling and Retries
- Debugging Serverless Applications
- Custom Metrics and CloudWatch Alarms