Skip to content

Commit ac4d09d

Browse files
authored
feat(cloudwatch): add account id field for log query and metric widgets to support cross account visibility (#34793)
### Issue # (if applicable) Closes #26105 ### Reason for this change CloudWatch supports cross-account visibility for dashboards, allowing users to view metrics and logs from multiple AWS accounts in a single dashboard. However, the CDK CloudWatch construct library was missing the ability to specify the account ID for widgets, preventing users from creating cross-account dashboards using CDK. ### Description of changes I've added an optional accountId property to the following interfaces and classes: • MetricWidgetProps interface in graph.ts (base interface for metric widgets) • LogQueryWidgetProps interface in log-query.ts This property is then passed through to the CloudWatch dashboard JSON for each widget type: • AlarmWidget • GraphWidget • SingleValueWidget • GaugeWidget • TableWidget • LogQueryWidget I've also: • Added comprehensive JSDoc documentation explaining the cross-account functionality • Updated the README.md with a new section on cross-account visibility • Updated unit tests to verify the accountId is properly included in the widget JSON • Updated integration tests to demonstrate the feature Design decisions: • Made accountId optional to maintain backward compatibility • Added the property to the base interfaces to ensure consistent implementation across widget types • Provided clear documentation on prerequisites for cross-account functionality ### Describe any new or updated permissions being added No new IAM permissions are added by this change in the CDK code itself. However, users implementing cross-account CloudWatch dashboards will need to configure appropriate permissions between their accounts: 1. The monitoring account must be set up as a monitoring account in CloudWatch settings 2. The source account must grant permissions to the monitoring account using CloudWatch resource policies 3. Appropriate IAM roles and policies must be configured as described in the AWS documentation ### Description of how you validated changes • Added unit tests for all widget types to verify the accountId property is correctly passed to the CloudWatch dashboard JSON • Updated integration tests to include accountId in various widget configurations ### Checklist • [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)
1 parent 8055016 commit ac4d09d

20 files changed

+322
-292
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/aws-cdk-cloudwatch-alarms.assets.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/aws-cdk-cloudwatch-alarms.template.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"Type": "AWS::CloudWatch::Alarm",
88
"Properties": {
99
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
10-
"EvaluationPeriods": 3,
1110
"DatapointsToAlarm": 2,
1211
"Dimensions": [
1312
{
@@ -20,6 +19,7 @@
2019
}
2120
}
2221
],
22+
"EvaluationPeriods": 3,
2323
"MetricName": "ApproximateNumberOfMessagesVisible",
2424
"Namespace": "AWS/SQS",
2525
"Period": 300,
@@ -94,7 +94,7 @@
9494
{
9595
"Ref": "AWS::Region"
9696
},
97-
"\",\"query\":\"SOURCE 'my-log-group' | fields @message\\n | filter @message like /Error/\"}},{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":50,\"properties\":{\"view\":\"singleValue\",\"title\":\"Sent message size\",\"region\":\"",
97+
"\",\"accountId\":\"123456789012\",\"query\":\"SOURCE 'my-log-group' | fields @message\\n | filter @message like /Error/\"}},{\"type\":\"metric\",\"width\":6,\"height\":3,\"x\":0,\"y\":50,\"properties\":{\"view\":\"singleValue\",\"title\":\"Sent message size\",\"region\":\"",
9898
{
9999
"Ref": "AWS::Region"
100100
},

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/cdkcloudwatchalarmsintegtestDefaultTestDeployAssertD2D0B407.assets.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/integ.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-cloudwatch/test/integ.alarm-and-dashboard.js.snapshot/manifest.json

Lines changed: 212 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)