-
Notifications
You must be signed in to change notification settings - Fork 59
/
webapp-autoscaling-webserver.yaml
296 lines (244 loc) · 8.16 KB
/
webapp-autoscaling-webserver.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
# 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.yaml file
# to meet your instances spin numbers.
#
---
AWSTemplateFormatVersion: "2010-09-09"
Description: >
This template deploys an AutoScaling Launch Config and Group for WebServer
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"
PMPublicSubnets:
Description: "Subnets to launch instances into"
Type: "List<AWS::EC2::Subnet::Id>"
PMProxyHostSG:
Description: "Select the Security Group to use for the EC2 hosts"
Type: "AWS::EC2::SecurityGroup::Id"
PMWEBDOMAIN:
Description: "A reference to the Webserver domain name"
Type: "String"
PMAPPLoadBalancerUrl:
Description: "A reference to the AppServer Load Balancer"
Type: "String"
PMWEBLoadBalancer:
Description: "A reference to the Webserver 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:
# Auto Scaling Launch Configuration
LaunchConfiguration:
Type: "AWS::AutoScaling::LaunchConfiguration"
Properties:
AssociatePublicIpAddress: "true"
KeyName:
Ref: "PMKeyName"
ImageId:
Ref: "PMRegionAMI"
InstanceType:
Ref: "PMInstanceType"
IamInstanceProfile:
Ref: "PMIAMS3CWInstanceProfile"
SecurityGroups:
- Ref: "PMProxyHostSG"
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum install nginx htop telnet git awslogs python-pip aws-cfn-bootstrap -y
yum update -y
mkdir -p /etc/nginx/sites-enabled
mkdir -p /etc/nginx/sites-available
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 "<?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}
echo "server {
server_name ${PMWEBDOMAIN};
listen 80;
root /home/www/public_html/${PMWEBDOMAIN}/;
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \\.php\$ {
try_files \$uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)\$;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
}" > /etc/nginx/sites-available/${PMWEBDOMAIN}
ln -sf /etc/nginx/sites-available/${PMWEBDOMAIN} /etc/nginx/sites-enabled/
echo "server {
listen 80 default;
server_name _;
access_log /var/log/nginx/localhost.access.log;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
}" > /etc/nginx/sites-available/default
ln -sf /etc/nginx/sites-available/default /etc/nginx/sites-enabled/
echo "proxy_redirect off;
client_body_buffer_size 256K;
client_max_body_size 12m;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;" > /etc/nginx/proxy.conf
echo "user nginx;
worker_processes auto;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
worker_rlimit_nofile 100000;
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# set client body size to 32M #
client_max_body_size 32M;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_static on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 9;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css text/csv application/json application/javascript application/x-javascript image/x-icon text/xml application/xml application/xml+rss text/javascript;
##
# open_file_cache optimization
##
open_file_cache max=200000 inactive=50s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
##
# Upstream Server
##
upstream php {
server ${PMAPPLoadBalancerUrl}:9000;
}
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}" > /etc/nginx/nginx.conf
service nginx start
chkconfig nginx on
/opt/aws/bin/cfn-signal -e 0 -r 'server setup complete' "${WaitHandleWEB}" > /tmp/userdata.log
WaitHandleWEB:
Type: "AWS::CloudFormation::WaitConditionHandle"
WaitCondition:
Type: "AWS::CloudFormation::WaitCondition"
Properties:
Handle: !Ref "WaitHandleWEB"
Timeout: '600'
# Auto Scaling Group Basic Setup
WebScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
Properties:
MinSize: !Ref "PMASMIN"
MaxSize: !Ref "PMASMAX"
DesiredCapacity: !Ref "PMASDES"
LaunchConfigurationName:
Ref: "LaunchConfiguration"
VPCZoneIdentifier:
Ref: "PMPublicSubnets"
LoadBalancerNames:
- Ref: "PMWEBLoadBalancer"
HealthCheckGracePeriod: "300"
HealthCheckType: "ELB"
Tags:
- Key: "Name"
Value: !Sub "${PMServerEnv}-WEBserver"
PropagateAtLaunch: 'true'
# Auto ScalingUp Policy - Basic Setup
WebServerScaleUpPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
AdjustmentType: "ChangeInCapacity"
AutoScalingGroupName:
Ref: "WebScalingGroup"
Cooldown: '300'
ScalingAdjustment: '1'
# Auto ScalingDown Policy - Basic Setup
WebServerScaleDownPolicy:
Type: "AWS::AutoScaling::ScalingPolicy"
Properties:
AdjustmentType: "ChangeInCapacity"
AutoScalingGroupName:
Ref: "WebScalingGroup"
Cooldown: '300'
ScalingAdjustment: '-1'
Outputs:
WebScalingGroup:
Description: "Auto Scaling Group Reference ID"
Value: !Ref "WebScalingGroup"
WebServerScaleUpPolicy:
Description: "Auto Scaling Up Policy Reference ID"
Value: !Ref "WebServerScaleUpPolicy"
WebServerScaleDownPolicy:
Description: "Auto Scaling Down Policy Reference ID"
Value: !Ref "WebServerScaleDownPolicy"