Skip to content
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

feat(applicationautoscaling): validate evaluationPeriods and datapointsToAlarm for step scaling policy #28880

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export interface BasicStepScalingPolicyProps {
*
* Only has meaning if `evaluationPeriods != 1`.
*
* @default `evaluationPeriods`
* @default - Same as `evaluationPeriods`
*/
readonly datapointsToAlarm?: number;

Expand Down Expand Up @@ -117,8 +117,22 @@ export class StepScalingPolicy extends Construct {
throw new Error(`'scalingSteps' can have at most 40 steps, got ${props.scalingSteps.length}`);
}

if (props.datapointsToAlarm !== undefined && props.datapointsToAlarm < 1) {
throw new RangeError(`datapointsToAlarm cannot be less than 1, got: ${props.datapointsToAlarm}`);
if (props.evaluationPeriods !== undefined && !cdk.Token.isUnresolved(props.evaluationPeriods) && props.evaluationPeriods < 1) {
throw new Error(`evaluationPeriods cannot be less than 1, got: ${props.evaluationPeriods}`);
}
if (props.datapointsToAlarm !== undefined) {
if (props.evaluationPeriods === undefined) {
throw new Error('evaluationPeriods must be set if datapointsToAlarm is set');
}
if (!cdk.Token.isUnresolved(props.datapointsToAlarm) && props.datapointsToAlarm < 1) {
throw new Error(`datapointsToAlarm cannot be less than 1, got: ${props.datapointsToAlarm}`);
}
if (!cdk.Token.isUnresolved(props.datapointsToAlarm)
&& !cdk.Token.isUnresolved(props.evaluationPeriods)
&& props.evaluationPeriods < props.datapointsToAlarm
) {
throw new Error(`datapointsToAlarm must be less than or equal to evaluationPeriods, got datapointsToAlarm: ${props.datapointsToAlarm}, evaluationPeriods: ${props.evaluationPeriods}`);
}
}

const adjustmentType = props.adjustmentType || AdjustmentType.CHANGE_IN_CAPACITY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@ describe('step scaling policy', () => {

});

test('step scaling with invalid evaluation period throws error', () => {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// THEN
expect(() => {
target.scaleOnMetric('Tracking', {
metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric', statistic: 'p99' }),
scalingSteps: [
{ upper: 0, change: -1 },
{ lower: 100, change: +1 },
{ lower: 500, change: +5 },
],
evaluationPeriods: 0,
metricAggregationType: appscaling.MetricAggregationType.MAXIMUM,
});
}).toThrow(/evaluationPeriods cannot be less than 1, got: 0/);
});

test('step scaling with evaluation period & data points to alarm configured', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down Expand Up @@ -274,6 +294,47 @@ describe('step scaling policy', () => {
}).toThrow('datapointsToAlarm cannot be less than 1, got: 0');
});

test('step scaling with datapointsToAlarm is greater than evaluationPeriods throws error', () => {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// THEN
expect(() => {
target.scaleOnMetric('Tracking', {
metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric', statistic: 'p99' }),
scalingSteps: [
{ upper: 0, change: -1 },
{ lower: 100, change: +1 },
{ lower: 500, change: +5 },
],
evaluationPeriods: 10,
datapointsToAlarm: 15,
metricAggregationType: appscaling.MetricAggregationType.MAXIMUM,
});
}).toThrow(/datapointsToAlarm must be less than or equal to evaluationPeriods, got datapointsToAlarm: 15, evaluationPeriods: 10/);
});

test('step scaling with datapointsToAlarm without evaluationPeriods throws error', () => {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// THEN
expect(() => {
target.scaleOnMetric('Tracking', {
metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric', statistic: 'p99' }),
scalingSteps: [
{ upper: 0, change: -1 },
{ lower: 100, change: +1 },
{ lower: 500, change: +5 },
],
datapointsToAlarm: 15,
metricAggregationType: appscaling.MetricAggregationType.MAXIMUM,
});
}).toThrow(/evaluationPeriods must be set if datapointsToAlarm is set/);
});

test('scalingSteps must have at least 2 steps', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
Loading