-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2_asg_frontend.yaml
100 lines (97 loc) · 3.43 KB
/
ec2_asg_frontend.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
### 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 Public EC2 Instance
ASGName:
Description: Name of ASG Instance
Type: String
Default: ASG Public EC2
ASGGroupMin:
Description: ASG Property MinSize
Type: String
Default: "1"
ASGGroupMax:
Description: ASG Property MaxSize
Type: String
Default: "3"
ASGGroupDesired:
Description: ASG Property Desired Capacity
Type: String
Default: "1"
### 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
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'
### Outputs
Outputs:
ASGPublicEC2Group:
Description: Public Auto Scaling Group
Value: !Ref ASGPublicEC2Group
Export:
Name: !Sub "${AWS::StackName}-asg-public"
ASGPublicEC2LaunchTemplate:
Description: Launch Template for Public EC2 Fleet
Value: !Ref ASGPublicEC2LaunchTemplate
Export:
Name: !Sub "${AWS::StackName}-asg-lt-public"