Skip to content

cloudwatch: Alarm on MathExpression that has a cross-account metric skips accountId #16331

@ameyp

Description

@ameyp

With the newly-added cross-account alarm support, I can now create an alarm on a simple cross-account metric. However, if I create a MathExpression with the same cross-account metric and then create an alarm on the MathExpression, the metric used in the resulting alarm is not cross-account.

Reproduction Steps

  1. Install aws-cdk@1.121.0
  2. Declare a dependency on monocdk@1.120.0 in your package.json
  3. Create a cross-account metric
  4. Create a MathExpression using this cross-account metric
  5. Create an Alarm in your stack using this MathExpression
  6. Inspect the generated CloudFormation template
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from 'monocdk';
import * as cloudwatch from "monocdk/aws-cloudwatch"

class CdkPlaygroundStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const metric1 = new cloudwatch.Metric({
      account: "123456789",
      namespace: "TestNamespace",
      metricName: "TestMetric.Count",
      period: cdk.Duration.minutes(1),
      dimensions: {},
      statistic: "sum",
    });

    const metric2 = new cloudwatch.MathExpression({
      expression: "m",
      period: cdk.Duration.minutes(1),
      usingMetrics: {
        m: metric1,
      },
    });

    metric2.createAlarm(this, "TestMathAlarm2", {
      datapointsToAlarm: 5,
      evaluationPeriods: 5,
      threshold: 100,
      comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
      treatMissingData: cloudwatch.TreatMissingData.BREACHING,
      alarmDescription: "Description",
    });
  }
}

const app = new cdk.App();
new CdkPlaygroundStack(app, 'CdkPlaygroundStack', {
});

What did you expect to happen?

I expected the generated CloudFormation template to contain the accountId in my cross-region metric.

What actually happened?

The generated CloudFormation template does not contain the accountId in the metric. Here's the relevant alarm resource:

    "TestMathAlarm2DA396B5F": {
      "Type": "AWS::CloudWatch::Alarm",
      "Properties": {
        "ComparisonOperator": "GreaterThanOrEqualToThreshold",
        "EvaluationPeriods": 5,
        "AlarmDescription": "Description",
        "DatapointsToAlarm": 5,
        "Metrics": [
          {
            "Expression": "m",
            "Id": "expr_1"
          },
          {
            "Id": "m",
            "MetricStat": {
              "Metric": {
                "MetricName": "TestMetric.Count",
                "Namespace": "TestNamespace"
              },
              "Period": 60,
              "Stat": "Sum"
            },
            "ReturnData": false
          }
        ],
        "Threshold": 100,
        "TreatMissingData": "breaching"
      },
      "Metadata": {
        "aws:cdk:path": "CdkPlaygroundStack/TestMathAlarm2/Resource"
      }
    }

Environment

  • CDK CLI Version : 1.121.0
  • Framework Version: 1.121.0
  • Node.js Version: v12.22.1
  • OS : macOS Catalina 10.15.7
  • Language (Version): TypeScript 4.4.2

Other


This is 🐛 Bug Report

Metadata

Metadata

Labels

@aws-cdk/aws-cloudwatchRelated to Amazon CloudWatchbugThis issue is a bug.effort/smallSmall work item – less than a day of effortin-progressThis issue is being actively worked on.p1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions