Skip to content

Bug: CloudWatchAlarmMetric.metric_stat raises KeyError for metric math expression entries #8673

Closed

Description

@thebadalov

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'

Activity

  1. thebadalov commented on Oct 26, 2026

    @thebadalov
    ContributorAuthor

    I'd like to work on this one. I already have the fix and a test ready: metric_stat returns None for expression entries, and the existing single metric test now checks it on the e1 entry from the fixture. Could you assign it to me? I'll open the PR once it's triaged.

  2. added this to the 3.39.0 milestone on Oct 28, 2026
  3. powertools-for-aws-oss-automation commented on Oct 29, 2026

    @powertools-for-aws-oss-automation

    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.

  4. github-actions commented on Nov 3, 2026

    @github-actions
    Contributor

    This is now released under 3.39.0 version!

  5. removed
    pending-releaseFix or implementation already in dev waiting to be released
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingevent_sourcesEvent Source Data Class utility

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions