-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project8template.yaml
147 lines (130 loc) · 4.02 KB
/
Project8template.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
AWSTemplateFormatVersion: "2010-09-09"
Description: This template creates a VPC with public and private subnets and an autoscaling group with a target tracking policy. NGINX Web Server and Stress Test tool are also installed on each EC2 instance at boot.
Parameters:
VpcCidr:
Default: 10.10.0.0/16
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Subnets:
Type: 'List<AWS::EC2::Subnet::Id>'
Description: The list of Subnets in your Virtual Private Cloud (VPC)
ConstraintDescription:
must be a list of at least two existing subnets associated with at least
two different availability zones.
CPUPolicyTargetValue:
Default: 80
Description: Please enter the target CPU utilization
Type: String
LaunchTemplateVersionNumber:
Default: 1
Type: String
SSHLocation:
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Default: 0.0.0.0/0
Description: The IP address range that can be used to access the web server using SSH.
MaxLength: '18'
MinLength: '9'
Type: String
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref VpcCidr
EnableDnsHostnames: True
EnableDnsSupport: True
InstanceTenancy: default
Tags:
- Key: Name
Value: Project8VPC
InternetGateway:
Type: AWS::EC2::InternetGateway
Properties:
Tags:
- Key: Name
Value: Project8IGW
InternetGatewayAttachment:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
#Create Route Table
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
Tags:
- Key: Name
Value: PublicRouteTable
VpcId: !Ref VPC
PublicRoute:
Type: AWS::EC2::Route
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
RouteTableId: !Ref PublicRouteTable
#Create Security Group
InstanceSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow access on ports 80 and 22
GroupName: MyWebServerSG
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 108.185.98.33/32
Tags:
- Key: Name
Value: MyWebServerSG
VpcId: vpc-0e8715e0609c5d988
#Create Launch Template
LaunchTemplate:
Type: 'AWS::EC2::LaunchTemplate'
Properties:
LaunchTemplateName: !Sub '${AWS::StackName}-launch-template-for-auto-scaling'
LaunchTemplateData:
NetworkInterfaces:
- DeviceIndex: 0
AssociatePublicIpAddress: true
Groups:
- !Ref InstanceSecurityGroup
DeleteOnTermination: true
Placement:
Tenancy: default
ImageId: ami-0022f774911c1d690
InstanceType: t2.micro
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum update -y
yum install nginx -y
systemctl start nginx
systemctl enable nginx
amazon-linux-extras install epel -y
yum install stress -y
#Create AutoScaling Group
AutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !Ref LaunchTemplateVersionNumber
MinSize: '2'
MaxSize: '5'
DesiredCapacity: '3'
VPCZoneIdentifier: !Ref Subnets
#AutoScaling Group Tracking Policy
myCPUPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName: !Ref AutoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
TargetValue: !Ref CPUPolicyTargetValue