-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeDeployAutoscalingTomcatInVPC.yaml
464 lines (438 loc) · 14.9 KB
/
CodeDeployAutoscalingTomcatInVPC.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# CodeDeployAutoscalingTomcatInVPC.yaml
#
# This is a CloudFormation template that creates the sample environment
# for the demonstration of the autoscaling deployments for AWS CodeDeploy.
#
# The main components that are created include:
#
# LunchConfig - This is a launch configuration that will
# bring up instances with version 1 of the code.
#
AWSTemplateFormatVersion: '2010-09-09'
Description: Create AutoScaling, ELB with CodeDeploy in VPC, 2 public subnets, 2 private subnets
# Define the mappings for the AmazonLinux in the regions that
# offer CodeDeploy.
Mappings:
RegionMap:
ap-northeast-1:
amazonlinux: ami-2803ac4e
ap-northeast-2:
amazonlinux: ami-fc862292
# Offer the IP of the workstation host so the template user can log in.
Outputs:
LoadBalancer:
Description: DNS Name of AWS Elastic Load Balancer
Value: !Join ["", ["http://", !GetAtt LoadBalancer.DNSName]]
ApplicationBucket:
Description: Use this bucket for deploy application
Value: !Ref DeploymentBucket
AutoScalingGroup:
Description: Initial auto scaling group
Value: !Ref AutoScalingGroup
CloudWatchLogsLink:
Description: LogStream list
Value: !Join ["", ["https://", !Ref "AWS::Region", ".console.aws.amazon.com/cloudwatch/home?region=ap-northeast-2#logs:" ] ]
CloudWatchLogGroupName:
Description: The name of the CloudWatch log group
Value: !Join ["", ["https://", !Ref "AWS::Region", ".console.aws.amazon.com/cloudwatch/home?region=ap-northeast-2#logStream:group=", !Ref TomcatLogGroup ]]
CloudWatchChart:
Description: Sample Chart of requests and BytesTransferred
Value: !Join ["", ["https://", !Ref "AWS::Region", ".console.aws.amazon.com/cloudwatch/home?region=ap-northeast-2#metricsV2:graph=~(view~'timeSeries~stacked~false~metrics~(~(~'test*2f200s~'test200Count~(period~10~stat~'Sum))~(~'test*2fBytesTransferred~'testBytesTransferred~(yAxis~'right~period~10~stat~'Sum))~(~'test*2f404s~'test404Count~(period~10~stat~'Sum)))~region~'ap-northeast-2);namespace=test/404s" ]]
# Request a key pair, AZ, and a prefix that will be used to assign
# resource name tags.
Parameters:
KeyPairName:
Description: EC2 keypair for hosts
Type: AWS::EC2::KeyPair::KeyName
OperatorEmail:
Description: Email address to notify if there are any scaling operations
Type: String
VpcId:
Description: "Id of an existing VPC"
Type: "String"
PrivateSubnet1:
Description: "PrivateSubnet1"
Type: String
PrivateSubnet2:
Description: "PrivateSubnet2"
Type: String
PublicSubnet1:
Description: "PublicSubnet1"
Type: String
PublicSubnet2:
Description: "PublicSubnet2"
Type: String
Resources:
LoadBalancer:
Type: AWS::ElasticLoadBalancing::LoadBalancer
Properties:
ConnectionSettings:
IdleTimeout: 60
HealthCheck:
HealthyThreshold: '2'
Interval: '7'
Target: HTTP:8080/examples/index.html
Timeout: '5'
UnhealthyThreshold: '2'
Listeners:
- InstancePort: '8080'
InstanceProtocol: HTTP
LoadBalancerPort: '80'
Protocol: HTTP
Scheme: internet-facing
SecurityGroups: [!Ref ELBSecurityGroup ]
Subnets: [!Ref PublicSubnet1, !Ref PublicSubnet2]
Tags:
- Key: Name
Value:
!Join ["", [ !Ref "AWS::StackName", "-elb"] ]
# AutoScalingGroup
#
# This is the autoscaling group for the Blue/Green environment.
# This will be used for version 1 of the code. When version 2 is
# deployed, CodeDeploy will copy this autoscaling group and attach
# new instances to it.
AutoScalingGroup:
Properties:
Cooldown: '60'
LaunchConfigurationName:
Ref: LunchConfig
LoadBalancerNames:
- Ref: LoadBalancer
MaxSize: '2'
MinSize: '2'
Tags:
- Key: Name
PropagateAtLaunch: 'true'
Value:
!Join ["", [ !Ref "AWS::StackName", "-asg"] ]
VPCZoneIdentifier: [!Ref PrivateSubnet1, !Ref PrivateSubnet2 ]
CreationPolicy:
ResourceSignal:
Count: '2'
Timeout: PT10M
Type: AWS::AutoScaling::AutoScalingGroup
# CodeDeploy application
CodeDeployApp:
Type: AWS::CodeDeploy::Application
Properties:
ApplicationName: !Join ["", [ !Ref "AWS::StackName", "-app"] ]
# DeploymentGroup - CodeDeploy deployment group
DeploymentBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Join ["", [ !Ref "AWS::StackName", "-app-bkt"] ]
DeploymentGroup:
Type: AWS::CodeDeploy::DeploymentGroup
Properties:
ApplicationName: !Ref CodeDeployApp
DeploymentGroupName: !Join ["", [ !Ref "AWS::StackName", "-dg"] ]
ServiceRoleArn: !GetAtt DeploymentRole.Arn
AutoScalingGroups: [!Ref AutoScalingGroup]
Deployment:
Description: FirstTime
IgnoreApplicationStopFailures: true
Revision:
RevisionType: S3
S3Location:
Bucket: apps-ap-northeast-2
BundleType: Zip
Key: sample_v2.zip
DeploymentRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- codedeploy.amazonaws.com
Version: '2012-10-17'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole
InstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- Ref: InstanceRole
InstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action:
- sts:AssumeRole
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
- codedeploy.amazonaws.com
Version: '2012-10-17'
Policies:
- PolicyName: LogRolePolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- logs:Create*
- logs:PutLogEvents
- s3:GetObject
Resource:
- arn:aws:logs:*:*:*
- arn:aws:s3:::*
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforAWSCodeDeploy
- arn:aws:iam::aws:policy/AutoScalingReadOnlyAccess
- arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess
- arn:aws:iam::aws:policy/AWSCodeDeployReadOnlyAccess
# WebSecurityGroup
#
# This is the security group for the instances created
# by the launch configuration. Only ports 22 and 80
# are needed.
ELBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable access to ELB
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '80'
IpProtocol: tcp
ToPort: '80'
Tags:
- Key: Name
Value:
!Join ["", [ !Ref "AWS::StackName", "-elb-sg"] ]
VpcId: !Ref VpcId
WebSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable access to web servers
SecurityGroupIngress:
- CidrIp: 0.0.0.0/0
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
- CidrIp: 0.0.0.0/0
FromPort: '80'
IpProtocol: tcp
ToPort: '80'
- SourceSecurityGroupId: !Ref ELBSecurityGroup
FromPort: '8080'
IpProtocol: tcp
ToPort: '8080'
Tags:
- Key: Name
Value:
!Join ["", [ !Ref "AWS::StackName", "-web-sg"] ]
VpcId: !Ref VpcId
# LunchConfig
#
# This is the launch configuration for the autoscaling group.
# The metadata will be used to install version 1 of the
# application and to set up the CodeDeploy agent. It also
# removes the index.html file which has a "higher" priority
# in the Apache server.
TomcatLogGroup:
Type: AWS::Logs::LogGroup
Properties:
RetentionInDays: 14
404MetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName:
Ref: TomcatLogGroup
FilterPattern: "[ip, identity, user_id, timestamp, request, status_code = 404, size, ...]"
MetricTransformations:
- MetricValue: '1'
MetricNamespace: !Ref "AWS::StackName"
MetricName: Response404
200MetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName:
Ref: TomcatLogGroup
FilterPattern: "[ip, identity, user_id, timestamp, request, status_code = 200, size, ...]"
MetricTransformations:
- MetricValue: '1'
MetricNamespace: !Ref "AWS::StackName"
MetricName: Response200
BytesTransferredMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
LogGroupName:
Ref: TomcatLogGroup
FilterPattern: "[ip, identity, user_id, timestamp, request, status_code, size, ...]"
MetricTransformations:
- MetricValue: "$size"
MetricNamespace: !Ref "AWS::StackName"
MetricName: BytesTransferred
404Alarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: The number of 404s is greater than 2 over 2 minutes
MetricName: Response404
Namespace: !Ref "AWS::StackName"
Statistic: Sum
Period: '60'
EvaluationPeriods: '2'
Threshold: '2'
AlarmActions:
- Ref: AlarmNotificationTopic
ComparisonOperator: GreaterThanThreshold
BandwidthAlarm:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: The average volume of traffic is greater 3500 KB over 10 minutes
MetricName: BytesTransferred
Namespace: !Ref "AWS::StackName"
Statistic: Average
Period: '300'
EvaluationPeriods: '2'
Threshold: '3500'
AlarmActions:
- Ref: AlarmNotificationTopic
ComparisonOperator: GreaterThanThreshold
AlarmNotificationTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint:
Ref: OperatorEmail
Protocol: email
LunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- amazonlinux1
amazonlinux1:
commands:
amazonlinux1cmd4:
command: mkdir -p /usr/share/tomcat7/logs
command: chown -R ec2-user:ec2-user /home/ec2-user
command: mkdir -p /etc/cfn/hooks.d
files:
/tmp/awslogs/tomcat7_accesslog.conf:
content: !Sub |
[general]
state_file= /var/awslogs/agent-state
[tomcat7_accesslog]
file = /usr/share/tomcat7/logs/localhost_access_log.*
log_group_name = ${TomcatLogGroup}
log_stream_name = {instance_id}/tomcat7.log
datetime_format = '%d/%b/%Y:%H:%M:%S %z'
mode: '000400'
owner: tomcat
group: tomcat
/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.LunchConfig.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v -s ${AWS::StackId} -r LunchConfig --region ${AWS::Region}
runas=root
group: root
mode: '000400'
owner: root
#
# codedeployinstall
#
# This copies the code deploy installation script from the AWS S3 buckets.
#
/home/ec2-user/codedeployinstall:
group: ec2-user
mode: '000755'
owner: ec2-user
source:
!Join [ "", [ "http://aws-codedeploy-", !Ref "AWS::Region",".s3.amazonaws.com/latest/install" ] ]
#
# codedeployinstallpt2
#
# This actually installs the code deploy agent. A script is used
# because we need ingest a "yes" into the installation process.
# This will be run in the command section below.
#
/home/ec2-user/codedeployinstallpt2:
content: !Sub |
#!/bin/bash
yes|/home/ec2-user/codedeployinstall auto
group: ec2-user
mode: '000755'
owner: ec2-user
commands:
amazonlinux1cmd1:
command: /home/ec2-user/codedeployinstallpt2
packages:
yum:
aws-cfn-bootstrap: []
aws-cli: []
java-1.8.0-openjdk-headless.x86_64: []
tomcat7: []
tomcat7-webapps: []
# ec2-ami-tools: []
# ec2-api-tools: []
# sysv-rc-conf: []
python:
boto3: []
awscli: []
services:
sysvinit:
tomcat7:
enabled: true
ensureRunning: true
Properties:
IamInstanceProfile: !Ref InstanceProfile
ImageId:
Fn::FindInMap:
- RegionMap
- Ref: AWS::Region
- amazonlinux
InstanceType: t2.micro
KeyName: !Ref KeyPairName
SecurityGroups:
- Ref: WebSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" --stack ${AWS::StackId} --resource AutoScalingGroup --region ${AWS::Region}
exit 1
}
yum -y update
yum -y install aws-cfn-bootstrap aws-cli java-1.8.0-openjdk-headless.x86_64 tomcat7 tomcat7-webapps python-setuptools python-pip python-software-properties
mkdir -p /tmp/codedeploy-test
rm -rf /usr/share/tomcat7/webapps/ROOT/*
rm -rf /usr/share/tomcat7/examples
rm -rf /usr/share/tomcat7/sample
mkdir -p /usr/share/tomcat7/logs
service tomcat7 start
easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
# Get the CloudWatch Logs agent
wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
# Install the CloudWatch Logs agent
/opt/aws/bin/cfn-init --region ${AWS::Region} -s ${AWS::StackId} -r LunchConfig
#/opt/aws/bin/cfn-hup || error_exit ''Failed to start cfn-hup''
python awslogs-agent-setup.py -n -r ${AWS::Region} -c /tmp/awslogs/tomcat7_accesslog.conf || error_exit 'Failed to run CloudWatch Logs agent setup'
/opt/aws/bin/cfn-signal -e 0 -r 'BlueGreen configuration' --stack ${AWS::StackId} --resource AutoScalingGroup --region ${AWS::Region}