-
Notifications
You must be signed in to change notification settings - Fork 1
/
launchtemplate.yaml
161 lines (147 loc) · 4.67 KB
/
launchtemplate.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
AWSTemplateFormatVersion: 2010-09-09
Description: |
This CloudFormation template creates a Security Group,
an Auto Scaling Group behind an Application Load Balancer with
a Launch Template, a Target Group, a Listener, and a CPU Policy for the Target Group.
Parameters:
myVPC:
Description: Select your VPC
Type: AWS::EC2::VPC::Id
mySubnets:
Description: Select at least 2 subnets
Type: List<AWS::EC2::Subnet::Id>
KeyPair:
Description: Select your Key
Type: AWS::EC2::KeyPair::KeyName
InstanceType:
Description: Select instance type
Type: String
Default: t2.micro
AllowedValues:
- t2.micro
- t3.micro
- t2.nano
- t3.nano
Mappings:
RegionImageMap:
us-east-1:
AMI: ami-0dc2d3e4c0f9ebd18
us-east-2:
AMI: ami-0233c2d874b811deb
us-west-1:
AMI: ami-0ed05376b59b90e46
us-west-2:
AMI: ami-0dc8f589abe99f538
eu-west-1:
AMI: ami-058b1b7fe545997ae
Resources:
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
SecurityGroups:
- !GetAtt SecurityGroup.GroupId
Subnets: !Ref mySubnets
Type: application
ALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn: !Ref ServerTargetGroup
LoadBalancerArn: !Ref ApplicationLoadBalancer
Port: 80
Protocol: HTTP
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
AvailabilityZones: !GetAZs
DesiredCapacity: "2"
HealthCheckGracePeriod: 90
HealthCheckType: ELB
LaunchTemplate:
LaunchTemplateId: !Ref ServerLaunchTemplate
Version: "1"
MaxSize: "3"
MinSize: "1"
TargetGroupARNs:
- !Ref ServerTargetGroup
CPUPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName: !Ref AutoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
TargetValue: 80.0
ServerLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !FindInMap
- RegionImageMap
- !Ref AWS::Region
- AMI
InstanceType: !Ref InstanceType
KeyName: !Ref KeyPair
SecurityGroupIds:
- !GetAtt SecurityGroup.GroupId
TagSpecifications:
- ResourceType: instance
Tags:
- Key: Name
Value: !Sub Web Server of ${AWS::StackName} Stack
UserData: !Base64 |
#! /bin/bash
yum update -y
yum install -y httpd
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& PRIVATE_IP=`curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4`
TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"` \
&& PUBLIC_IP=`curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/public-ipv4`
DATE_TIME=`date`
chmod -R 777 /var/www/html
echo "<html>
<head>
<title> Congratulations! You have created an instance from Launch Template</title>
</head>
<body>
<h1>This web server is launched from launch template that created by Enes</h1>
<p>This instance is created at <b>$DATE_TIME</b></p>
<p>Private IP address of this instance is <b>$PRIVATE_IP</b></p>
<p>Public IP address of this instance is <b>$PUBLIC_IP</b></p>
</body>
</html>" > /var/www/html/index.html
systemctl start httpd
systemctl enable httpd
ServerTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 10
HealthCheckTimeoutSeconds: 5
HealthyThresholdCount: 5
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 3
VpcId: !Ref myVPC
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enables SSH and HTTP
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
VpcId: !Ref myVPC
Outputs:
ServerURL:
Description: Web Server Application Load Balancer URL
Value: !Join
- ""
- - "http://"
- !GetAtt ApplicationLoadBalancer.DNSName