-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix(applicationautoscaling): add validation for cross-account metrics in target tracking policy #36503
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
base: main
Are you sure you want to change the base?
fix(applicationautoscaling): add validation for cross-account metrics in target tracking policy #36503
Conversation
… in target tracking policy. The `renderCustomMetric` function was silently ignoring the `account` property from custom metrics. This change adds validation to detect when a cross-account metric is used and throws a clear error, since Application Auto Scaling's CloudFormation schema does not currently support cross-account metrics (unlike CloudWatch Alarms which have `accountId` in MetricDataQuery). This provides immediate feedback during synthesis rather than having the metric silently default to the current account. Closes aws#36401
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pull request linter fails with the following errors:
❌ Fixes must contain a change to an integration test file and the resulting snapshot.
If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.
✅ A exemption request has been requested. Please wait for a maintainer's review.
|
Putting together this diagram helped me a lot tracking flowchart TB
subgraph UserCode["User Code"]
A["new cloudwatch.Metric({
namespace: 'Test',
metricName: 'Metric',
account: '222222222222'
})"]
end
subgraph ScalableTarget["ScalableTarget"]
B["scaleToTrackMetric(id, props)"]
end
subgraph TargetTrackingScalingPolicy["TargetTrackingScalingPolicy"]
C["constructor(scope, id, props)"]
D["renderCustomMetric(scope, props.customMetric)"]
end
subgraph MetricProcessing["Metric Processing"]
E["metric.toMetricConfig()"]
F["Validation: Compare c.account vs Stack.of(scope).account"]
end
subgraph ValidationOutcome["Validation Outcome"]
G["ValidationError:
Cross-account metrics are not supported"]
H["Return CustomizedMetricSpecificationProperty
{dimensions, metricName, namespace, statistic, unit}"]
end
subgraph CfnLayer["CloudFormation Layer"]
I["new CfnScalingPolicy()"]
J["CDK Synthesizer"]
end
subgraph CloudFormation["CloudFormation API"]
K["AWS::ApplicationAutoScaling::ScalingPolicy
CustomizedMetricSpecification:
- Dimensions
- MetricName
- Namespace
- Statistic
- Unit
(NO Account field)"]
end
A -->|"IMetric"| B
B -->|"BasicTargetTrackingScalingPolicyProps
{customMetric: IMetric, targetValue: number, ...}"| C
C -->|"IMetric"| D
D -->|"IMetric.toMetricConfig()"| E
E -->|"MetricConfig.metricStat: MetricStatConfig
{namespace, metricName, statistic, period,
dimensions?, unitFilter?, account?, region?}"| F
F -->|"account !== stackAccount"| G
F -->|"account === stackAccount OR account undefined"| H
H -->|"CfnScalingPolicy.CustomizedMetricSpecificationProperty
{dimensions?, metricName, namespace, statistic, unit?}"| I
I -->|"CfnScalingPolicy resource properties"| J
J -->|"CloudFormation JSON Template"| K
|
|
Exemption Request: This change will throw at synthesis time. Because of that, no template will be generated. That being said, I can add an integration test for the happy path, where no account id is provided and synthesis completes as expected. I need a bit of feedback here, not sure what to do |
Issue
Closes #36401.
Reason for this change
When trying to create a custom metric with an
accountproperty from a different AWS account for Application Auto Scaling target tracking policies, the account information is silently ignored. This leads to unexpected behavior where the policy uses metrics from the current account instead of the specified cross-account metric.Description of changes
Simply adding
account: metric.accountto the return object will not fix the problem. Here's why:1. CloudFormation schema does not support
Accountfor CustomizedMetricSpecificationThe AWS CloudFormation documentation for CustomizedMetricSpecification only supports these properties:
DimensionsMetricNameMetricsNamespaceStatisticUnitThere is no
AccountorAccountIdproperty listed.2. CloudFormation schema does not support
AccountIdfor TargetTrackingMetricDataQueryThe TargetTrackingMetricDataQuery (used by the
Metricsarray) only supports:ExpressionIdLabelMetricStatReturnDataAgain, no
AccountIdproperty - unlike CloudWatch Alarms' MetricDataQuery which does haveAccountId.3. CDK's generated CloudFormation converter strips unknown properties
Even if we add
accountto the TypeScript object, the CDK's generated conversion function will strip it out.In
packages/aws-cdk-lib/aws-applicationautoscaling/lib/applicationautoscaling.generated.ts(lines 2097-2108):There is no
Accountproperty in the return object. Anyaccountproperty we add gets silently dropped during CloudFormation synthesis.Solution
Rather than silently ignoring the
accountproperty (which was the original behavior), this change adds a validation that throws a clear error when a user attempts to use a cross-account metric. This provides immediate feedback during synthesis rather than having the deployment fail or behave unexpectedly.Code changes:
renderCustomMetric()atpackages/aws-cdk-lib/aws-applicationautoscaling/lib/target-tracking-scaling-policy.tsto detect when a metric's account differs from the stack's accountValidationErrorwith a clear message explaining that cross-account metrics are not supported for Application Auto Scaling target tracking policiesAlternatives considered:
Accountfield (docs)convertCfnScalingPolicyCustomizedMetricSpecificationPropertyToCloudFormation) strips unknown propertiesMetricsarray format: Investigated using the newerMetricsproperty format, butTargetTrackingMetricDataQueryalso doesn't supportAccountId(docs)Describe any new or updated permissions being added
None.
Description of how you validated changes
Added two unit tests:
throws error when using cross-account custom metric- Verifies that using a metric with a different account throws the expected errorallows custom metric from same account- Verifies that metrics from the same account continue to work correctlyAll existing tests continue to pass:
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license