-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathscheduled-action.ts
142 lines (126 loc) · 3.85 KB
/
scheduled-action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { Construct } from 'constructs';
import { IAutoScalingGroup } from './auto-scaling-group';
import { CfnScheduledAction } from './autoscaling.generated';
import { Schedule } from './schedule';
import { Resource } from '../../core';
/**
* Properties for a scheduled scaling action
*/
export interface BasicScheduledActionProps {
/**
* Specifies the time zone for a cron expression. If a time zone is not provided, UTC is used by default.
*
* Valid values are the canonical names of the IANA time zones, derived from the IANA Time Zone Database (such as Etc/GMT+9 or Pacific/Tahiti).
*
* For more information, see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
*
* @default - UTC
*
*/
readonly timeZone?: string;
/**
* When to perform this action.
*
* Supports cron expressions.
*
* For more information about cron expressions, see https://en.wikipedia.org/wiki/Cron.
*/
readonly schedule: Schedule;
/**
* When this scheduled action becomes active.
*
* @default - The rule is activate immediately.
*/
readonly startTime?: Date;
/**
* When this scheduled action expires.
*
* @default - The rule never expires.
*/
readonly endTime?: Date;
/**
* The new minimum capacity.
*
* At the scheduled time, set the minimum capacity to the given capacity.
*
* At least one of maxCapacity, minCapacity, or desiredCapacity must be supplied.
*
* @default - No new minimum capacity.
*/
readonly minCapacity?: number;
/**
* The new maximum capacity.
*
* At the scheduled time, set the maximum capacity to the given capacity.
*
* At least one of maxCapacity, minCapacity, or desiredCapacity must be supplied.
*
* @default - No new maximum capacity.
*/
readonly maxCapacity?: number;
/**
* The new desired capacity.
*
* At the scheduled time, set the desired capacity to the given capacity.
*
* At least one of maxCapacity, minCapacity, or desiredCapacity must be supplied.
*
* @default - No new desired capacity.
*/
readonly desiredCapacity?: number;
}
/**
* Properties for a scheduled action on an AutoScalingGroup
*/
export interface ScheduledActionProps extends BasicScheduledActionProps {
/**
* The AutoScalingGroup to apply the scheduled actions to
*/
readonly autoScalingGroup: IAutoScalingGroup;
}
/**
* Define a scheduled scaling action
*/
export class ScheduledAction extends Resource {
/**
* The name of the scheduled action.
*
* @attribute
*/
public readonly scheduledActionName: string;
constructor(scope: Construct, id: string, props: ScheduledActionProps) {
super(scope, id);
if (props.minCapacity === undefined && props.maxCapacity === undefined && props.desiredCapacity === undefined) {
throw new Error('At least one of minCapacity, maxCapacity, or desiredCapacity is required');
}
// add a warning on synth when minute is not defined in a cron schedule
props.schedule._bind(this);
const resource = new CfnScheduledAction(this, 'Resource', {
autoScalingGroupName: props.autoScalingGroup.autoScalingGroupName,
startTime: formatISO(props.startTime),
endTime: formatISO(props.endTime),
minSize: props.minCapacity,
maxSize: props.maxCapacity,
desiredCapacity: props.desiredCapacity,
recurrence: props.schedule.expressionString,
timeZone: props.timeZone,
});
this.scheduledActionName = resource.attrScheduledActionName;
}
}
function formatISO(date?: Date) {
if (!date) { return undefined; }
return date.getUTCFullYear() +
'-' + pad(date.getUTCMonth() + 1) +
'-' + pad(date.getUTCDate()) +
'T' + pad(date.getUTCHours()) +
':' + pad(date.getUTCMinutes()) +
':' + pad(date.getUTCSeconds()) +
'Z';
function pad(num: number) {
if (num < 10) {
return '0' + num;
}
return num;
}
}