-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.yml
229 lines (209 loc) · 5.7 KB
/
servers.yml
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
Description: >
Luis Fernandez / UdacityProjectLuis
This template deploys a the servers
Parameters:
EnvironmentName:
Description: An Environment name that will be prefixed to resources
Type: String
DNSProtocol:
Description: An DNS protocol that will be prefixed to resources
Type: String
ApplicationS3Bucket:
Description: Bucket name for Application
Type: String
InstanceType:
Description: WebServer EC2 instance type
Type: String
Default: t3.medium
Resources:
LBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow http traffic to the load balancer
VpcId:
Fn::ImportValue:
!Sub "${EnvironmentName}-VPC-ID"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
WebServerSecGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Allow http traffic to our hosts and SHH for local only
VpcId:
Fn::ImportValue:
!Sub "${EnvironmentName}-VPC-ID"
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp:
Fn::ImportValue:
!Sub ${EnvironmentName}-BASTION-ONE-HOST-CIDR
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp:
Fn::ImportValue:
!Sub ${EnvironmentName}-BASTION-TWO-HOST-CIDR
WebAppLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId: ami-07b4f3c02c7f83d59
KeyName:
Fn::ImportValue:
!Sub ${EnvironmentName}-JUMPBOX-SSH-KEY-NAME
IamInstanceProfile: !Ref IamInstanceProfile
SecurityGroups:
- Ref: WebServerSecGroup
InstanceType: !Ref InstanceType
BlockDeviceMappings:
- DeviceName: "/dev/sdk"
Ebs:
VolumeSize: '10'
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update -y
apt-get install unzip awscli -y
apt-get install apache2 -y
systemctl start apache2.service
cd /var/www/html
aws s3 cp s3://udacity-demo-1/udacity.zip .
unzip -o udacity.zip
WebAppGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- Fn::ImportValue:
!Sub "${EnvironmentName}-PRIVATE-NETS"
LaunchConfigurationName:
Ref: WebAppLaunchConfig
MinSize: '4'
MaxSize: '5'
TargetGroupARNs:
- Ref: WebAppTargetGroup
WebAppLB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Subnets:
- Fn::ImportValue: !Sub "${EnvironmentName}-PUBLIC-ONE-SUBNET"
- Fn::ImportValue: !Sub "${EnvironmentName}-PUBLIC-TWO-SUBNET"
SecurityGroups:
- Ref: LBSecurityGroup
Listener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: WebAppTargetGroup
LoadBalancerArn:
Ref: WebAppLB
Port: '80'
Protocol: HTTP
ALBListenerRule:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref WebAppTargetGroup
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref Listener
Priority: 1
WebAppTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
DependsOn:
- WebAppLB
Properties:
HealthCheckIntervalSeconds: 20
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 10
HealthyThresholdCount: 2
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 5
VpcId:
Fn::ImportValue:
!Sub "${EnvironmentName}-VPC-ID"
UdacityS3ReadOnlyEC2:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: "/"
IamPolicies:
Type: AWS::IAM::Policy
Properties:
PolicyName: root
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action: "s3:*"
Resource: "*"
Roles:
- !Ref UdacityS3ReadOnlyEC2
IamInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: "/"
Roles:
- !Ref UdacityS3ReadOnlyEC2
WebServerScaleUpPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref WebAppGroup
Cooldown: '60'
ScalingAdjustment: '1'
CPUAlarmHigh:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Scale-up if CPU > 95% for 5 minutes
MetricName: CPUUtilization
Namespace: AWS/EC2
Statistic: Average
Period: '300'
EvaluationPeriods: '3'
Threshold: '95'
AlarmActions:
- Ref: WebServerScaleUpPolicy
Dimensions:
- Name: AutoScalingGroupName
Value:
Ref: WebAppGroup
ComparisonOperator: GreaterThanThreshold
Outputs:
WebAppLB:
Description: A reference to the Loadbalancer DNS Name
Value:
Fn::Join:
- ''
- - !Sub ${DNSProtocol}
- Fn::GetAtt:
- WebAppLB
- DNSName
Export:
Name: !Sub ${EnvironmentName}-LOAD-BALANCER-DNS