-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsm-instances.yaml
318 lines (303 loc) · 8.77 KB
/
bsm-instances.yaml
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
AWSTemplateFormatVersion: "2010-09-09"
Description: "BSM Cloud Formation"
Parameters:
SourceCodeBucket:
Type: String
Default: repo.bsm.pub
AZ:
Type: String
Default: eu-central-1a
OperatorEMail:
Description: EMail address to notify if there are any scaling operations
Type: String
AllowedPattern: "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)"
OpenApiHttpPort:
Type: String
Default: '5000'
DictionaryHttpPort:
Type: String
Default: '5001'
OpenApiVersion:
Type: String
DictionaryVersion:
Type: String
Default: 0.0.1
Resources:
NotificationTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint:
Ref: OperatorEMail
Protocol: email
bsmDNS:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: bsm.pub.
Comment: Zone apex alias targeted to BSM LoadBalancer.
RecordSets:
- Name: bsm.pub.
Type: A
AliasTarget:
HostedZoneId: !GetAtt bsmELB.CanonicalHostedZoneNameID
DNSName: !GetAtt bsmELB.DNSName
- Name: dict.bsm.pub.
Type: A
TTL: '900'
ResourceRecords:
- !GetAtt DictionaryInstance.PrivateIp
bsmELB:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
AvailabilityZones:
Fn::GetAZs: ''
CrossZone: 'true'
Listeners:
- LoadBalancerPort: '80'
InstancePort:
Ref: OpenApiHttpPort
Protocol: HTTP
HealthCheck:
Target:
Fn::Join:
- ''
- - 'HTTP:'
- Ref: OpenApiHttpPort
- "/api/v2/api-docs"
HealthyThreshold: '3'
UnhealthyThreshold: '2'
Interval: '30'
Timeout: '5'
OpenApiASG:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AvailabilityZones:
Fn::GetAZs: ''
LaunchConfigurationName:
Ref: OpenApiLaunchConfig
MinSize: '1'
MaxSize: '3'
LoadBalancerNames:
- Ref: bsmELB
NotificationConfiguration:
TopicARN:
Ref: NotificationTopic
NotificationTypes:
- autoscaling:EC2_INSTANCE_LAUNCH
- autoscaling:EC2_INSTANCE_LAUNCH_ERROR
- autoscaling:EC2_INSTANCE_TERMINATE
- autoscaling:EC2_INSTANCE_TERMINATE_ERROR
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Count: '0'
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: '1'
MaxBatchSize: '1'
PauseTime: PT10M
WaitOnResourceSignals: 'true'
OpenApiScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName:
Ref: OpenApiASG
Cooldown: '60'
ScalingAdjustment: '1'
OpenApiScaleDownPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName:
Ref: OpenApiASG
Cooldown: '60'
ScalingAdjustment: "-1"
CPUAlarmHigh:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Scale-up if CPU > 50% for 1 minute
MetricName: CPUUtilization
Namespace: AWS/EC2
Statistic: Average
Period: '60'
EvaluationPeriods: '2'
Threshold: '50'
AlarmActions:
- Ref: OpenApiScaleUpPolicy
Dimensions:
- Name: AutoScalingGroupName
Value:
Ref: OpenApiASG
ComparisonOperator: GreaterThanThreshold
CPUAlarmLow:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Scale-down if CPU < 50% for 1 minute
MetricName: CPUUtilization
Namespace: AWS/EC2
Statistic: Average
Period: '60'
EvaluationPeriods: '2'
Threshold: '50'
AlarmActions:
- Ref: OpenApiScaleDownPolicy
Dimensions:
- Name: AutoScalingGroupName
Value:
Ref: OpenApiASG
ComparisonOperator: LessThanThreshold
OpenApiSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access and HTTP from the load balancer only
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
- IpProtocol: tcp
FromPort:
Ref: OpenApiHttpPort
ToPort:
Ref: OpenApiHttpPort
SourceSecurityGroupOwnerId:
Fn::GetAtt:
- bsmELB
- SourceSecurityGroup.OwnerAlias
SourceSecurityGroupName:
Fn::GetAtt:
- bsmELB
- SourceSecurityGroup.GroupName
DictionarySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access and HTTP from the Open Api ASG only
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
- IpProtocol: tcp
FromPort:
Ref: DictionaryHttpPort
ToPort:
Ref: DictionaryHttpPort
SourceSecurityGroupId:
Fn::GetAtt:
- OpenApiSecurityGroup
- GroupId
#Application source code repository access
RootRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
Policies:
- PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- s3:Get*
- s3:List*
Resource:
Fn::Join:
- ''
- - 'arn:aws:s3:::'
- Ref: SourceCodeBucket
- "/*"
RootInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- Ref: RootRole
OpenApiLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
KeyName: MasterKeyBSM
ImageId: ami-07f1fbbff759e24dd
SecurityGroups:
- Ref: OpenApiSecurityGroup
InstanceType: t2.micro
IamInstanceProfile:
Ref: RootInstanceProfile
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash\n"
- "set -x"
- "\n"
- "exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1"
- "\n"
- '/opt/aws/bin/cfn-init -v '
- ' --stack '
- Ref: AWS::StackName
- ' --resource OpenApiASG '
- ' --region '
- Ref: AWS::Region
- "\n"
- "aws s3 cp s3://repo.bsm.pub/release/com/bsm/application/"
- Ref: OpenApiVersion
- "/application-"
- Ref: OpenApiVersion
- ".jar /opt/open-api/open-api.jar"
- "\n"
- "aws s3 cp s3://repo.bsm.pub/cloud-formation-init/open-api/open-api-init.sh /home/ec2-user/open-api-init.sh"
- "\n"
- "chmod +x /home/ec2-user/open-api-init.sh"
- "\n"
- "bash /home/ec2-user/open-api-init.sh"
- "\n"
# Signal the status from cfn-init
- '/opt/aws/bin/cfn-signal -e $? '
- ' --stack '
- Ref: AWS::StackName
- ' --resource OpenApiASG '
- ' --region '
- Ref: AWS::Region
- "\n"
DictionaryInstance:
Type: AWS::EC2::Instance
Properties:
AvailabilityZone:
Ref: AZ
KeyName: MasterKeyBSM
ImageId: ami-07f1fbbff759e24dd
SecurityGroups:
- Ref: DictionarySecurityGroup
InstanceType: t2.micro
IamInstanceProfile:
Ref: RootInstanceProfile
UserData:
Fn::Base64:
Fn::Join:
- ''
- - "#!/bin/bash\n"
- "set -x"
- "\n"
- "exec > >(tee /var/log/user-data.log|logger -t user-data ) 2>&1"
- "\n"
- "aws s3 cp s3://repo.bsm.pub/release/com/bsm/dict/application/"
- Ref: DictionaryVersion
- "/application-"
- Ref: DictionaryVersion
- ".jar /opt/dictionary/dictionary.jar"
- "\n"
- "aws s3 cp s3://repo.bsm.pub/cloud-formation-init/dictionary/dictionary-init.sh /home/ec2-user/dictionary-init.sh"
- "\n"
- "chmod +x /home/ec2-user/dictionary-init.sh"
- "\n"
- "bash /home/ec2-user/dictionary-init.sh"
- "\n"