Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ameyp opened this issue Sep 1, 2021 · 3 comments · Fixed by #16333
Closed

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

ameyp opened this issue Sep 1, 2021 · 3 comments · Fixed by #16333
Assignees
Labels
@aws-cdk/aws-cloudwatch Related to Amazon CloudWatch bug This issue is a bug. effort/small Small work item – less than a day of effort in-progress This issue is being actively worked on. p1

Comments

@ameyp
Copy link

ameyp commented Sep 1, 2021

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

@ameyp ameyp added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 1, 2021
@github-actions github-actions bot added the @aws-cdk/aws-cloudwatch Related to Amazon CloudWatch label Sep 1, 2021
@madeline-k
Copy link
Contributor

Thanks for bringing this issue to our attention, @ameyp!

Can you please provide the CDK code of your stack that reproduces the issue as well?

@madeline-k madeline-k added effort/small Small work item – less than a day of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Sep 1, 2021
@madeline-k madeline-k assigned kaizencc and unassigned madeline-k Sep 1, 2021
@kaizencc kaizencc added the in-progress This issue is being actively worked on. label Sep 1, 2021
@ameyp
Copy link
Author

ameyp commented Sep 2, 2021

Thanks for bringing this issue to our attention, @ameyp!

Can you please provide the CDK code of your stack that reproduces the issue as well?

Whoops, looks like I originally pasted the path to my stack code instead of the code by mistake. I've updated the bug with my example.

@mergify mergify bot closed this as completed in #16333 Sep 7, 2021
mergify bot pushed a commit that referenced this issue Sep 7, 2021
…ns (#16333)

Now they do. Closes #16331. 

I have also modified the comment to explain `returnData: entry.tag ? undefined : false` but I endeavor to explain even more here. Only 1 metric within an expression can have `returnData = true`; the rest must be `false`. Cloudformation also defaults an undefined return data to `true` as long as the rest are set to `false`, which is why this ternary operation works. The idea behind this line is that `entry.tag` is defined (with a default of `true`) for the top level expression only and thus every other metric within the expression has `returnData = false`. 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Sep 7, 2021

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-cloudwatch Related to Amazon CloudWatch bug This issue is a bug. effort/small Small work item – less than a day of effort in-progress This issue is being actively worked on. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants