-
Notifications
You must be signed in to change notification settings - Fork 0
/
servers.yaml
275 lines (251 loc) · 7.04 KB
/
servers.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
AWSTemplateFormatVersion: 2010-09-09
Description: |
Udagram CloudFormation Servers Deployment.
- Launch Configuration and Group
- Application Load Balancer
- Bastion Host
- IAM Service Role
- Security Group
Parameters:
ENVIRONMENTNAME:
Description: An environment name that will be prefixed to resource names.
Type: String
WEBPORT:
Description: Enter the TCP Port where Apache is going to listen. Default is 80.
Type: String
Default: 80
WEBSERVERAMI:
Description: The AMI used to deploy web severs. Default is Ubuntu18, ami-0d1cd67c26f5fca19
Type: String
Default: ami-0d1cd67c26f5fca19
WEBSERVERTYPE:
Description: The instance type used to deploy web severs. Default is t2.medium
Type: String
Default: t2.medium
BASTIONAMI:
Description: The AMI used to deploy the bastion. Default is ami-0ce21b51cb31a48b8
Type: String
Default: ami-0ce21b51cb31a48b8
BASTIONTYPE:
Description: The instance type used the bastion. Default is t2.micro
Type: String
Default: t2.micro
BASTIONKEY:
Description: The Key to deploy in the bastion
Type: String
Default: UDAGRAM-BASTION
MYIP:
Description: Home IP to access the bastion
Type: String
Default: 79.155.6.195/32
Resources:
S3READROLE:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
Policies:
- PolicyName: WebServersS3ReadRole
PolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: "Allow"
Action:
- S3:List*
- S3:Get*
Resource: "*"
INSTANCEPROFILE:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- !Ref S3READROLE
BASTIONSECURITYGROUP:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref MYIP
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-BASTIONSECURITYGROUP
VpcId:
Fn::ImportValue: udagram-network-VPC
BASTION:
Type: 'AWS::EC2::Instance'
Properties:
SecurityGroupIds:
- !Ref BASTIONSECURITYGROUP
KeyName: !Ref BASTIONKEY
ImageId: !Ref BASTIONAMI
InstanceType: !Ref BASTIONTYPE
SubnetId:
Fn::ImportValue: udagram-network-PUBSUBNET1
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-BASTION
WEBSERVERSECURITYGROUP:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: WebServers Security Group
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 0
ToPort: 65535
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref WEBPORT
ToPort: !Ref WEBPORT
CidrIp: 0.0.0.0/0
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-WEBSERVERGROUP
VpcId:
Fn::ImportValue: udagram-network-VPC
LBSECURITYGROUP:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: LB Security Group
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: !Ref WEBPORT
ToPort: !Ref WEBPORT
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: !Ref WEBPORT
ToPort: !Ref WEBPORT
CidrIp: 0.0.0.0/0
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-LBSECURITYGROUP
VpcId:
Fn::ImportValue: udagram-network-VPC
WEBSERVERLAUNCH:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeSize: 10
VolumeType: gp2
ImageId: !Ref WEBSERVERAMI
InstanceType: !Ref WEBSERVERTYPE
IamInstanceProfile: !Ref INSTANCEPROFILE
SecurityGroups:
- !Ref WEBSERVERSECURITYGROUP
UserData:
Fn::Base64: !Sub |
#!/bin/bash
apt-get update -y
apt-get install apache2 awscli -y
systemctl start apache2.service
cd /var/www/html
aws s3 cp s3://udagram-bucket-project/index.html ./
WEBTARGETGROUP:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
HealthCheckIntervalSeconds: 35
HealthCheckPath: /
HealthCheckProtocol: HTTP
HealthCheckTimeoutSeconds: 30
HealthyThresholdCount: 2
Port: 80
Protocol: HTTP
UnhealthyThresholdCount: 5
VpcId:
Fn::ImportValue: udagram-network-VPC
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-WEBTARGETGROUP
WEBSERVERAUTOSCALINGGROUP:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
HealthCheckGracePeriod: 120
HealthCheckType: EC2
LaunchConfigurationName: !Ref WEBSERVERLAUNCH
MaxSize: 8
MinSize: 4
TargetGroupARNs:
- !Ref WEBTARGETGROUP
VPCZoneIdentifier:
- Fn::ImportValue: udagram-network-PRIVSUBNETS
Tags:
- Key: Environment
Value: !Ref ENVIRONMENTNAME
PropagateAtLaunch: "true"
-
Key: Name
Value: !Sub ${ENVIRONMENTNAME}-WEBSERVERAUTOSCALINGGROUP
PropagateAtLaunch: "true"
LOADBALANCER:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
SecurityGroups:
- !Ref LBSECURITYGROUP
Subnets:
- Fn::ImportValue: udagram-network-PUBSUBNET1
- Fn::ImportValue: udagram-network-PUBSUBNET2
LISTENER:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- Type: forward
TargetGroupArn:
!Ref WEBTARGETGROUP
LoadBalancerArn:
Ref: LOADBALANCER
Port: '80'
Protocol: HTTP
LBLISTENERRULE:
Type: AWS::ElasticLoadBalancingV2::ListenerRule
Properties:
Actions:
- Type: forward
TargetGroupArn: !Ref WEBTARGETGROUP
Conditions:
- Field: path-pattern
Values: [/]
ListenerArn: !Ref LISTENER
Priority: 1
Outputs:
StackLBDNS:
Description: The URL of the Load Balancer
Value: !Sub
- http://${LBDNS}
- { LBDNS: !GetAtt LOADBALANCER.DNSName}
Export:
Name: !Sub "${AWS::StackName}-LB"
StackBASTIONDNS:
Description: The DNS of the Bastion Host
Value: !GetAtt BASTION.PublicDnsName
Export:
Name: !Sub "${AWS::StackName}-BASTION"