aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.39k stars 3.79k forks source link

Attempting to create AWS Cloudwatch Dashboard, and specify colors for each metric fails. #5026

Closed ghost closed 4 years ago

ghost commented 4 years ago

When trying to add color parameter under Cloudwatch Dashboard graphwidget metric, it fails on deployment time.

Error message from stack deploy attempt is: Should NOT have more than 2 items

CDK API Documentation states that it should accept color parameter under metric, and this color: 'red' paramer works in Cloudwatch json code if it is added under metric.

Reproduction Steps

This code succeeds without errors:

dashboard.addWidgets(
  new GraphWidget({
    title: 'Log errors, warnings and restarts',
    width: 24,
    stacked: true,
    left: [
      new Metric({
        label: 'Errors',
        namespace: 'ECSLogs',
        metricName: 'MyCustomErrorCountMetric',
        statistic: 'Sum'
      }),          
    ]
  })
);

But this fails:

dashboard.addWidgets(
  new GraphWidget({
    title: 'Log errors, warnings and restarts',
    width: 24,
    stacked: true,
    left: [
      new Metric({
        label: 'Errors',
        namespace: 'ECSLogs',
        metricName: 'MyCustomErrorCountMetric',
        statistic: 'Sum',
        color: 'red'
      }),          
    ]
  })
);

Error Log

0/2 | 1:26:09 PM | UPDATE_IN_PROGRESS | AWS::CloudWatch::Dashboard | MyDashboard( MyDashboard4D881D82) 1/2 | 1:26:11 PM | UPDATE_FAILED | AWS::CloudWatch::Dashboard | MyDashboard(MyDashboard4D881D82) The dashboard body is invalid, there are 1 validation errors: [ { "dataPath": "/widgets/0/properties/metrics/0", "message": "Should NOT have more than 2 items" } ]

Environment

Other

Error message comes when deploying, and looks like this:

0/2 | 1:26:09 PM | UPDATE_IN_PROGRESS | AWS::CloudWatch::Dashboard | MyDashboard( MyDashboard4D881D82) 1/2 | 1:26:11 PM | UPDATE_FAILED | AWS::CloudWatch::Dashboard | MyDashboard(MyDashboard4D881D82) The dashboard body is invalid, there are 1 validation errors: [ { "dataPath": "/widgets/0/properties/metrics/0", "message": "Should NOT have more than 2 items" } ]

I have not yet tried to take a look at synthesized json etc, just wondering if this is repeatable issue/known issue already. I was unable to find any concrete examples of this in documentation, or with a web search.


This is :bug: Bug Report

ghost commented 4 years ago

It seems that this would be more a Cloudformation side issue, since generated json seems valid. I'm closing this one.

wsoula commented 4 years ago

For others that run into this I had the same issue and was able to get around it by moving my color attribute. Using python. Previously I had:

cloudwatch.MathExpression(expression="m1+m4",
    using_metrics={"m1":cloudwatch.Metric(metric_name=diskRead,namespace=customNamespace,dimensions=dimensions,statistic='Maximum'),"m4":cloudwatch.Metric(metric_name=diskWrite,namespace=customNamespace,dimensions=dimensions,statistic='Maximum')},
    label='TotalIOPS-Maximum',period=core.Duration.minutes(5),color='#1f77b4')

Just moving the color attribute to before the period made it work correctly:

cloudwatch.MathExpression(expression="m1+m4",
    using_metrics={"m1":cloudwatch.Metric(metric_name=diskRead,namespace=customNamespace,dimensions=dimensions,statistic='Maximum'),"m4":cloudwatch.Metric(metric_name=diskWrite,namespace=customNamespace,dimensions=dimensions,statistic='Maximum')},
    label='TotalIOPS-Maximum',color='#1f77b4',period=core.Duration.minutes(5))

I agree that this looks like a cloudformation bug and not a cdk bug, but wanted to let people know what I found to make it work for me.

simonherbert commented 1 year ago

Just encountered the same issue, the error message is misleading. It happened when setting an invalid color value, in my case .color("ff0000") instead of .color("#ff0000").

shearn89 commented 11 months ago

I also had this error - in my IDE I had accidentally made a whole metric block lowercase, which was causing issues (e.g. functionname rather than FunctionName, etc. etc.)