forked from misohu/kubeflow-cloud-formation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_kubeflow_ha.yaml
363 lines (358 loc) · 11.2 KB
/
cf_kubeflow_ha.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
AWSTemplateFormatVersion: "2010-09-09"
Mappings:
RegionMap:
eu-central-1:
AMI: ami-xxxxxxxxxxxxxxxxx
us-east-1:
AMI: ami-0ea0f37840b2f2b19
us-gov-east-1:
AMI: ami-xxxxxxxxxxxxxxxxx
us-gov-west-1:
AMI: ami-xxxxxxxxxxxxxxxxx
us-east-2:
AMI: ami-xxxxxxxxxxxxxxxxx
us-west-1:
AMI: ami-xxxxxxxxxxxxxxxxx
us-west-2:
AMI: ami-xxxxxxxxxxxxxxxxx
ca-central-1:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-central-1:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-west-1:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-west-2:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-west-3:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-north-1:
AMI: ami-xxxxxxxxxxxxxxxxx
eu-south-1:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-east-1:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-southeast-1:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-southeast-2:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-south-1:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-northeast-1:
AMI: ami-xxxxxxxxxxxxxxxxx
ap-northeast-2:
AMI: ami-xxxxxxxxxxxxxxxxx
me-south-1:
AMI: ami-xxxxxxxxxxxxxxxxx
Description: ""
Parameters:
AppName:
Description: Name of the application the cluster will serve
Type: String
MinLength: "3"
AllowedPattern: '[a-zA-Z0-9\(\)\.\-/_]+'
Default: "MicroK8s"
KeyPair:
Description: Amazon EC2 Key Pair used to ssh to the cluster nodes
Type: "AWS::EC2::KeyPair::KeyName"
InstanceTypeParameter:
Type: String
Default: m4.large
AllowedValues:
- m4.large
- m4.xlarge
- m4.2xlarge
Description: Select the instance type of the cluster nodes
KubernetesVersion:
Type: String
Default: 1.23
AllowedValues:
- 1.23
- 1.22
- 1.21
Description: The Kubernetes version
SubNet:
Description: Amazon VPC Subnet to deploy the cluster on
Type: "AWS::EC2::Subnet::Id"
Resources:
SQSQueue:
Type: "AWS::SQS::Queue"
Properties:
DelaySeconds: "0"
MaximumMessageSize: "262144"
MessageRetentionPeriod: "345600"
ReceiveMessageWaitTimeSeconds: "0"
VisibilityTimeout: "30"
QueueName: !Sub "${AppName}"
SQSQueuePolicy:
Type: "AWS::SQS::QueuePolicy"
Properties:
PolicyDocument:
Version: "2008-10-17"
Id: SQS-Policy-1
Statement:
- Sid: !Sub "${AppName}-SQS-1"
Effect: Allow
Principal:
AWS: !Sub "${AWS::AccountId}"
Action:
- "sqs:*"
Resource: !Sub "${SQSQueue.QueueName}"
- Sid: !Sub "${AppName}-SQS-2"
Effect: Allow
Principal:
AWS: "*"
Action:
- SQS:SendMessage
Resource: !GetAtt
- SQSQueue
- Arn
Condition:
ArnLike:
aws:SourceArn: !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic.TopicName}"
Queues:
- !Sub "https://sqs.${AWS::Region}.amazonaws.com/${AWS::AccountId}/${SQSQueue.QueueName}"
DependsOn: "SQSQueue"
SNSTopic:
Type: "AWS::SNS::Topic"
Properties:
DisplayName: "Foo"
TopicName: !Sub "${AppName}"
SNSSubscription:
Type: "AWS::SNS::Subscription"
Properties:
TopicArn: !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic.TopicName}"
Endpoint: !GetAtt
- SQSQueue
- Arn
Protocol: "sqs"
RawMessageDelivery: "false"
Region: !Ref AWS::Region
DependsOn: "SNSTopic"
SNSTopicPolicy:
Type: "AWS::SNS::TopicPolicy"
Properties:
PolicyDocument:
Version: "2008-10-17"
Id: SNSSubscriptionPolicy
Statement:
- Sid: SNSSubscriptionPolicySid
Effect: Allow
Principal:
AWS: !Sub "${AWS::AccountId}"
Action:
- SNS:GetTopicAttributes
- SNS:SetTopicAttributes
- SNS:AddPermission
- SNS:RemovePermission
- SNS:DeleteTopic
- SNS:Subscribe
- SNS:ListSubscriptionsByTopic
- SNS:Publish
- SNS:Receive
Resource: !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic.TopicName}"
Condition:
StringEquals:
AWS:SourceOwner: !Sub "${AWS::AccountId}"
Topics:
- !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic.TopicName}"
DependsOn: [SNSTopic]
EC2LaunchTemplate:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateName: !Sub "${AppName}"
LaunchTemplateData:
UserData:
Fn::Base64: !Sub |
#!/bin/bash
snap refresh joule-expansion --classic
snap refresh microk8s --classic --channel=${KubernetesVersion}/stable
usermod -a -G microk8s ubuntu
mkdir /home/ubuntu/.kube
chown -f -R ubuntu /home/ubuntu/.kube
snap set joule-expansion provider=aws
snap set joule-expansion applications=microk8s
BlockDeviceMappings:
- DeviceName: "/dev/sda1"
Ebs:
Encrypted: false
DeleteOnTermination: true
VolumeSize: 100
VolumeType: "gp2"
IamInstanceProfile:
Arn: !GetAtt
- InstanceProfile
- Arn
EbsOptimized: false
KeyName: !Sub "${KeyPair}"
DisableApiTermination: false
ImageId: !FindInMap
- RegionMap
- !Ref AWS::Region
- AMI
InstanceType: !Sub "${InstanceTypeParameter}"
AutoScalingAutoScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
AutoScalingGroupName: !Sub "${AppName}"
LaunchTemplate:
LaunchTemplateId: !Ref EC2LaunchTemplate
Version: 1
MinSize: 1
MaxSize: 1
DesiredCapacity: 1
Cooldown: 300
VPCZoneIdentifier:
- !Ref SubNet
HealthCheckType: "EC2"
HealthCheckGracePeriod: 300
TerminationPolicies:
- "NewestInstance"
ServiceLinkedRoleARN: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling_${AppName}"
NewInstancesProtectedFromScaleIn: false
NotificationConfigurations:
- NotificationTypes:
- autoscaling:EC2_INSTANCE_LAUNCH
- autoscaling:EC2_INSTANCE_LAUNCH_ERROR
- autoscaling:EC2_INSTANCE_TERMINATE
- autoscaling:EC2_INSTANCE_TERMINATE_ERROR
TopicARN: !Sub "arn:aws:sns:${AWS::Region}:${AWS::AccountId}:${SNSTopic.TopicName}"
DependsOn:
- EC2LaunchTemplate
- SNSTopic
- AutoScalingRole
AutoScalingScalingPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
AutoScalingGroupName: !Sub "${AppName}"
PolicyType: "SimpleScaling"
AdjustmentType: "ChangeInCapacity"
ScalingAdjustment: 1
DependsOn: "AutoScalingAutoScalingGroup"
AutoScalingRole:
Type: "AWS::IAM::ServiceLinkedRole"
Properties:
AWSServiceName: autoscaling.amazonaws.com
Description: AutoScaling ServiceLinked Role
CustomSuffix: !Sub "${AppName}"
EC2Role:
Type: "AWS::IAM::Role"
Properties:
Description: !Sub "${AppName} Role"
RoleName: !Sub "${AppName}-EC2Role"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: autoscaling:DescribeAutoScalingGroups
Effect: Allow
Resource: "*"
- Action: autoscaling:DescribeAutoScalingInstances
Effect: Allow
Resource: "*"
- Action: autoscaling:SetInstanceProtection
Effect: Allow
Resource: !Sub "arn:*:autoscaling:*:*:autoScalingGroup:*:autoScalingGroupName/${AppName}"
- Action: ec2:CreateTags
Effect: Allow
Resource: "arn:*:ec2:*:*:instance/*"
- Action: ec2:DescribeTags
Effect: Allow
Resource: "*"
- Action: sqs:ListQueues
Effect: Allow
Resource: "*"
- Action: sqs:GetQueueUrl
Effect: Allow
Resource: "*"
- Action: sqs:ReceiveMessage
Effect: Allow
Resource: !Sub "arn:*:sqs:*:*:${AppName}"
- Action: sqs:DeleteMessage
Effect: Allow
Resource: !Sub "arn:*:sqs:*:*:${AppName}"
- Action: sqs:SendMessage
Effect: Allow
Resource: !Sub "arn:*:sqs:*:*:${AppName}"
LambdaRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service:
- events.amazonaws.com
- lambda.amazonaws.com
- ec2.amazonaws.com
Version: "2012-10-17"
Policies:
- PolicyName: root
PolicyDocument:
Version: "2012-10-17"
Statement:
- Action: autoscaling:DescribeAutoScalingGroups
Effect: Allow
Resource: "*"
- Action: autoscaling:DescribeAutoScalingInstances
Effect: Allow
Resource: "*"
- Action: ec2:CreateTags
Effect: Allow
Resource: "arn:*:ec2:*:*:instance/*"
Metadata:
aws:cdk:path: aws-instance-scheduler/SchedulerRole/Resource
InstanceProfile:
Type: AWS::IAM::InstanceProfile
DependsOn: "EC2Role"
Properties:
InstanceProfileName: !Sub "${AppName}-InstanceProfile"
Roles:
- !Ref EC2Role
LeaderNodeTagger:
Type: AWS::Lambda::Function
DependsOn:
- "AutoScalingAutoScalingGroup"
- "LambdaRole"
Properties:
Handler: "index.lambda_handler"
Role: !GetAtt LambdaRole.Arn
Runtime: "python3.8"
Code:
ZipFile: |
import boto3
import cfnresponse
import json
def get_first_instance():
client = boto3.client('autoscaling')
return client.describe_auto_scaling_groups() \
["AutoScalingGroups"][0]["Instances"][0]
def lambda_handler(event, context):
first_instance = get_first_instance()
response = boto3.client("ec2").create_tags(
Resources=[first_instance["InstanceId"]],
Tags=[
{"Key": "joule:enrolled", "Value": "1"}
],
)
cfnresponse.send(event, context, cfnresponse.SUCCESS, {}, "CustomResourcePhysicalID")
return {
'statusCode': 200,
'body': first_instance["InstanceId"].strip()
}
LeaderNodeTaggerTrigger:
Type: AWS::CloudFormation::CustomResource
DependsOn: LeaderNodeTagger
Version: "1.0"
Properties:
ServiceToken: !GetAtt LeaderNodeTagger.Arn