-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSAA-Lab01-Stack-Private.yaml
617 lines (616 loc) · 18.5 KB
/
SAA-Lab01-Stack-Private.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
AWSTemplateFormatVersion: 2010-09-09
Description: This template will create the Infrastructure for the Lab01 sample application. Instances are created in Private Subnets and NAT Gateways are created for Outbound only internet access meant for instances in Private Subnets.
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
-
Label:
default: "Network Configuration"
Parameters:
- VpcCIDR
- PubSub01CIDR
- PubSub02CIDR
- PvtSub01CIDR
- PvtSub02CIDR
- NATGatewaysRequired
-
Label:
default: "Application Configuration"
Parameters:
- Environment
- MinSize
- MaxSize
- DesiredCapacity
- LatestAmazonLinux2
- KeyPair
Parameters:
Environment:
Type: String
Description: Type of environment to provision
Default: DEV
AllowedValues:
- DEV
- TEST
- PROD
VpcCIDR:
Description: "Please enter the IP range (CIDR notation) for this VPC"
Type: String
Default: 10.0.0.0/16
PubSub01CIDR:
Description: "Please enter the IP range (CIDR notation) for the public subnet in the first Availability Zone"
Type: String
Default: 10.0.3.0/24
PubSub02CIDR:
Description: "Please enter the IP range (CIDR notation) for the public subnet in the second Availability Zone"
Type: String
Default: 10.0.4.0/24
PvtSub01CIDR:
Description: "Please enter the IP range (CIDR notation) for the private subnet in the first Availability Zone"
Type: String
Default: 10.0.1.0/24
PvtSub02CIDR:
Description: "Please enter the IP range (CIDR notation) for the private subnet in the second Availability Zone"
Type: String
Default: 10.0.2.0/24
NATGatewaysRequired:
Description: Please mention do you require the two NATGateways to be installed at this stage.
Type: String
Default: "No"
AllowedValues:
- "Yes"
- "No"
MinSize:
Type: String
Description: Minimum number of app servers
Default: "2"
MaxSize:
Type: String
Description: Maximum number of app servers (Desired x 2)
Default: "4"
DesiredCapacity:
Type: String
Description: Nominal Size of APP Env
Default: "2"
LatestAmazonLinux2:
Description: Latest Amazon Linux 2 AMI is auto selected
Type: "AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>"
Default: "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2"
KeyPair:
Description: Please select the EC2 key pair from dropdown
Type: "AWS::EC2::KeyPair::KeyName"
Mappings:
InstanceSize:
DEV:
"EC2": "t2.micro"
TEST:
"EC2": "t2.micro"
PROD:
"EC2": "t2.micro"
Conditions:
isRequired: !Equals [!Ref NATGatewaysRequired, "Yes"]
isntRequired: !Not [!Equals [!Ref NATGatewaysRequired, "No"]]
Resources:
VPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: !Ref VpcCIDR
EnableDnsSupport: "true"
EnableDnsHostnames: "true"
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", VPC]]
- Key: Env
Value: !Ref Environment
InternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", IGW]]
- Key: Env
Value: !Ref Environment
InternetGatewayAttachment:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref InternetGateway
VpcId: !Ref VPC
PubSub01:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Ref PubSub01CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PubSub01]]
- Key: Env
Value: !Ref Environment
PubSub02:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: !Ref PubSub02CIDR
MapPublicIpOnLaunch: true
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PubSub02]]
- Key: Env
Value: !Ref Environment
PvtSub01:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [1, !GetAZs ""]
CidrBlock: !Ref PvtSub01CIDR
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PvtSub01]]
- Key: Env
Value: !Ref Environment
PvtSub02:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref VPC
AvailabilityZone: !Select [0, !GetAZs ""]
CidrBlock: !Ref PvtSub02CIDR
MapPublicIpOnLaunch: false
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PvtSub02]]
- Key: Env
Value: !Ref Environment
PublicRouteTable:
Type: "AWS::EC2::RouteTable"
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PubRT]]
- Key: Env
Value: !Ref Environment
DefaultPublicRoute:
Type: "AWS::EC2::Route"
DependsOn: InternetGatewayAttachment
Properties:
RouteTableId: !Ref PublicRouteTable
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PubSub01RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PubSub01
PubSub02RouteTableAssociation:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref PublicRouteTable
SubnetId: !Ref PubSub02
NatGateway01EIP:
Type: AWS::EC2::EIP
Condition: isRequired
DependsOn: InternetGatewayAttachment
Properties:
Domain: vpc
NatGateway01:
Type: AWS::EC2::NatGateway
Condition: isRequired
Properties:
AllocationId: !GetAtt NatGateway01EIP.AllocationId
SubnetId: !Ref PubSub01
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", NatGateway01]]
- Key: Env
Value: !Ref Environment
NatGateway02EIP:
Type: AWS::EC2::EIP
Condition: isRequired
DependsOn: InternetGatewayAttachment
Properties:
Domain: vpc
NatGateway02:
Type: AWS::EC2::NatGateway
Condition: isRequired
Properties:
AllocationId: !GetAtt NatGateway02EIP.AllocationId
SubnetId: !Ref PubSub02
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", NatGateway02]]
- Key: Env
Value: !Ref Environment
PrivateRouteTable01:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PvtRTable01]]
- Key: Env
Value: !Ref Environment
DefaultPrivateRoute01:
Type: AWS::EC2::Route
Condition: isRequired
Properties:
RouteTableId: !Ref PrivateRouteTable01
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway01
PvtSub01RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable01
SubnetId: !Ref PvtSub01
PrivateRouteTable02:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", PvtRTable02]]
- Key: Env
Value: !Ref Environment
DefaultPrivateRoute02:
Type: AWS::EC2::Route
Condition: isRequired
Properties:
RouteTableId: !Ref PrivateRouteTable02
DestinationCidrBlock: 0.0.0.0/0
NatGatewayId: !Ref NatGateway02
PvtSub02RouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
RouteTableId: !Ref PrivateRouteTable02
SubnetId: !Ref PvtSub02
ELBSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: ELBSG
GroupDescription: "Accepts traffic on port 80, 443 from anywhere."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", ELBSG]]
- Key: Env
Value: !Ref Environment
OpenWebSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: OpenWebSG
GroupDescription: "Accepts web and remote access traffic from anywhere."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 3389
ToPort: 3389
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", OpenWebSG]]
- Key: Env
Value: !Ref Environment
WinWebSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: WinWebSG
GroupDescription: "Accepts web traffic from ELBSG and remote access from Bastion host."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref ELBSG
- IpProtocol: tcp
FromPort: 443
ToPort: 443
SourceSecurityGroupId: !Ref ELBSG
- IpProtocol: tcp
FromPort: 3389
ToPort: 3389
SourceSecurityGroupId: !Ref WinBHSG
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", WinWebSG]]
- Key: Env
Value: !Ref Environment
LnxWebSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: LnxWebSG
GroupDescription: "Accepts web traffic from ELBSG and remote access from Bastion host."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
SourceSecurityGroupId: !Ref ELBSG
- IpProtocol: tcp
FromPort: 443
ToPort: 443
SourceSecurityGroupId: !Ref ELBSG
- IpProtocol: tcp
FromPort: 22
ToPort: 22
SourceSecurityGroupId: !Ref WinBHSG
- IpProtocol: tcp
FromPort: 22
ToPort: 22
SourceSecurityGroupId: !Ref LnxBHSG
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", LnxWebSG]]
- Key: Env
Value: !Ref Environment
WinBHSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: WinBHSG
GroupDescription: "Accepts RDP traffic from anywhere."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3389
ToPort: 3389
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", WinBHSG]]
- Key: Env
Value: !Ref Environment
LnxBHSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: LnxBHSG
GroupDescription: "Accepts RDP traffic from anywhere."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", LnxBHSG]]
- Key: Env
Value: !Ref Environment
DBSG:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupName: DBSG
GroupDescription: "Accepts DB traffic from WebSG."
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref LnxWebSG
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", DBSG]]
- Key: Env
Value: !Ref Environment
DBSubnetGroup:
Type: "AWS::RDS::DBSubnetGroup"
Properties:
DBSubnetGroupDescription: description
SubnetIds:
- !Ref PvtSub01
- !Ref PvtSub02
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", DBSubnetGroup]]
- Key: Env
Value: !Ref Environment
SSMInstanceRole:
Type: AWS::IAM::Role
Properties:
Policies:
- PolicyDocument:
Version: "2012-10-17"
Statement:
- Action:
- s3:GetObject
Resource:
- !Sub "arn:aws:s3:::aws-ssm-${AWS::Region}/*"
- !Sub "arn:aws:s3:::aws-windows-downloads-${AWS::Region}/*"
- !Sub "arn:aws:s3:::amazon-ssm-${AWS::Region}/*"
- !Sub "arn:aws:s3:::amazon-ssm-packages-${AWS::Region}/*"
- !Sub "arn:aws:s3:::${AWS::Region}-birdwatcher-prod/*"
- !Sub "arn:aws:s3:::patch-baseline-snapshot-${AWS::Region}/*"
Effect: Allow
PolicyName: ssm-custom-s3-policy
Path: /
ManagedPolicyArns:
- !Sub "arn:${AWS::Partition}:iam::aws:policy/AmazonSSMManagedInstanceCore"
- !Sub "arn:${AWS::Partition}:iam::aws:policy/CloudWatchAgentServerPolicy"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "ec2.amazonaws.com"
- "ssm.amazonaws.com"
Action: "sts:AssumeRole"
SSMInstanceProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles:
- !Ref SSMInstanceRole
SsmAssociation:
# CloudFormation Resource Type that creates State Manager Associations
Type: AWS::SSM::Association
Properties:
# Command Document that this Association will run
Name: AWS-RunShellScript
WaitForSuccessTimeoutSeconds: 300
# Targeting Instance by Tags
Targets:
- Key: tag:Env
Values:
- "DEV"
# Parameters for the AWS-RunShellScript, in this case commands to install sample app
Parameters:
commands:
- |
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
cd /var/www/html
sudo wget https://raw.githubusercontent.com/nvaws/labs/main/index.txt
INSTANCEID=`curl http://169.254.169.254/latest/meta-data/instance-id`
INSTANCETYPE=`curl http://169.254.169.254/latest/meta-data/instance-type`
PRIVATEIP=`curl http://169.254.169.254/latest/meta-data/local-ipv4`
sudo sed -e "s/INSTANCEID/$INSTANCEID/" -e "s/INSTANCETYPE/$INSTANCETYPE/" -e "s/PRIVATEIP/$PRIVATEIP/" index.txt > index.html
MyTG:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Name: MyTG
Port: 80
Protocol: HTTP
VpcId: !Ref VPC
LaunchConfiguration:
Type: "AWS::AutoScaling::LaunchConfiguration"
Properties:
ImageId: !Ref LatestAmazonLinux2
InstanceType: !FindInMap [InstanceSize, !Ref Environment, EC2]
IamInstanceProfile: !Ref SSMInstanceProfile
KeyName: !Ref "KeyPair"
SecurityGroups:
- !Ref LnxWebSG
UserData:
"Fn::Base64":
!Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource AutoScalingGroup --region ${AWS::Region}
yum update -y
AutoScalingGroup:
Type: "AWS::AutoScaling::AutoScalingGroup"
CreationPolicy:
ResourceSignal:
Count: !Ref DesiredCapacity
UpdatePolicy:
AutoScalingReplacingUpdate:
WillReplace: true
Properties:
Cooldown: "300"
DesiredCapacity: !Ref DesiredCapacity
LaunchConfigurationName: !Ref LaunchConfiguration
HealthCheckGracePeriod: "300"
HealthCheckType: ELB
MaxSize: !Ref MaxSize
MinSize: !Ref MinSize
TargetGroupARNs:
- !Ref MyTG
VPCZoneIdentifier:
- !Ref PvtSub01
- !Ref PvtSub02
Tags:
- Key: Name
PropagateAtLaunch: true
Value: !Join [_, [!Ref "AWS::StackName", WebServer]]
- Key: Env
PropagateAtLaunch: true
Value: !Ref Environment
AutoScalingGroupScalingPolicy:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AutoScalingGroupName: !Ref AutoScalingGroup
PolicyType: TargetTrackingScaling
TargetTrackingConfiguration:
PredefinedMetricSpecification:
PredefinedMetricType: ASGAverageCPUUtilization
TargetValue: "60"
MyALBListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
DefaultActions:
- TargetGroupArn: !Ref MyTG
Type: forward
LoadBalancerArn: !Ref ALB
Port: 80
Protocol: HTTP
ALB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: MyALB
Scheme: internet-facing
SecurityGroups:
- !Ref ELBSG
Subnets:
- !Ref PubSub01
- !Ref PubSub02
Tags:
- Key: Name
Value: !Join [_, [!Ref "AWS::StackName", ALB]]
- Key: Env
Value: !Ref Environment
Outputs:
VPC:
Description: "A reference to the created VPC"
Value: !Ref VPC
Export:
Name: !Sub "${AWS::StackName}-VPC"
PubSub01:
Description: "A reference to the public subnet in the 1st Availability Zone"
Value: !Ref PubSub01
Export:
Name: !Sub "${AWS::StackName}-PubSub01"
PubSub02:
Description: "A reference to the public subnet in the 2nd Availability Zone"
Value: !Ref PubSub02
Export:
Name: !Sub "${AWS::StackName}-PubSub02"
PvtSub01:
Description: "A reference to the private subnet in the 1st Availability Zone"
Value: !Ref PvtSub01
Export:
Name: !Sub "${AWS::StackName}-PvtSub01"
PvtSub02:
Description: "A reference to the private subnet in the 2nd Availability Zone"
Value: !Ref PvtSub02
Export:
Name: !Sub "${AWS::StackName}-PvtSub02"
ELBSG:
Description: "Security group for the Load Balancer"
Value: !Ref ELBSG
Export:
Name: !Sub "${AWS::StackName}-ELBSG"
WinBHSG:
Description: "Security group for the Bastion Host"
Value: !Ref WinBHSG
Export:
Name: !Sub "${AWS::StackName}-WinBHSG"
WinWebSG:
Description: "Security group for the Windows instance"
Value: !Ref WinWebSG
Export:
Name: !Sub "${AWS::StackName}-WinWebSG"
LnxWebSG:
Description: "Security group for the Linux instance"
Value: !Ref LnxWebSG
Export:
Name: !Sub "${AWS::StackName}-LnxWebSG"
ALBDNSName:
Description: The DNSName of the application load balancer
Value: !GetAtt ALB.DNSName