-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest-ecs-cluster.yaml
304 lines (269 loc) · 7.85 KB
/
test-ecs-cluster.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
Description: >
Create ECS Cluster resources for Deployment on ECS.
Parameters:
InstanceType:
Type: String
Default: t2.medium
ClusterSize:
Type: Number
Default: 1
Subnet1:
Type: AWS::EC2::Subnet::Id
Subnet2:
Type: AWS::EC2::Subnet::Id
VpcId:
Type: AWS::EC2::VPC::Id
VpcCIDR:
Type: String
Code:
Type: String
Default: latest
Repository:
Type: String
TemplateBucket:
Type: String
Description: >
S3 Bucket used for nested templates
HostedZoneName:
Type: String
Hostname:
Type: String
certificateArn:
Type: String
clientId:
Type: String
clientSecret:
Type: String
Stack:
Type: String
lexBotName:
Type: String
DBPassword:
Default: password
NoEcho: true
Description: testdb admin account password
Type: String
MinLength: 6
MaxLength: 41
AllowedPattern: "[a-zA-Z0-9]*"
ConstraintDescription: must contain only alphanumeric characters.
Mappings:
AWSRegionToAMI:
us-east-1:
AMI: ami-6df8fe7a
us-east-2:
AMI: ami-c6b5efa3
us-west-1:
AMI: ami-1eda8d7e
us-west-2:
AMI: ami-a2ca61c2
eu-west-1:
AMI: ami-ba346ec9
eu-west-2:
AMI: ami-42c5cf26
eu-central-1:
AMI: ami-e012d48f
ap-northeast-1:
AMI: ami-08f7956f
ap-southeast-1:
AMI: ami-f4832f97
ap-southeast-2:
AMI: ami-774b7314
ca-central-1:
AMI: ami-be45f7da
Resources:
ECSRole:
Type: AWS::IAM::Role
Properties:
Path: /
#RoleName: !Sub ecs-${AWS::StackName}
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ec2.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
Policies:
- PolicyName: ecsCluster
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- lambda:invokeFunction
- ecs:CreateCluster
- ecs:DeregisterContainerInstance
- ecs:DiscoverPollEndpoint
- ecs:Poll
- ecs:RegisterContainerInstance
- ecs:StartTelemetrySession
- ecs:UpdateContainerInstancesState
- ecs:Submit*
- ecr:GetAuthorizationToken
- ecr:BatchCheckLayerAvailability
- ecr:GetDownloadUrlForLayer
- ecr:BatchGetImage
- logs:CreateLogStream
- logs:PutLogEvents
- lex:PostContent
- lex:PostText
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ECSRole
SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: !Sub ${AWS::StackName}-hosts
SecurityGroupIngress:
- SourceSecurityGroupId: !GetAtt LoadBalancer.Outputs.SecurityGroup
IpProtocol: -1
VpcId: !Ref VpcId
Cluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: !Ref AWS::StackName
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref Subnet1
- !Ref Subnet2
LaunchConfigurationName: !Ref LaunchConfiguration
MinSize: 1
MaxSize: 4
DesiredCapacity: !Ref ClusterSize
Tags:
- Key: Name
Value: !Sub ${AWS::StackName} - ECS Host
PropagateAtLaunch: true
CreationPolicy:
ResourceSignal:
Timeout: PT15M
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: 1
MaxBatchSize: 1
PauseTime: PT15M
WaitOnResourceSignals: true
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
config:
commands:
01_add_instance_to_cluster:
command: !Sub echo ECS_CLUSTER=${Cluster} > /etc/ecs/ecs.config
files:
"/etc/cfn/cfn-hup.conf":
mode: 000400
owner: root
group: root
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
"/etc/cfn/hooks.d/cfn-auto-reloader.conf":
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.ContainerInstances.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Properties:
ImageId: !FindInMap [ AWSRegionToAMI, !Ref "AWS::Region", AMI ]
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref InstanceProfile
SecurityGroups:
- !Ref SecurityGroup
UserData:
"Fn::Base64": !Sub |
#!/bin/bash
yum install -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --region ${AWS::Region} --stack ${AWS::StackName} --resource LaunchConfiguration
/opt/aws/bin/cfn-signal -e $? --region ${AWS::Region} --stack ${AWS::StackName} --resource AutoScalingGroup
LoadBalancer:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/${TemplateBucket}/test-load-balancer.yaml
Parameters:
Subnet1 : !Ref Subnet1
Subnet2 : !Ref Subnet2
VpcId: !Ref VpcId
VpcCIDR: !Ref VpcCIDR
Name: !Ref Hostname
CloudwatchLogsGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub ${Hostname}-${Stack}-${AWS::StackName}
RetentionInDays: 14
Service:
Type: AWS::CloudFormation::Stack
DependsOn:
- LoadBalancer
- Cluster
- CloudwatchLogsGroup
- AutoScalingGroup
- DBInstance
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/${TemplateBucket}/test-service.yaml
Parameters:
Cluster: !Ref Cluster
LoadBalancer: !GetAtt LoadBalancer.Outputs.LoadBalancerArn
Port: 443
Tag: !Ref Code
DesiredCount: "1"
VpcId: !Ref VpcId
Repository: !Ref Repository
Identifier: "Code"
IsProduction: True
DBAddress: !GetAtt DBInstance.Outputs.DBAddress
DBPassword: !Ref DBPassword
HostedZoneName: !Ref HostedZoneName
CloudwatchLogsGroup: !Ref CloudwatchLogsGroup
Hostname: !Ref Hostname
certificateArn: !Ref certificateArn
clientId: !Ref clientId
clientSecret: !Ref clientSecret
lexBotName: !Ref lexBotName
BotkitAlias:
Type: AWS::Route53::RecordSetGroup
Properties:
HostedZoneName: !Sub ${HostedZoneName}.
Comment: Alias to botkit elb
RecordSets:
- Name : !Sub ${Hostname}.${HostedZoneName}.
Type : A
AliasTarget:
HostedZoneId : !GetAtt LoadBalancer.Outputs.CanonicalHostedZoneID
DNSName : !GetAtt LoadBalancer.Outputs.DNSName
DBInstance:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: !Sub https://s3.amazonaws.com/${TemplateBucket}/test-db.yaml
Parameters:
Subnet1 : !Ref Subnet1
Subnet2 : !Ref Subnet2
VpcId: !Ref VpcId
VpcCIDR: !Ref VpcCIDR
DBPassword: !Ref DBPassword
SecurityGroup: !Ref SecurityGroup
Outputs:
ClusterName:
Value: !Ref Cluster
OAuthEndpoint:
Value: !Sub https://${Hostname}.${HostedZoneName}/oauth
LoginEndpoint:
Value: !Sub https://${Hostname}.${HostedZoneName}/login