diff --git a/packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts b/packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts index c974a13d4ec8a..e0252b377ddc9 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts +++ b/packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts @@ -136,6 +136,11 @@ export class TargetTrackingScalingPolicy extends CoreConstruct { super(scope, id); + // replace dummy value in DYNAMODB_WRITE_CAPACITY_UTILIZATION due to a jsii bug (https://github.com/aws/jsii/issues/2782) + const predefinedMetric = props.predefinedMetric === PredefinedMetric.DYNAMODB_WRITE_CAPACITY_UTILIZATION ? + PredefinedMetric.DYANMODB_WRITE_CAPACITY_UTILIZATION : + props.predefinedMetric; + const resource = new CfnScalingPolicy(this, 'Resource', { policyName: props.policyName || cdk.Names.uniqueId(this), policyType: 'TargetTrackingScaling', @@ -143,8 +148,8 @@ export class TargetTrackingScalingPolicy extends CoreConstruct { targetTrackingScalingPolicyConfiguration: { customizedMetricSpecification: renderCustomMetric(props.customMetric), disableScaleIn: props.disableScaleIn, - predefinedMetricSpecification: props.predefinedMetric !== undefined ? { - predefinedMetricType: props.predefinedMetric, + predefinedMetricSpecification: predefinedMetric !== undefined ? { + predefinedMetricType: predefinedMetric, resourceLabel: props.resourceLabel, } : undefined, scaleInCooldown: props.scaleInCooldown && props.scaleInCooldown.toSeconds(), @@ -183,9 +188,20 @@ export enum PredefinedMetric { * @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html */ DYNAMODB_READ_CAPACITY_UTILIZATION = 'DynamoDBReadCapacityUtilization', + /** + * DYNAMODB_WRITE_CAPACITY_UTILIZATION + * + * Suffix `dummy` is necessary due to jsii bug (https://github.com/aws/jsii/issues/2782). + * Duplicate values will be dropped, so this suffix is added as a workaround. + * The value will be replaced when this enum is used. + * + * @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html + */ + DYNAMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization-dummy', /** * DYANMODB_WRITE_CAPACITY_UTILIZATION * @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html + * @deprecated use `PredefinedMetric.DYNAMODB_WRITE_CAPACITY_UTILIZATION` */ DYANMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization', /** diff --git a/packages/@aws-cdk/aws-applicationautoscaling/package.json b/packages/@aws-cdk/aws-applicationautoscaling/package.json index df80e8a73f458..36da42a721e0a 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/package.json +++ b/packages/@aws-cdk/aws-applicationautoscaling/package.json @@ -126,6 +126,7 @@ "docs-public-apis:@aws-cdk/aws-applicationautoscaling.StepScalingPolicyProps", "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.ECS_SERVICE_AVERAGE_CPU_UTILIZATION", "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.DYNAMODB_READ_CAPACITY_UTILIZATION", + "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.DYNAMODB_WRITE_CAPACITY_UTILIZATION", "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.DYANMODB_WRITE_CAPACITY_UTILIZATION", "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.ALB_REQUEST_COUNT_PER_TARGET", "docs-public-apis:@aws-cdk/aws-applicationautoscaling.PredefinedMetric.RDS_READER_AVERAGE_CPU_UTILIZATION", diff --git a/packages/@aws-cdk/aws-applicationautoscaling/test/target-tracking.test.ts b/packages/@aws-cdk/aws-applicationautoscaling/test/target-tracking.test.ts index 2ec9f8dd07a26..fccc2e2e55f17 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/test/target-tracking.test.ts +++ b/packages/@aws-cdk/aws-applicationautoscaling/test/target-tracking.test.ts @@ -53,6 +53,26 @@ describe('target tracking', () => { }); + test('test setup target tracking on predefined metric for DYNAMODB_WRITE_CAPACITY_UTILIZATION', () => { + // GIVEN + const stack = new cdk.Stack(); + const target = createScalableTarget(stack); + + // WHEN + target.scaleToTrackMetric('Tracking', { + predefinedMetric: appscaling.PredefinedMetric.DYNAMODB_WRITE_CAPACITY_UTILIZATION, + targetValue: 0.9, + }); + + // THEN + expect(stack).toHaveResourceLike('AWS::ApplicationAutoScaling::ScalingPolicy', { + TargetTrackingScalingPolicyConfiguration: { + PredefinedMetricSpecification: { PredefinedMetricType: 'DynamoDBWriteCapacityUtilization' }, + TargetValue: 0.9, + }, + }); + }); + test('test setup target tracking on custom metric', () => { // GIVEN const stack = new cdk.Stack(); diff --git a/packages/@aws-cdk/aws-dynamodb/lib/scalable-table-attribute.ts b/packages/@aws-cdk/aws-dynamodb/lib/scalable-table-attribute.ts index fc2a538e2545d..8c5f6467f4715 100644 --- a/packages/@aws-cdk/aws-dynamodb/lib/scalable-table-attribute.ts +++ b/packages/@aws-cdk/aws-dynamodb/lib/scalable-table-attribute.ts @@ -25,7 +25,7 @@ export class ScalableTableAttribute extends appscaling.BaseScalableAttribute { } this.scalingPolicyCreated = true; const predefinedMetric = this.props.dimension.indexOf('ReadCapacity') === -1 - ? appscaling.PredefinedMetric.DYANMODB_WRITE_CAPACITY_UTILIZATION + ? appscaling.PredefinedMetric.DYNAMODB_WRITE_CAPACITY_UTILIZATION : appscaling.PredefinedMetric.DYNAMODB_READ_CAPACITY_UTILIZATION; super.doScaleToTrackMetric('Tracking', {