-
Notifications
You must be signed in to change notification settings - Fork 2
/
application.template
executable file
·1118 lines (1081 loc) · 42.7 KB
/
application.template
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
AWSTemplateFormatVersion: 2010-09-09
Description: Enterprise Accelerator - Provides nesting for required stacks to deploy a full sample web application with reverse proxy, ELBs, IAM, and other resources (for demonstration/POC/testing) QS(5026)
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Region Config
Parameters:
- pRegionAZ1Name
- pRegionAZ2Name
- Label:
default: Network (existing VPC config)
Parameters:
- pProductionCIDR
- pProductionVPC
- pDMZSubnetA
- pDMZSubnetB
- pAppPrivateSubnetA
- pAppPrivateSubnetB
- pDBPrivateSubnetA
- pDBPrivateSubnetB
- Label:
default: Application Configuration
Parameters:
- pWebInstanceType
- pWebServerAMI
- pAppInstanceType
- pAppAmi
- Label:
default: Database Configuration
Parameters:
- pDBEngine
- pDBName
- pDBUser
- pDBPassword
- pDBClass
- pDBAllocatedStorage
- Label:
default: AWS Quick Start Configuration
Parameters:
- QuickStartS3URL
- QSS3BucketName
- QSS3KeyPrefix
- QSS3SSLKeyPrefix
Stack:
Value: 3
VersionDate:
Value: 20160510
Identifier:
Value: template-application
Input:
Description: VPC, SubnetIDs, S3 bucket names, CIDR blocks, KeyNames, AMIs, DB name and password
Output:
Description: Outputs ID of all deployed resources
Parameters:
pSecurityAlarmTopic:
Description: SNS topic for alarms and notifications
Type: String
Default: ''
pEC2KeyPair:
Description: Key Name for Instance
Type: String
Default: ''
pProductionCIDR:
Description: Production VPC CIDR
Type: String
pManagementCIDR:
Description: Management VPC CIDR
Type: String
pProductionVPC:
Description: Production VPC
Type: AWS::EC2::VPC::Id
pBastionSSHCIDR:
Description: CIDR block (optional) of Public IPs allowed to access Bastion instance in this deployment
Type: String
Default: 0.0.0.0/0
pDMZSubnetA:
Description: DMZ Subnet A
Type: AWS::EC2::Subnet::Id
pDMZSubnetB:
Description: DMZ Subnet B
Type: AWS::EC2::Subnet::Id
pAppPrivateSubnetA:
Description: WebApp Subnet A
Type: AWS::EC2::Subnet::Id
pAppPrivateSubnetB:
Description: WebApp Subnet A
Type: AWS::EC2::Subnet::Id
pWebInstanceType:
Description: Instance type for the webservers
Type: String
pAppInstanceType:
Description: Instance type for the app webservers
Type: String
pDBPrivateSubnetA:
Description: rDBPrivateSubnetA
Type: AWS::EC2::Subnet::Id
pDBPrivateSubnetB:
Description: rDBPrivateSubnetB
Type: AWS::EC2::Subnet::Id
pRegionAZ1Name:
Description: rDBPrivateSubnetB
Type: AWS::EC2::AvailabilityZone::Name
pRegionAZ2Name:
Description: rDBPrivateSubnetB
Type: AWS::EC2::AvailabilityZone::Name
pWebServerAMI:
Description: Which webserver AMI do you want to use, default
Type: String
Default: none
pAppAmi:
Description: Which App AMI do you want to use?
Type: String
Default: none
pDBEngine:
Description: Database engine of RDS Database
Type: String
pDBName:
Description: Name of RDS Database
Type: String
pDBUser:
Description: Username of DB Instance
Type: String
pDBPassword:
Description: Password of DB Instance
NoEcho: true
Type: String
pDBClass:
Description: Instance class of RDS instance
Type: String
pDBAllocatedStorage:
Description: Allocated Storage (in GB) for RDS instance
Type: String
pEnvironment:
Description: Environment type (development, test, or production)
Type: String
Default: development
pSupportsGlacier:
Description: Determines whether this region supports Glacier (passed in from main template)
Type: String
Default: true
QSS3BucketName:
AllowedPattern: ^[0-9a-zA-Z]+([0-9a-zA-Z-.]*[0-9a-zA-Z])*$
ConstraintDescription: Quick Start bucket name can include numbers, lowercase letters, uppercase letters, periods (.), and hyphens (-). It cannot start or end with a hyphen (-).
Default: quickstart-reference
Description: S3 bucket name for the Quick Start assets. Quick Start bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).
Type: String
QSS3KeyPrefix:
AllowedPattern: ^[0-9a-zA-Z-]+(/[0-9a-zA-Z-]+)*$
ConstraintDescription: Quick Start key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/). It cannot start or end with forward slash (/) because they are automatically appended.
Default: enterprise-accelerator/hipaa/latest
Description: S3 key prefix for the Quick Start assets. Quick Start key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/). It cannot start or end with forward slash (/) because they are automatically appended.
Type: String
QSS3SSLKeyPrefix:
AllowedPattern: ^[0-9a-zA-Z-]+(/[0-9a-zA-Z-]+)*$
ConstraintDescription: Quick Start SSL key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/). It cannot start or end with forward slash (/) because they are automatically appended.
Default: enterprise-accelerator/hipaa/latest
Description: S3 key prefix for the Quick Start assets. Quick Start key prefix can include numbers, lowercase letters, uppercase letters, hyphens (-), and forward slash (/). It cannot start or end with forward slash (/) because they are automatically appended.
Type: String
QuickStartS3URL:
Description: Quick Start S3 URL prefix.
Default: https://s3.amazonaws.com
Type: String
Mappings:
elbMap:
us-east-1:
ELB: '127311923021'
us-east-2:
ELB: '033677994240'
us-west-1:
ELB: '027434742980'
us-west-2:
ELB: '797873946194'
ca-central-1:
ELB: '985666609251'
eu-west-1:
ELB: '156460612806'
eu-west-2:
ELB: '652711504416'
eu-central-1:
ELB: '054676820928'
ap-south-1:
ELB: '718504428378'
ap-southeast-2:
ELB: '783225319266'
ap-southeast-1:
ELB: '114774131450'
ap-northeast-1:
ELB: '582318560864'
ap-northeast-2:
ELB: '600734575887'
sa-east-1:
ELB: '507241528517'
us-gov-west-1:
ELB: '048591011584'
Conditions:
IsGovCloud: !Equals [ !Ref 'AWS::Region', us-gov-west-1 ]
SupportsGlacier: !Equals [ !Ref pSupportsGlacier, true ]
Resources:
rS3ELBAccessLogs:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
AccessControl: Private
rS3AccessLogsPolicy:
Type: AWS::S3::BucketPolicy
DeletionPolicy: Retain
Properties:
Bucket: !Ref rS3ELBAccessLogs
PolicyDocument:
Version: 2008-10-17
Statement:
- Sid: ELBAccessLogs20130930
Effect: Allow
Resource: !Sub
- arn:${Endpoint}:s3:::${rS3ELBAccessLogs}/Logs/AWSLogs/${AWS::AccountId}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
Principal:
AWS: !FindInMap [ elbMap, !Ref 'AWS::Region', ELB ]
Action:
- s3:PutObject
rSecurityGroupWeb:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for Reverse Proxy in DMZ
VpcId: !Ref pProductionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
SecurityGroupEgress:
- 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: sg-reverse-proxy-dmz
- Key: Environment
Value: !Ref pEnvironment
rSecurityGroupWebInstance:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for Reverse Proxy Instances in DMZ
VpcId: !Ref pProductionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref pProductionCIDR
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref pProductionCIDR
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref pManagementCIDR
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 123
ToPort: 123
CidrIp: !Ref pProductionCIDR
Tags:
- Key: Name
Value: sg-reverse-proxy-dmz-instances
- Key: Environment
Value: !Ref pEnvironment
rSecurityGroupApp:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for Appservers ELB
VpcId: !Ref pProductionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref pProductionCIDR
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: sg-app-server-elb
- Key: Environment
Value: !Ref pEnvironment
rSecurityGroupAppInstance:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Security group for Appserver Instances
VpcId: !Ref pProductionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: !Ref pProductionCIDR
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: !Ref pProductionCIDR
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: !Ref pManagementCIDR
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 443
ToPort: 443
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 123
ToPort: 123
CidrIp: !Ref pProductionCIDR
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
CidrIp: !Ref pProductionCIDR
Tags:
- Key: Name
Value: sg-app-server-elb-instances
- Key: Environment
Value: !Ref pEnvironment
rSecurityGroupRDS:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Port 3306 database for access
VpcId: !Ref pProductionVPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref rSecurityGroupAppInstance
Tags:
- Key: Name
Value: sg-database-access
- Key: Environment
Value: !Ref pEnvironment
rWebContentBucket:
Type: AWS::S3::Bucket
Properties:
AccessControl: Private
LifecycleConfiguration:
Rules:
- Id: Transition90daysRetain7yrs
Status: Enabled
ExpirationInDays: 2555
Transition:
TransitionInDays: 90
StorageClass: !If [ SupportsGlacier, GLACIER, STANDARD_IA ]
VersioningConfiguration:
Status: Enabled
DeletionPolicy: Delete
rWebContentS3Policy:
Type: AWS::S3::BucketPolicy
DependsOn: rWebContentBucket
Properties:
Bucket: !Ref rWebContentBucket
PolicyDocument:
Statement:
- Sid: EnforceSecureTransport
Action: s3:*
Effect: Deny
Principal: '*'
Resource: !Sub
- arn:${Endpoint}:s3:::${rWebContentBucket}
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
Bool:
aws:SecureTransport: 'false'
- Sid: EnforceEncryptionOnPut
Effect: Deny
Principal: '*'
Action: s3:PutObject
Resource: !Sub
- arn:${Endpoint}:s3:::${rWebContentBucket}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
Condition:
StringNotEquals:
s3:x-amz-server-side-encryption: AES256
rDBSubnetGroup:
Type: AWS::RDS::DBSubnetGroup
Properties:
DBSubnetGroupDescription: RDS Subnet Group
SubnetIds:
- !Ref pDBPrivateSubnetA
- !Ref pDBPrivateSubnetB
rRDSInstance:
Type: AWS::RDS::DBInstance
DependsOn:
- rDBSubnetGroup
- rSecurityGroupRDS
Properties:
DBInstanceIdentifier:
!Sub
- ${Environment}-dbinstance
- Environment: !Ref pEnvironment
DBName: !Ref pDBName
Engine: !Ref pDBEngine
MultiAZ: true
StorageEncrypted: true
MasterUsername: !Ref pDBUser
MasterUserPassword: !Ref pDBPassword
DBInstanceClass: !Ref pDBClass
AllocatedStorage: !Ref pDBAllocatedStorage
VPCSecurityGroups:
- !Ref rSecurityGroupRDS
DBSubnetGroupName: !Ref rDBSubnetGroup
rELBApp:
Type: AWS::ElasticLoadBalancing::LoadBalancer
DependsOn:
- rS3ELBAccessLogs
- rSecurityGroupApp
- rS3AccessLogsPolicy
Properties:
Subnets:
- !Ref pAppPrivateSubnetA
- !Ref pAppPrivateSubnetB
HealthCheck:
HealthyThreshold: 2
Interval: 15
Target: TCP:443
Timeout: 5
UnhealthyThreshold: 3
AccessLoggingPolicy:
S3BucketName: !Ref rS3ELBAccessLogs
S3BucketPrefix: Logs
Enabled: true
EmitInterval: 60
SecurityGroups:
- !Ref rSecurityGroupApp
Listeners:
- InstancePort: 80
LoadBalancerPort: 80
Protocol: TCP
InstanceProtocol: TCP
- InstancePort: 443
LoadBalancerPort: 443
Protocol: TCP
InstanceProtocol: TCP
Scheme: internal
Tags:
- Key: Name
Value: ProxyELB
- Key: Environment
Value: !Ref pEnvironment
rELBWeb:
Type: AWS::ElasticLoadBalancing::LoadBalancer
DependsOn:
- rS3ELBAccessLogs
- rSecurityGroupWeb
- rS3AccessLogsPolicy
Properties:
Subnets:
- !Ref pDMZSubnetA
- !Ref pDMZSubnetB
HealthCheck:
HealthyThreshold: 2
Interval: 30
Target: TCP:443
Timeout: 5
UnhealthyThreshold: 5
AccessLoggingPolicy:
S3BucketName: !Ref rS3ELBAccessLogs
S3BucketPrefix: Logs
Enabled: true
EmitInterval: 60
SecurityGroups:
- !Ref rSecurityGroupWeb
Listeners:
- InstancePort: 80
LoadBalancerPort: 80
Protocol: TCP
InstanceProtocol: TCP
- InstancePort: 443
LoadBalancerPort: 443
Protocol: TCP
InstanceProtocol: TCP
Tags:
- Key: Name
Value: Proxy ELB
- Key: Environment
Value: !Ref pEnvironment
rWebInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: S3Assets
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: GetServerCertificate
Effect: Allow
Action:
- s3:Get*
- s3:List*
- s3:Head*
Resource:
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}/${QSS3KeyPrefix}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}/${QSS3SSLKeyPrefix}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: DescribeVolumes
Effect: Allow
Action:
- ec2:DescribeVolumes
Resource: '*'
rWebInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref rWebInstanceRole
rAutoScalingConfigWeb:
Type: AWS::AutoScaling::LaunchConfiguration
DependsOn:
- rELBApp
- rAutoScalingGroupApp
Metadata:
AWS::CloudFormation::Init:
config:
packages:
yum:
nginx: []
java-1.6.0-openjdk-devel: []
git: []
files:
/etc/nginx/snippets/self-signed.conf:
content: |
ssl_certificate /etc/ssl/certs/common.crt;
ssl_certificate_key /etc/ssl/private/common.key;
/tmp/nginx/default.conf:
content: !Sub |
server {
listen 80;
charset utf-8;
location / {
resolver xxxxx;
set $elb 'https://${rELBApp.DNSName}';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass $elb;
}
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
location / {
resolver xxxxx;
set $elb 'https://${rELBApp.DNSName}';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass $elb;
}
include snippets/self-signed.conf;
}
mode: '000755'
owner: root
group: root
commands:
01-nginx-setup:
command: |
#!/bin/bash
## Nginx setup
sleep 5
echo 'Replace resolver placeholder with /etc/resolv.conf nameservers'
sed -i "s/xxxxx/$(grep ^nameserver /etc/resolv.conf | sed 's/^nameserver//' | tr -d '\n')/" /tmp/nginx/default.conf
cp /tmp/nginx/default.conf /etc/nginx/conf.d/default.conf
service nginx stop
sed -i '/default_server;/d' /etc/nginx/nginx.conf
sleep 10
service nginx restart
services:
sysvinit:
nginx:
enabled: true
ensureRunning: true
files:
- /etc/nginx/conf.d/default.conf
Properties:
AssociatePublicIpAddress: true
ImageId: !Ref pWebServerAMI
IamInstanceProfile: !Ref rWebInstanceProfile
InstanceType: !Ref pWebInstanceType
BlockDeviceMappings:
- DeviceName: /dev/sdh
Ebs:
VolumeSize: 50
VolumeType: gp2
Encrypted: true
KeyName: !Ref pEC2KeyPair
SecurityGroups:
- !Ref rSecurityGroupWebInstance
UserData: !Base64
'Fn::Sub': |
#!/bin/bash
yum update -y
aws configure set default.s3.signature_version s3v4
aws s3 cp s3://${QSS3BucketName}/${QSS3SSLKeyPrefix}/common.crt /etc/ssl/certs/common.crt --region ${AWS::Region}
aws s3 cp s3://${QSS3BucketName}/${QSS3SSLKeyPrefix}/common.key /etc/ssl/private/common.key --region ${AWS::Region}
EC2_INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id)
######################################################################
# Volume /dev/sdh (which will get created as /dev/xvdh on Amazon Linux)
DATA_STATE="unknown"
until [ "${!DATA_STATE}" == "attached" ]; do
DATA_STATE=$(aws ec2 describe-volumes \
--region ${AWS::Region} \
--filters \
Name=attachment.instance-id,Values=${!EC2_INSTANCE_ID} \
Name=attachment.device,Values=/dev/sdh \
--query Volumes[].Attachments[].State \
--output text)
sleep 5
done
# Format /dev/xvdh if it does not contain a partition yet
if [ "$(file -b -s /dev/xvdh)" == "data" ]; then
mkfs -t ext4 /dev/xvdh
fi
mkdir -p /data
mount /dev/xvdh /data
# Persist the volume in /etc/fstab so it gets mounted again
echo '/dev/xvdh /data ext4 defaults,nofail 0 2' >> /etc/fstab
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource rAutoScalingConfigWeb --region ${AWS::Region}
## Nginx setup
sleep 5
cp /tmp/nginx/default.conf /etc/nginx/conf.d/default.conf
service nginx stop
sed -i '/default_server;/d' /etc/nginx/nginx.conf
sleep 10
service nginx restart
rAutoScalingGroupWeb:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: rAutoScalingConfigWeb
Properties:
AvailabilityZones:
- !Ref pRegionAZ1Name
- !Ref pRegionAZ2Name
VPCZoneIdentifier:
- !Ref pDMZSubnetA
- !Ref pDMZSubnetB
LaunchConfigurationName: !Ref rAutoScalingConfigWeb
MinSize: 1
MaxSize: 1
LoadBalancerNames:
- !Ref rELBWeb
HealthCheckType: ELB
HealthCheckGracePeriod: 300
Tags:
- Key: Name
Value: Proxy Server
PropagateAtLaunch: true
- Key: Environment
Value: !Ref pEnvironment
PropagateAtLaunch: true
rAutoScalingUpWeb:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref rAutoScalingGroupWeb
Cooldown: 500
ScalingAdjustment: 1
rAutoScalingDownWeb:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref rAutoScalingGroupWeb
Cooldown: 500
ScalingAdjustment: -1
rCWAlarmHighCPUWeb:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: 1
Statistic: Average
Threshold: 50
AlarmDescription: Alarm if CPU too high or metric disappears indicating instance is down
Period: 60
AlarmActions:
- !Ref rAutoScalingUpWeb
Namespace: AWS/EC2
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref rAutoScalingGroupWeb
ComparisonOperator: GreaterThanThreshold
MetricName: WebServerCpuHighUtilization
rCWAlarmLowCPUWeb:
Type: AWS::CloudWatch::Alarm
DependsOn: rAutoScalingGroupWeb
Properties:
EvaluationPeriods: 1
Statistic: Average
Threshold: 10
AlarmDescription: Alarm if CPU too low, remove a web server
Period: 60
AlarmActions:
- !Ref rAutoScalingDownWeb
Namespace: AWS/EC2
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref rAutoScalingGroupWeb
ComparisonOperator: LessThanThreshold
MetricName: WebServerCpuLowUtilization
rAppInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
Path: /
Policies:
- PolicyName: S3Assets
PolicyDocument:
Version: 2012-10-17
Statement:
- Sid: GetServerCertificate
Effect: Allow
Action:
- s3:Get*
- s3:List*
- s3:Head*
Resource:
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}/${QSS3KeyPrefix}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- !Sub
- arn:${Endpoint}:s3:::${QSS3BucketName}/${QSS3SSLKeyPrefix}/*
- { Endpoint: !If [ IsGovCloud, aws-us-gov, aws ] }
- Sid: DescribeVolumes
Effect: Allow
Action:
- ec2:DescribeVolumes
Resource: '*'
rAppInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref rAppInstanceRole
rAutoScalingConfigApp:
Type: AWS::AutoScaling::LaunchConfiguration
DependsOn:
- rRDSInstance
Metadata:
AWS::CloudFormation::Init:
configSets:
webserver_install:
- install_cfn
- install_codedeploy
- install_webserver
install_cfn:
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.rAutoScalingConfigApp.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource rAutoScalingGroupApp --configsets webserver_install --region ${AWS::Region}
mode: '000400'
owner: root
group: root
services:
sysvinit:
cfn-hup:
enabled: true
ensureRunning: true
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
install_codedeploy:
services:
sysvinit:
codedeploy-agent:
enabled: true
ensureRunning: true
install_webserver:
packages:
yum:
httpd: []
files:
/tmp/httpd/ssl.conf:
content: |
Listen 443
<VirtualHost *:443>
DocumentRoot /var/www/html
LoadModule ssl_module /etc/httpd/modules/mod_ssl.so
SSLEngine on
SSLCertificateFile /etc/ssl/certs/common.crt
SSLCertificateKeyFile /etc/ssl/private/common.key
</VirtualHost>
mode: '000755'
owner: root
group: root
services:
sysvinit:
httpd:
enabled: true
ensureRunning: true
Properties:
ImageId: !Ref pAppAmi
IamInstanceProfile: !Ref rAppInstanceProfile
InstanceType: !Ref pAppInstanceType
BlockDeviceMappings:
- DeviceName: /dev/sdh
Ebs:
VolumeSize: 50
VolumeType: gp2
Encrypted: true
KeyName: !Ref pEC2KeyPair
SecurityGroups:
- !Ref rSecurityGroupAppInstance
UserData: !Base64
'Fn::Sub': |
#!/bin/bash -x
sudo yum -y update
sudo yum -y install mod_ssl
# install codedeploy agent
sudo yum -y install ruby
sudo yum -y install wget
cd /home/ec2-user/
sudo wget https://aws-codedeploy-${AWS::Region}.s3.amazonaws.com/latest/install
sudo chmod +x ./install
sudo ./install auto
aws configure set default.s3.signature_version s3v4
aws s3 cp s3://${QSS3BucketName}/${QSS3SSLKeyPrefix}/common.crt /etc/ssl/certs/common.crt --region ${AWS::Region}
aws s3 cp s3://${QSS3BucketName}/${QSS3SSLKeyPrefix}/common.key /etc/ssl/private/common.key --region ${AWS::Region}
EC2_INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id)
######################################################################
# Volume /dev/sdh (which will get created as /dev/xvdh on Amazon Linux)
DATA_STATE="unknown"
until [ "${!DATA_STATE}" == "attached" ]; do
DATA_STATE=$(aws ec2 describe-volumes \
--region ${AWS::Region} \
--filters \
Name=attachment.instance-id,Values=${!EC2_INSTANCE_ID} \
Name=attachment.device,Values=/dev/sdh \
--query Volumes[].Attachments[].State \
--output text)
sleep 5
done
# Format /dev/xvdh if it does not contain a partition yet
if [ "$(file -b -s /dev/xvdh)" == "data" ]; then
mkfs -t ext4 /dev/xvdh
fi
mkdir -p /data
mount /dev/xvdh /data
# Persist the volume in /etc/fstab so it gets mounted again
echo '/dev/xvdh /data ext4 defaults,nofail 0 2' >> /etc/fstab
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource rAutoScalingConfigApp --configsets webserver_install --region ${AWS::Region}
cp /tmp/httpd/ssl.conf /etc/httpd/conf.d/ssl.conf
service httpd restart
#build a website url page
echo 'Hello World.' > /var/www/html/index.html
# signal the auto scaling to start
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource rAutoScalingGroupApp --region ${AWS::Region}
rAutoScalingGroupApp:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: rAutoScalingConfigApp
Properties:
AvailabilityZones:
- !Ref pRegionAZ1Name
- !Ref pRegionAZ2Name
VPCZoneIdentifier:
- !Ref pAppPrivateSubnetA
- !Ref pAppPrivateSubnetB
LaunchConfigurationName: !Ref rAutoScalingConfigApp
MinSize: 1
MaxSize: 1
LoadBalancerNames:
- !Ref rELBApp
HealthCheckType: ELB
HealthCheckGracePeriod: 300
Tags:
- Key: Name
Value: AppServer
PropagateAtLaunch: true
- Key: Environment
Value: !Ref pEnvironment
PropagateAtLaunch: true
CreationPolicy:
ResourceSignal:
Count: 1
Timeout: PT15M
rAutoScalingDownApp:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref rAutoScalingGroupApp
Cooldown: 1
ScalingAdjustment: 1
rAutoScalingUpApp:
Type: AWS::AutoScaling::ScalingPolicy
Properties:
AdjustmentType: ChangeInCapacity
AutoScalingGroupName: !Ref rAutoScalingGroupApp
Cooldown: 1
ScalingAdjustment: -1
rCWAlarmHighCPUApp:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: 1
Statistic: Average
Threshold: 50
AlarmDescription: Alarm if CPU too high or metric disappears indicating instance is down
Period: 60
AlarmActions:
- !Ref rAutoScalingDownApp
Namespace: AWS/EC2
Dimensions:
- Name: AutoScalingGroupName
Value: !Ref rAutoScalingGroupApp
ComparisonOperator: GreaterThanThreshold
MetricName: AppServerCpuHighUtilization
rCWAlarmLowCPUApp:
Type: AWS::CloudWatch::Alarm
Properties:
EvaluationPeriods: 1