Repository navigation
Description
Expected Behaviour
For a metric math alarm, alarm_data.configuration.metrics contains both expression entries and metric entries. An expression entry has expression and no metricStat, so CloudWatchAlarmMetric.metric_stat should return None for it, the same way expression and label already return None when they are missing.
Current Behaviour
metric_stat reads self["metricStat"] directly, so it raises KeyError: 'metricStat' on every expression entry. Any loop over the metrics that reads metric_stat fails on the first expression, even though the event is valid.
The repo's own fixture tests/events/cloudWatchAlarmEventSingleMetric.json has this shape: its first metric (e1, "expression": "m1/m2") has no metricStat.
Code snippet
from aws_lambda_powertools.utilities.data_classes import CloudWatchAlarmEvent
event = CloudWatchAlarmEvent(
{
"source": "aws.cloudwatch",
"alarmArn": "arn:aws:cloudwatch:eu-west-1:123456789012:alarm:invocations-ratio",
"accountId": "123456789012",
"time": "2024-02-17T11:53:08.431+0000",
"region": "eu-west-1",
"alarmData": {
"alarmName": "invocations-ratio",
"state": {"value": "ALARM", "reason": "Threshold Crossed", "timestamp": "2024-02-17T11:53:08.431+0000"},
"previousState": {"value": "OK", "reason": "Threshold Crossed", "timestamp": "2024-02-17T11:51:31.460+0000"},
"configuration": {
"metrics": [
{"id": "e1", "expression": "m1/m2", "label": "Expression1", "returnData": True},
{
"id": "m1",
"metricStat": {
"metric": {"namespace": "AWS/Lambda", "name": "Invocations", "dimensions": {}},
"period": 60,
"stat": "SampleCount",
},
"returnData": False,
},
],
},
},
},
)
for metric in event.alarm_data.configuration.metrics:
print(metric.metric_id, metric.metric_stat) # KeyError: 'metricStat' on "e1"Possible Solution
Return None when metricStat is missing, like RequestContextClientCert and other optional nested wrappers in the data classes do:
@property
def metric_stat(self) -> CloudWatchAlarmMetricStat | None:
metric_stat = self.get("metricStat")
return None if metric_stat is None else CloudWatchAlarmMetricStat(metric_stat)Entries that have metricStat behave the same as today. I'm happy to send a PR with a test for the expression entry.
Steps to Reproduce
Run the snippet above, or load tests/events/cloudWatchAlarmEventSingleMetric.json and read metric_stat on the first metric.
Powertools for AWS Lambda (Python) version
latest (3.37.0, also on develop)
AWS Lambda function runtime
3.13
Packaging format used
PyPi
Debugging logs
KeyError: 'metricStat'
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsComing soon
Activity
thebadalov commentedon Oct 26, 2026
I'd like to work on this one. I already have the fix and a test ready:
metric_statreturnsNonefor expression entries, and the existing single metric test now checks it on thee1entry from the fixture. Could you assign it to me? I'll open the PR once it's triaged.powertools-for-aws-oss-automation commentedon Oct 29, 2026
Warning
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
github-actions commentedon Nov 3, 2026
This is now released under 3.39.0 version!