-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2_asg_frontend_alb.yaml
242 lines (214 loc) · 7.3 KB
/
ec2_asg_frontend_alb.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
### Shanil V2 ###
### Creates an Autoscaling Group (Public Facing Web Servers) wth a ALB and Healthchecks ###
AWSTemplateFormatVersion: 2010-09-09
Description: Provisions an EC2 Fleet in a Public Subnet for Front Services using a launch template defined below.
### Configure Parameters as needed ###
Parameters:
KeyPair:
Description: Key Pair
Type: AWS::EC2::KeyPair::KeyName
AMIid:
Description: AMI ID
Type: AWS::EC2::Image::Id
Default: ami-09ead922c1dad67e4
InstanceSize:
Description: Enter t2.micro, m1.small, or m1.large. Default is t2.micro.
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- m1.small
- m1.large
ASGInstanceName:
Description: Name of your EC2 Fleet Instances
Type: String
Default: asg-ec2-publ-001
ASGName:
Description: Name of ASG Instance
Type: String
Default: asg-ec2-publ-001
ASGGroupMin:
Description: Min No. of Instances you require running at all times.
Type: String
Default: "1"
ASGGroupMax:
Description: Max No. of Instances you are willing to spin up
Type: String
Default: "3"
ASGGroupDesired:
Description: Your desired No. of Instances (consider Availablity and Reduntancy)
Type: String
Default: "1"
ALBName:
Description: Name of your Application Load Balancer sitting in front of your Public EC2 Fleet
Type: String
Default: "alb-publ-001"
ALBHealthIntervalSec:
Description: Define the healthcheck period you require.
Type: String
Default: "30"
HealthCheckPath:
Description: Define your Health Check Path
Type: String
Default: "/healthcheck.html"
ALBHealthPort:
Description: Define the port for healthcheck (80 is default however please change)
Type: String
Default: "80"
ALBHealthTCP:
Description: Protocol used for healthcheck (HTTP)
Type: String
Default: "HTTP"
ALBHealthTimeout:
Description: Time out between healthchecks
Type: String
Default: "15"
ALBHealthThreshold:
Description: ALB Health Threshold Count
Type: String
Default: "5"
ALBPort:
Description: Port forwarded to Target Group
Type: String
Default: "80"
ALBProtocol:
Description: Protocol forwarded to Target Group
Type: String
Default: "HTTP"
SecurityGroupPublic:
Description: Specify the Security Group used for Public Instances
Type: "AWS::EC2::SecurityGroup::Id"
### ASG Launch Template and Group
Resources:
### Launch Template
ASGPublicEC2LaunchTemplate:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateName: ASGPublicEC2
LaunchTemplateData:
InstanceType: !Ref InstanceSize
ImageId: !Ref AMIid
KeyName: !Ref KeyPair
SecurityGroupIds:
- !ImportValue multi-az-vpc-sg-public
- !ImportValue multi-az-vpc-sg-public-mgmt
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y # Update packages
amazon-linux-extras install -y lamp-mariadb10.2-php7.2 php7.2 #Install LAMP packages
yum install -y httpd mariadb-server
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php
curl http://169.254.169.254/latest/meta-data/public-hostname > /var/www/html/index.html #Creates a simple page with DNS name and ALB/CLB Monitor url.
echo "<html><h1>Healthcheck Test Page</h1></html>" > /var/www/html/healthcheck.html
usermod -a -G apache ec2-user # Assigns Perms to Dirs.
chown -R ec2-user:apache /var/www
chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \;
find /var/www -type f -exec sudo chmod 0664 {} \;
systemctl start httpd && systemctl enable httpd # starts HTTPD and Maria DB
systemctl start mariadb && systemctl enable mariadb
### Autoscale Group
ASGPublicEC2Group:
Type: "AWS::AutoScaling::AutoScalingGroup"
DependsOn: "ASGPublicEC2LaunchTemplate"
Properties:
MaxSize: !Ref ASGGroupMax
MinSize: !Ref ASGGroupMin
DesiredCapacity: !Ref ASGGroupDesired
LaunchTemplate:
LaunchTemplateId: !Ref ASGPublicEC2LaunchTemplate
Version: 1
VPCZoneIdentifier: !Split [",", !ImportValue multi-az-vpc-public-subnet-id]
Tags:
- Key: Name
Value: !Ref ASGInstanceName
PropagateAtLaunch: 'true'
TargetGroupARNs:
- !Ref ALBPublicEC2GroupTargetGroup
### ALB Security Group
ALBPublicEC2SecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: ALB Public EC2 Group Security Group
GroupName: Public Load Balancer Security Group
SecurityGroupEgress:
-
IpProtocol: -1
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
-
IpProtocol: tcp
FromPort: "80"
ToPort: "80"
SourceSecurityGroupId: !Ref SecurityGroupPublic
Tags:
- Key: Name
Value: Public Load Balancer Security Group
VpcId: !ImportValue multi-az-vpc
### ALB Loadbalancer Config
ALBPublicEC2Group:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Type: application
IpAddressType: ipv4
Scheme: internet-facing
Subnets:
- !Select [0, !Split [",", !ImportValue multi-az-vpc-public-subnet-id]]
- !Select [1, !Split [",", !ImportValue multi-az-vpc-public-subnet-id]]
- !Select [2, !Split [",", !ImportValue multi-az-vpc-public-subnet-id]]
SecurityGroups:
- !ImportValue multi-az-vpc-sg-public
- !Ref ALBPublicEC2SecurityGroup
### ALB Listener
ALBPublicEC2GroupListener:
Type: AWS::ElasticLoadBalancingV2::Listener
DependsOn: ALBPublicEC2GroupTargetGroup
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: ALBPublicEC2GroupTargetGroup
LoadBalancerArn:
Ref: ALBPublicEC2Group
Port: 80
Protocol: HTTP
### ALB Target Group - Use Parameters to configure
ALBPublicEC2GroupTargetGroup:
Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
Properties:
TargetType: instance
HealthCheckIntervalSeconds: !Ref ALBHealthIntervalSec
HealthCheckPath: !Ref HealthCheckPath
HealthCheckPort: !Ref ALBHealthPort
HealthCheckProtocol: !Ref ALBHealthTCP
HealthCheckTimeoutSeconds: !Ref ALBHealthTimeout
HealthyThresholdCount: !Ref ALBHealthThreshold
Name: !Ref ALBName
Port: !Ref ALBPort
Protocol: !Ref ALBProtocol
VpcId: !ImportValue multi-az-vpc
Tags:
- Key: Name
Value: !Ref ALBName
### Outputs
Outputs:
ASGPublicEC2Group:
Description: Public Auto Scaling Group
Value: !Ref ASGPublicEC2Group
Export:
Name: !Sub "${AWS::StackName}-asg-publ-001"
ASGPublicEC2LaunchTemplate:
Description: Launch Template for Public EC2 Fleet
Value: !Ref ASGPublicEC2LaunchTemplate
Export:
Name: !Sub "${AWS::StackName}-asg-lt-publ-001"
ALBPublicEC2Group:
Description: ALB for Back-End Instance
Value: !Ref ALBPublicEC2Group
Export:
Name: !Sub "${AWS::StackName}-alb-publ-001"
ALBPublicEC2GroupTargetGroup:
Description: ALB for Back-End Instance
Value: !Ref ALBPublicEC2GroupTargetGroup
Export:
Name: !Sub "${AWS::StackName}-alb-publ-tg-001"