-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcf-all.yaml
341 lines (330 loc) · 11.1 KB
/
cf-all.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
AWSTemplateFormatVersion: 2010-09-09
Description: 'CloudFormation Application stack'
Parameters:
InstanceTypeParameter:
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- m5.large
Description: Enter t2.micro or m5.large. Default is t2.micro.
KeyName:
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
DBName:
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
ConstraintDescription: must begin with a letter and contain only alphanumeric
characters.
Default: isen
Description: The Isen database name
MaxLength: '64'
MinLength: '1'
Type: String
DBPassword:
AllowedPattern: '[a-zA-Z0-9]*'
ConstraintDescription: must contain only alphanumeric characters.
Description: The Isen database admin account password
MaxLength: '41'
MinLength: '8'
NoEcho: 'true'
Type: String
DBRootPassword:
AllowedPattern: '[a-zA-Z0-9]*'
ConstraintDescription: must contain only alphanumeric characters.
Description: MySQL root password
MaxLength: '41'
MinLength: '8'
NoEcho: 'true'
Type: String
DBUser:
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
ConstraintDescription: must begin with a letter and contain only alphanumeric
characters.
Description: The ISEN database admin account username
MaxLength: '16'
MinLength: '1'
NoEcho: 'true'
Type: String
Subnets:
Type: List<AWS::EC2::Subnet::Id>
Description: The Subnets in which the the LoadBalancer will deployed in (select at least two)
UpdateVariable:
Type: String
Description: Enter a random string. This will ensure that the application instance will always be updated.
Mappings:
RegionMap:
us-east-1:
"HVM64": "ami-00b882ac5193044e4"
eu-west-1:
"HVM64": "ami-0f62aafc6efe8fd7b"
Resources:
LoadBalancer:
Type: 'AWS::ElasticLoadBalancingV2::LoadBalancer'
Properties:
Scheme: "internet-facing"
Subnets: !Ref Subnets
SecurityGroups:
- !GetAtt LoadBalancerSecurityGroup.GroupId
HttpListener:
Type: 'AWS::ElasticLoadBalancingV2::Listener'
Properties:
DefaultActions:
- TargetGroupArn: !Ref DefaultTargetGroup
Type: forward
LoadBalancerArn: !Ref LoadBalancer
Port: 80
Protocol: HTTP
DefaultTargetGroup:
Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'
Properties:
HealthCheckIntervalSeconds: 15
HealthCheckPath: '/'
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 2
UnhealthyThresholdCount: 2
VpcId: !GetAtt LoadBalancerSecurityGroup.VpcId
Matcher:
HttpCode: '200-299'
Port: 8080
Protocol: HTTP
ApplicationASG:
Type: AWS::AutoScaling::AutoScalingGroup
UpdatePolicy:
AutoScalingRollingUpdate:
MinInstancesInService: 1
PauseTime: PT5M
WaitOnResourceSignals: true
Properties:
AvailabilityZones:
Fn::GetAZs: ""
LaunchConfigurationName: !Ref ApplicationLC
MinSize: "1"
MaxSize: "4"
TargetGroupARNs:
- !Ref DefaultTargetGroup
MetricsCollection:
-
Granularity: "1Minute"
Metrics:
- "GroupMinSize"
- "GroupMaxSize"
Tags:
- Key: Name
Value: application
PropagateAtLaunch: true
ApplicationLC:
Type: AWS::AutoScaling::LaunchConfiguration
CreationPolicy:
ResourceSignal:
Timeout: PT5M
DependsOn: Database
Metadata:
AWS::CloudFormation::Init:
configSets:
application_install:
- install_cfn
- install_application
- configure_application
install_cfn:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack= ${AWS::StackId}
region=${AWS::Region}
group: root
mode: '000400'
owner: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.Application.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource ApplicationLC --configsets application_install --url https://stackbuilder.amazonaws.com
group: root
mode: '000400'
owner: root
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
install_application:
sources:
/usr/local/bin/crud-app: "https://3r1co-github-artifacts.s3.amazonaws.com/crud-app.tar.gz"
files:
/etc/systemd/system/crud-app.service:
content: !Sub |
[Unit]
Description=CRUD App Service
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
WorkingDirectory=/usr/local/bin/crud-app
ExecStart=/usr/local/bin/crud-app/crud-app --dbHost ${Database.PrivateIp} --dbName ${DBName} --dbUser ${DBUser} --dbPass ${DBPassword}
[Install]
WantedBy=multi-user.target
group: root
mode: '000664'
owner: root
configure_application:
commands:
01_enable_service:
command: "systemctl enable crud-app"
02_start_service:
command: "systemctl start crud-app"
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
InstanceType: !Ref InstanceTypeParameter
KeyName:
Ref: KeyName
SecurityGroups:
- Ref: ApplicationSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
# ${UpdateVariable}
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource ApplicationLC --configsets application_install --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource ApplicationASG --region ${AWS::Region}
Database:
Type: "AWS::EC2::Instance"
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Metadata:
AWS::CloudFormation::Init:
configSets:
database_install:
- install_cfn
- install_database
- configure_database
configure_database:
commands:
01_set_mysql_root_password:
command: !Sub |
mysqladmin -u root password '${DBRootPassword}'
test: !Sub |
$(mysql ${DBName} -u root --password='${DBRootPassword}' >/dev/null 2>&1 </dev/null); (( $? != 0 ))
02_create_database:
command: !Sub |
mysql -u root --password='${DBRootPassword}' < /tmp/setup.mysql
test: !Sub |
$(mysql ${DBName} -u root --password='${DBRootPassword}' >/dev/null 2>&1 </dev/null); (( $? !=0))
install_cfn:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack= ${AWS::StackId}
region=${AWS::Region}
group: root
mode: '000400'
owner: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.WebServer.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource Database --configsets database_install --url https://stackbuilder.amazonaws.com
group: root
mode: '000400'
owner: root
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
install_database:
files:
/tmp/setup.mysql:
content: !Sub |
CREATE DATABASE ${DBName};
USE ${DBName};
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`city` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
GRANT ALL PRIVILEGES ON *.* TO '${DBUser}'@'%' IDENTIFIED BY '${DBPassword}';
group: root
mode: '000400'
owner: root
packages:
yum:
mariadb-server: []
mariadb: []
services:
sysvinit:
mariadb:
enabled: true
ensureRunning: true
Properties:
ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", HVM64]
InstanceType: !Ref InstanceTypeParameter
Tags:
- Key: Name
Value: database
KeyName:
Ref: KeyName
SecurityGroups:
- Ref: DatabaseSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource Database --configsets database_install --region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource Database --region ${AWS::Region}
#############################
# #
# Security Groups #
# #
#############################
LoadBalancerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Enable SSH and HTTP access"
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: tcp
ToPort: 80
ApplicationSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Enable SSH and HTTP access"
SecurityGroupIngress:
- SourceSecurityGroupName: !Ref LoadBalancerSecurityGroup
FromPort: 8080
IpProtocol: tcp
ToPort: 8080
- CidrIp: 0.0.0.0/0 #Attention!! Don't do this in real life!!
FromPort: 22
IpProtocol: tcp
ToPort: 22
DatabaseSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: "Enable SSH and MySQL access"
SecurityGroupIngress:
- SourceSecurityGroupName: !Ref ApplicationSecurityGroup
FromPort: 3306
IpProtocol: tcp
ToPort: 3306
- CidrIp: 0.0.0.0/0 #Attention!! Don't do this in real life!!
FromPort: 22
IpProtocol: tcp
ToPort: 22