-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_create_appstream_fleet_autoscale.py
executable file
·166 lines (136 loc) · 5.14 KB
/
run_create_appstream_fleet_autoscale.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python3.12
import json
import time
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
options, args = dict(), list()
if __name__ == "__main__":
from run_common import parse_args
options, args = parse_args()
def create_appstream_fleet_autoscale(settings):
aws_cli = AWSCli(settings['AWS_REGION'])
fleet_name = settings['FLEET_NAME']
print_message(f'create fleet autoscale for: {fleet_name}')
fleet_path = f"fleet/{settings['FLEET_NAME']}"
max_capacity = settings['MAX_CAPACITY']
min_capacity = settings['MIN_CAPACITY']
cc = ['application-autoscaling', 'describe-scalable-targets']
cc += ['--service-namespace', 'appstream']
cc += ['--resource-ids', fleet_path]
rr = aws_cli.run(cc)
if rr['ScalableTargets']:
cc = ['application-autoscaling', 'deregister-scalable-target']
cc += ['--service-namespace', 'appstream']
cc += ['--resource-id', fleet_path]
cc += ['--scalable-dimension', 'appstream:fleet:DesiredCapacity']
aws_cli.run(cc)
time.sleep(30)
# register target auto-scale
cc = ['application-autoscaling', 'register-scalable-target']
cc += ['--service-namespace', 'appstream']
cc += ['--scalable-dimension', 'appstream:fleet:DesiredCapacity']
cc += ['--resource-id', fleet_path]
cc += ['--min-capacity', min_capacity]
cc += ['--max-capacity', max_capacity]
aws_cli.run(cc)
# Scale out
appstream_policy = {
'PolicyName': 'scale-out-utilization-policy',
'ServiceNamespace': 'appstream',
'ResourceId': fleet_path,
'ScalableDimension': 'appstream:fleet:DesiredCapacity',
'PolicyType': 'StepScaling',
'StepScalingPolicyConfiguration': {
'AdjustmentType': 'PercentChangeInCapacity',
'StepAdjustments': [
{
'MetricIntervalLowerBound': 0,
'ScalingAdjustment': 25
}
],
'Cooldown': 60
}
}
cc = ['application-autoscaling', 'put-scaling-policy']
cc += ['--cli-input-json', json.dumps(appstream_policy)]
rr = aws_cli.run(cc)
policy_arn = rr['PolicyARN']
cc = ['cloudwatch', 'put-metric-alarm']
cc += ['--alarm-name', 'scale-out-utilization-policy']
cc += ['--alarm-description', '"Alarm when Capacity Utilization exceeds 75 percent"']
cc += ['--metric-name', 'CapacityUtilization']
cc += ['--namespace', 'AWS/AppStream']
cc += ['--statistic', 'Average']
cc += ['--period', '120']
cc += ['--threshold', '75']
cc += ['--comparison-operator', 'GreaterThanThreshold']
cc += ['--dimensions', 'Name=Fleet,Value=naoko-fleet']
cc += ['--evaluation-periods', '1']
cc += ['--unit', 'Percent']
cc += ['--alarm-actions', policy_arn]
aws_cli.run(cc)
# Scale in
appstream_policy = {
'PolicyName': 'scale-in-utilization-policy',
'ServiceNamespace': 'appstream',
'ResourceId': fleet_path,
'ScalableDimension': 'appstream:fleet:DesiredCapacity',
'PolicyType': 'StepScaling',
'StepScalingPolicyConfiguration': {
'AdjustmentType': 'PercentChangeInCapacity',
'StepAdjustments': [
{
'MetricIntervalUpperBound': 0,
'ScalingAdjustment': -25
}
],
'Cooldown': 360
}
}
cc = ['application-autoscaling', 'put-scaling-policy']
cc += ['--cli-input-json', json.dumps(appstream_policy)]
rr = aws_cli.run(cc)
policy_arn = rr['PolicyARN']
cc = ['cloudwatch', 'put-metric-alarm']
cc += ['--alarm-name', 'scale-in-utilization-policy']
cc += ['--alarm-description', '"Alarm when Capacity Utilization is less than or equal to 25 percent"']
cc += ['--metric-name', 'CapacityUtilization']
cc += ['--namespace', 'AWS/AppStream']
cc += ['--statistic', 'Average']
cc += ['--period', '120']
cc += ['--threshold', '25']
cc += ['--comparison-operator', 'LessThanOrEqualToThreshold']
cc += ['--dimensions', f'Name=Fleet,Value={fleet_name}']
cc += ['--evaluation-periods', '6']
cc += ['--unit', 'Percent']
cc += ['--alarm-actions', policy_arn]
aws_cli.run(cc)
################################################################################
#
# start
#
################################################################################
print_session('create appstream autoscaling setting for stack & fleet')
appstream = env['appstream']
target_name = None
region = options.get('region')
is_target_exists = False
if len(args) > 1:
target_name = args[1]
for settings in appstream.get('STACK', list()):
if target_name and settings['NAME'] != target_name:
continue
if region and settings['AWS_REGION'] != region:
continue
is_target_exists = True
create_appstream_fleet_autoscale(settings)
if is_target_exists is False:
mm = list()
if target_name:
mm.append(target_name)
if region:
mm.append(region)
mm = ' in '.join(mm)
print(f'appstream autoscale: {mm} is not found in config.json')