forked from thinegan/cloudformation-project1
-
Notifications
You must be signed in to change notification settings - Fork 4
/
webapp-autoscaling-appserver.yaml
181 lines (148 loc) · 5.1 KB
/
webapp-autoscaling-appserver.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
# Note, the development environment will only spin a min of 1 instance (Not HA Support)
# The production environment will spin a min of 2 instance (HA support)
# You can adjust the ASMIN, ASMAX, ASDES at the master.yml file
# to meet your instances spin numbers.
#
---
AWSTemplateFormatVersion: "2010-09-09"
Description: >
This template deploys an AutoScaling Launch Config and Group
Parameters:
PMServerEnv:
Description: "Server Environment name."
ConstraintDescription: "Choose an Environment from the drop down"
Type: "String"
AllowedValues:
- "dev"
- "staging"
- "prod"
PMKeyName:
Description: "Enter an existing EC2 KeyPair. Default is MyEC2Key"
Type: "String"
PMInstanceType:
Description: "Enter t2.micro or m1.small. Default is t2.micro."
Type: "String"
AllowedValues:
- "t2.micro"
- "m1.small"
PMPrivateSubnets:
Description: "Subnets to launch instances into"
Type: "List<AWS::EC2::Subnet::Id>"
PMAPPHostSG:
Description: "Select the Security Group to use for the EC2 hosts"
Type: "AWS::EC2::SecurityGroup::Id"
PMWEBDOMAIN:
Description: "A reference to the Application domain name"
Type: "String"
PMAPPLoadBalancer:
Description: "A reference to the Application Load Balancer"
Type: "String"
PMIAMS3CWInstanceProfile:
Description: "A reference to the IamInstanceProfile"
Type: "String"
PMRegionAMI:
Description: "A reference to the Region AMI"
Type: "String"
PMASMIN:
Description: "A reference to the MinSize"
Type: "String"
PMASMAX:
Description: "A reference to the MaxSize"
Type: "String"
PMASDES:
Description: "A reference to the DesiredCapacity"
Type: "String"
####### Resources Setup #######
Resources:
# Auto Scaling Launch Configuration
LaunchConfiguration:
Type: "AWS::AutoScaling::LaunchConfiguration"
Properties:
AssociatePublicIpAddress: "false"
KeyName:
Ref: "PMKeyName"
ImageId:
Ref: "PMRegionAMI"
InstanceType:
Ref: "PMInstanceType"
IamInstanceProfile:
Ref: "PMIAMS3CWInstanceProfile"
SecurityGroups:
- Ref: "PMAPPHostSG"
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
yum update -y
yum install htop php-fpm php-gd php-mcrypt php-mysql php-xmlrpc php-cli php-devel telnet git awslogs python-pip aws-cfn-bootstrap nginx -y
yum remove nginx -y
echo "[plugins]
cwlogs = cwlogs
[default]
region = ${AWS::Region}" > /etc/awslogs/awscli.conf
service awslogs start
chkconfig awslogs on
mkdir -p /home/www/public_html/${PMWEBDOMAIN}
echo "This is App Server <br> <?php phpinfo(); ?>" > /home/www/public_html/${PMWEBDOMAIN}/index.php
chown -R nginx:nginx /home/www/public_html/${PMWEBDOMAIN}
chmod -R +x /home/www/public_html/${PMWEBDOMAIN}
sed -ie 's/127.0.0.1:9000/9000/g' /etc/php-fpm.d/www.conf
sed -ie 's/listen.allowed_clients/;listen.allowed_clients/g' /etc/php-fpm.d/www.conf
sed -ie 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sed -ie 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf
chkconfig php-fpm on
service php-fpm start
/opt/aws/bin/cfn-signal -e 0 -r 'server setup complete' "${WaitHandleAPP}" > /tmp/userdata.log
WaitHandleAPP:
Type: "AWS::CloudFormation::WaitConditionHandle"
WaitCondition:
Type: "AWS::CloudFormation::WaitCondition"
Properties:
Handle: !Ref "WaitHandleAPP"
Timeout: '600'
# Auto Scaling Group Basic Setup
AppScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
MinSize: !Ref "PMASMIN"
MaxSize: !Ref "PMASMAX"
DesiredCapacity: !Ref "PMASDES"
LaunchConfigurationName:
Ref: "LaunchConfiguration"
VPCZoneIdentifier: !Ref "PMPrivateSubnets"
LoadBalancerNames:
- Ref: "PMAPPLoadBalancer"
HealthCheckGracePeriod: "300"
HealthCheckType: "ELB"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-APPserver"
PropagateAtLaunch: 'true'
# Auto ScalingUp Policy - Basic Setup
APPServerScaleUpPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
AdjustmentType: "ChangeInCapacity"
AutoScalingGroupName:
Ref: "AppScalingGroup"
Cooldown: '300'
ScalingAdjustment: '1'
# Auto ScalingDown Policy - Basic Setup
APPServerScaleDownPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
AdjustmentType: "ChangeInCapacity"
AutoScalingGroupName:
Ref: "AppScalingGroup"
Cooldown: '300'
ScalingAdjustment: '-1'
Outputs:
AppScalingGroup:
Description: "Auto Scaling Group Reference ID"
Value: !Ref "AppScalingGroup"
APPServerScaleUpPolicy:
Description: "Auto Scaling Up Policy Reference ID"
Value: !Ref "APPServerScaleUpPolicy"
APPServerScaleDownPolicy:
Description: "Auto Scaling Down Policy Reference ID"
Value: !Ref "APPServerScaleDownPolicy"