Skip to content

fix(cloudwatch): remove false positive warning for CDK tokens in MathExpression#36882

Open
kaizencc wants to merge 2 commits intomainfrom
fix-34977
Open

fix(cloudwatch): remove false positive warning for CDK tokens in MathExpression#36882
kaizencc wants to merge 2 commits intomainfrom
fix-34977

Conversation

@kaizencc
Copy link
Contributor

@kaizencc kaizencc commented Feb 4, 2026

Issue # (if applicable)

Closes #34977.

Reason for this change

When using CDK tokens (from custom resources or other dynamic values) in CloudWatch MathExpression, users receive a false positive warning message. The allIdentifiersInExpression function incorrectly parses token strings like ${Token[TOKEN.81]} and extracts "oken" as an identifier, triggering a confusing warning that suggests adding "oken" to the usingMetrics map.

This degrades user experience and creates confusion, particularly for users leveraging custom resources or dynamic values in CloudWatch dashboards.

Description of changes

Modified the allIdentifiersInExpression function in packages/aws-cdk-lib/aws-cloudwatch/lib/metric.ts to filter out CDK token patterns before extracting metric identifiers:

  • Added regex replacement to remove CDK token patterns (${Token[TOKEN.N]}) from expressions before identifier extraction
  • Token pattern regex: /\$\{Token\[[^\]]+\]\}/g matches the standard CDK token format
  • Preserves existing validation behavior for actual unknown identifiers
  • Added 4 comprehensive unit tests covering single tokens, multiple tokens, mixed expressions, and complex patterns

Technical Details:

// Before: Incorrectly extracted "oken" from "${Token[TOKEN.81]}"
function allIdentifiersInExpression(x: string) {
  return Array.from(matchAll(x, FIND_VARIABLE)).map(m => m[0]);
}

// After: Filters out tokens first, then extracts identifiers
function allIdentifiersInExpression(x: string) {
  // Remove CDK token patterns before extracting identifiers
  // Token format: ${Token[TOKEN.123]} or ${Token[TOKEN.456]}
  const withoutTokens = x.replace(/\$\{Token\[[^\]]+\]\}/g, '');
  return Array.from(matchAll(withoutTokens, FIND_VARIABLE)).map(m => m[0]);
}

This approach follows the same pattern used for special functions (SEARCH, METRICS, SELECT, INSIGHT_RULE_METRIC) where certain syntax patterns are filtered or handled specially before validation.

No breaking changes - This fix only removes false positive warnings and does not affect CloudFormation template generation, resource behavior, or public APIs.

Describe any new or updated permissions being added

N/A - No IAM permissions or resource access changes. This is a validation-only fix that does not affect CloudFormation templates or runtime behavior.

Description of how you validated changes

  • Unit tests: Added 4 new unit tests in packages/aws-cdk-lib/aws-cloudwatch/test/metric-math.test.ts:

    1. Verified single token in expression doesn't produce warning
    2. Verified multiple tokens in expression don't produce warning
    3. Verified mixed token + real identifier warns only about the real identifier (not "oken")
    4. Verified complex token patterns (arrays, functions) don't produce warning
  • Regression testing: All 40 existing unit tests pass without modification, confirming:

    • Warnings for actual unknown identifiers still work correctly
    • Special function handling (METRICS, SEARCH, etc.) unchanged
    • Nested expression validation preserved
    • No impact on other CloudWatch functionality
  • Build validation:

    • Linting passed with no violations (yarn lint --fix)
    • TypeScript compilation successful
    • No JSII compatibility issues

Checklist

@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Feb 4, 2026
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Feb 4, 2026
@aws-cdk-automation aws-cdk-automation requested a review from a team February 4, 2026 20:33
return Array.from(matchAll(x, FIND_VARIABLE)).map(m => m[0]);
// Remove CDK token patterns before extracting identifiers
// Token format: ${Token[TOKEN.123]} or ${Token[TOKEN.456]}
const withoutTokens = x.replace(/\$\{Token\[[^\]]+\]\}/g, '');

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings starting with '${Token[' and with many repetitions of '${Token['.
This
regular expression
that depends on
library input
may run slow on strings starting with '${Token[' and with many repetitions of '${Token['.
This
regular expression
that depends on
library input
may run slow on strings starting with '${Token[' and with many repetitions of '${Token['.
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws-cloudwatch: CDK synth "Warning 'TIME_SERIES(${Token[TOKEN.81]})' references unknown identifiers: oken"

2 participants