forked from aws-samples/personal-game-server-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcCFNGamingServerSolution.YAML
1565 lines (1484 loc) · 57.7 KB
/
mcCFNGamingServerSolution.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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: 2010-09-09
Description: AWS CloudFormation Template to setup a cloud optimised Personal Gaming server.
Metadata:
'AWS::CloudFormation::Interface':
ParameterGroups:
- Label:
default: Personal Gaming Server Configuration
Parameters:
- GameServer
- InstanceType
- DiskSize
- DiskType
- GamingTCPTrafficPortStart
- GamingTCPTrafficPortEnd
- GamingUDPTrafficPortStart
- GamingUDPTrafficPortEnd
- Label:
default: AWS Configuration
Parameters:
- KeyName
- HostedZoneId
- Domain
- ShutdownTimeHours
- ShutdownTimeMins
- IdTagValue
- Label:
default: You should not need to change anything in this section.
Parameters:
- IdTagName
- LatestAMIIDx86
ParameterLabels:
GameServer:
default: Choose what game server you wish to install
InstanceType:
default: Select the server size (we recommend selecting a larger instance for initial install and then changing this to a smaller size later).
DiskSize:
default: How many Gibibytes would you like?
DiskType:
default: What do you want your hard-drive made from (GP3 is recommended)?
KeyName:
default: Select the key pair you will use to access server OS
Domain:
default: Enter the domain name you will use. Note this should be controlled by the Hosted Zone above (see setup instructions earlier)
HostedZoneId:
default: Enter the ARN for the Hosted Zone that contains your DNS records (see earlier step)
IdTagName:
default: Tag name that will be used to identify your gaming server(s)
IdTagValue:
default: Tag value that will be used to identify your gaming server(s)
ShutdownTimeHours:
default: The time of day to shutdown the server automatically in hours (NOTE GMT/UTC TIME!! - not local time zone)
ShutdownTimeMins:
default: The minutes past the hours specfied above to automatically shut down your server
LatestAMIIDx86:
default: The AMI to use. This is the machine image and includes at a minimum the OS for the server
GamingTCPTrafficPortStart:
default: Start of the port range that should be accessible from the internet for TCP traffic. Leave blank if you only want to allow UDP traffic to your server.
GamingTCPTrafficPortEnd:
default: End of the port range that should be accessible from the internet for TCP traffic.
GamingUDPTrafficPortStart:
default: Start of the port range that should be accessible from the internet for UDP traffic. Leave blank if you only want to allow TCP traffic to your server.
GamingUDPTrafficPortEnd:
default: End of the port range that should be accessible from the internet for UDP traffic.
Parameters:
GameServer:
Description: Please enter the URL for the shell script to install the game server of your choice - we call this the game cartridge URL. Please note this must be an uncompressed shell script to run on selected AMI (Ubuntu by default).
Type: String
AllowedPattern: https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
ConstraintDescription: 'Must be a valid URL.'
Default: https://raw.githubusercontent.com/Wondro/personal-game-server-manager/main/Bash/palworld.sh
KeyName:
Description: >-
This is required so you can access the server. If there is no option to
select one, please go back and create one before trying to run this
template again
AllowedPattern: .+
ConstraintDescription: >-
You must choose a keypair otherwise you won't be able to login to your
server's OS
Type: 'AWS::EC2::KeyPair::KeyName'
DiskSize:
Description: >-
Size of disk - you must choose a disk size of at least 8GiB (because this
is the size of the Linux AMI)
AllowedPattern: '^([8-9]|([1-9]\d{1,}))$'
ConstraintDescription: You must choose a disk size of at least 8 GiB
Default: '8'
Type: String
DiskType:
Description: Choose disk type. GP3 is recommended
AllowedPattern: .+
AllowedValues:
- gp3
- gp2
- standard
- IO2
Default: gp3
ConstraintDescription: You must choose a disk type
Type: String
InstanceType:
Description: 'Select the AWS EC2 instance type you want. You can change avaialble options later via the AWS Console - see blog entry for more details'
AllowedPattern: .+
ConstraintDescription: You must choose a server size
Type: String
AllowedValues:
- t3.medium
- t3.large
- t3.xlarge
LatestAMIIDx86:
Description: Defaults to the latest Ubuntu AMI ID. You can replace this if you would like to use a different AMI / Operating System.
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: /aws/service/canonical/ubuntu/server/20.04/stable/current/amd64/hvm/ebs-gp2/ami-id
GamingTCPTrafficPortStart:
Description: Enter port number for your server ingress security group. This will open your gaming server to TCP traffic to a range of ports with this as the lower boundary.
Type: String
AllowedPattern: ^(^$|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
ConstraintDescription: Must be a valid port number.
GamingTCPTrafficPortEnd:
Description: Enter port number for your server ingress security group. This will open your gaming server to TCP traffic to a range of ports with this as the upper boundary.
Type: String
AllowedPattern: ^(^$|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
ConstraintDescription: Must be a valid port number.
GamingUDPTrafficPortStart:
Description: Enter port number for your server ingress security group. This will open your gaming server to UDP traffic to a range of ports with this as the lower boundary.
Type: String
AllowedPattern: ^(^$|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
ConstraintDescription: Must be a valid port number.
Default: '8211'
GamingUDPTrafficPortEnd:
Description: Enter port number for your server ingress security group. This will open your gaming server to UDP traffic to a range of ports with this as the upper boundary.
Type: String
AllowedPattern: ^(^$|[0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$
ConstraintDescription: Must be a valid port number.
Default: '8211'
Domain:
Description: Domain name you will use to access your server (e.g. mygameserver.link). This is optional however without it you will need to configure a new IP address each stop/start
Type: String
HostedZoneId:
Description: Select the ID of your hosted zone - if there isn't one please go back to the section that describes setting up a domain name and hosted zone
Type: 'AWS::Route53::HostedZone::Id'
ConstraintDescription: >-
Although optional, it's required if you are using a custom domain
(recommended) so the DNS record for your domain can be updated
IdTagName:
Description: The name of the tag that will be used to identify your gaming server(s). DO NOT CHANGE THIS VALUE
AllowedPattern: mcServerFinder
Default: mcServerFinder
Type: String
ConstraintDescription: Please set this to "mcserverfinder" this is required so we can find your server(s) to control them
IdTagValue:
Description: The value of the tag that will be used to identify your gaming server(s). This can be useful if you want to control different servers independently - such as if you wanted to spin up a server to test out some features or mods without risking your main server - in this case you may want to change the tag value to something like mcDev or mcTest for example.
AllowedPattern: .+
Type: String
Default: mcServer
ConstraintDescription: This is a mandatory field - you must pust something in. We recommend not changing the default value.
ShutdownTimeHours:
Description: Setting a time here will automatically stop your server at the same time every day. You adjust the rule, add rules, changes days etc. later directly in the AWS EventBridge Console. IMPORTANT NOTE - the time must be set in GMT not your local time zone (also note therefore this time will appear to change when going from summer time to winter for relevant time zones). Leaving blank will not set an auto server shut down rule.
Type: String
AllowedValues:
- 'None - do not auto shut-down'
- '23'
- '22'
- '21'
- '20'
- '19'
- '18'
- '17'
- '16'
- '15'
- '14'
- '13'
- '12'
- '11'
- '10'
- '9'
- '8'
- '7'
- '6'
- '5'
- '4'
- '3'
- '2'
- '1'
- '0'
ShutdownTimeMins:
Description: Minutes past the hours to auto shut down your server each day
Type: String
AllowedValues:
- 'Not Applicable'
- '0'
- '5'
- '10'
- '15'
- '20'
- '25'
- '30'
- '35'
- '40'
- '45'
- '50'
- '55'
Conditions:
ShutdownTimeHoursIsNull: !Equals
- !Ref ShutdownTimeHours
- 'None - do not auto shut-down'
ShutdownTimeMinsIsNull: !Equals
- !Ref ShutdownTimeMins
- 'None'
noSecIngress: !And
- !Condition hasTCP
- !Condition hasUDP
hasTCP: !Not
- !Equals
- !Ref GamingTCPTrafficPortStart
- ''
hasUDP: !Not
- !Equals
- !Ref GamingUDPTrafficPortStart
- ''
Resources:
mcEC2Instance:
Type: 'AWS::EC2::Instance'
DependsOn: mcIgAttachment
Properties:
SecurityGroupIds:
- !Ref mcSecurityGroup
InstanceType: !Ref InstanceType
CreditSpecification:
CPUCredits: Standard
SubnetId: !Ref mcSubnet
IamInstanceProfile: !Ref mcInstanceProfile
KeyName: !Ref KeyName
Tags:
- Key: Name
Value: Cloudformation MC server
- Key: !Ref IdTagName
Value: !Ref IdTagValue
- Key: domain
Value: !Ref Domain
- Key: hostedZoneId
Value: !Ref HostedZoneId
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
DeleteOnTermination: 'true'
Encrypted: 'false'
VolumeSize: !Ref DiskSize
VolumeType: !Ref DiskType
ImageId: !Ref LatestAMIIDx86
UserData:
Fn::Base64: !Sub
- |
#!/bin/bash -xe
apt update
apt upgrade -y
apt install -y python-setuptools
mkdir -p /opt/aws/bin
wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-py3-latest.tar.gz
python3 -m easy_install --script-dir /opt/aws/bin aws-cfn-bootstrap-py3-latest.tar.gz
ln -s /root/aws-cfn-bootstrap-latest/init/ubuntu/cfn-hup /etc/init.d/cfn-hup
echo ${AWS::StackName} > /tmp/mcParamName.txt
mkdir -p /usr/games/install
cd /usr/games/install
wget -O gameserverinstall.sh ${GameServer}
chmod +x gameserverinstall.sh
./gameserverinstall.sh
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource mcC2Instance --region ${AWS::Region}
- GameServer: !Ref GameServer
mcPwordParameter:
Type: AWS::SSM::Parameter
Properties:
DataType: text
Description: Parameter to store temp password randomly generated on initial stack creation
Name: !Sub mcPalworldPW-${AWS::StackName}
Tier: Standard
Type: String
Value: tempValue
mcInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /service-role/
Roles:
- !Ref mcGamingServerIamRole
mcGamingServerIamRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: ec2.amazonaws.com
Action: 'sts:AssumeRole'
Description: IAM role to allow gaming EC2 server to update parameter with generated app password
Path: /service-role/
Policies:
- PolicyName: mcPersonalGamingServerIamRole
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "ssm:PutParameter"
Resource: !Sub
- arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${paramName}
- paramName: !Ref mcPwordParameter
mcControlApi:
Type: 'AWS::ApiGateway::RestApi'
Properties:
Description: "Primary API to control gaming server"
Name: !Sub mcControlApi-${mcVPC}
EndpointConfiguration:
Types:
- REGIONAL
FailOnWarnings: true
mcCognitoAuthoriser:
Type: 'AWS::ApiGateway::Authorizer'
Properties:
IdentitySource: method.request.header.Authorization
Name: mcCognitoAuthoriser
ProviderARNs:
- !GetAtt mcCognitoUserPool.Arn
RestApiId: !Ref mcControlApi
Type: COGNITO_USER_POOLS
mcControlApiDeployment:
DependsOn: mcApiGetInfoGetMethod
Type: 'AWS::ApiGateway::Deployment'
Properties:
RestApiId: !Ref mcControlApi
Description: Deployment of gaming control REST API
StageName: prod
mcControlApiResGetInfo:
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt
- mcControlApi
- RootResourceId
PathPart: getinfo
RestApiId: !Ref mcControlApi
mcControlApiResResize:
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt
- mcControlApi
- RootResourceId
PathPart: resize
RestApiId: !Ref mcControlApi
mcControlApiResStart:
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt
- mcControlApi
- RootResourceId
PathPart: start
RestApiId: !Ref mcControlApi
mcControlApiResStop:
Type: 'AWS::ApiGateway::Resource'
Properties:
ParentId: !GetAtt
- mcControlApi
- RootResourceId
PathPart: stop
RestApiId: !Ref mcControlApi
mcApiGetInfoGetMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: !Ref mcCognitoAuthoriser
HttpMethod: GET
Integration:
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : "'*'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json : "{\r\n \"command\": \"getInfo\",\r\n \"mcTagName\": \"$input.params('mctagname')\",\r\n \"mcTagValue\": \"$input.params('mctagvalue')\"\r\n}"
TimeoutInMillis: 15000
Type: AWS
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt mcStartStopLambda.Arn
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : false
OperationName: GetInfo
RequestParameters:
method.request.querystring.mctagname : false
method.request.querystring.mctagvalue : false
ResourceId: !Ref mcControlApiResGetInfo
RestApiId: !Ref mcControlApi
mcApiGetInfoOptionsMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin : "'*'"
method.response.header.Access-Control-Allow-Credentials : "'true'"
method.response.header.Access-Control-Allow-Methods : "'GET,OPTIONS'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json : '{"statusCode": 200}'
TimeoutInMillis: 15000
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : false
method.response.header.Access-Control-Allow-Origin : false
method.response.header.Access-Control-Allow-Credentials : false
method.response.header.Access-Control-Allow-Methods : false
OperationName: GetInfoOPTIONS
ResourceId: !Ref mcControlApiResGetInfo
RestApiId: !Ref mcControlApi
mcApiStartGetMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: !Ref mcCognitoAuthoriser
HttpMethod: GET
Integration:
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : "'*'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json : "{\r\n \"command\": \"start\",\r\n \"mcTagName\": \"$input.params('mctagname')\",\r\n \"mcTagValue\": \"$input.params('mctagvalue')\"\r\n}"
TimeoutInMillis: 15000
Type: AWS
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt mcStartStopLambda.Arn
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : false
OperationName: Start
RequestParameters:
method.request.querystring.mctagname : false
method.request.querystring.mctagvalue : false
ResourceId: !Ref mcControlApiResStart
RestApiId: !Ref mcControlApi
mcApiStartOptionsMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin : "'*'"
method.response.header.Access-Control-Allow-Credentials : "'true'"
method.response.header.Access-Control-Allow-Methods : "'GET,OPTIONS'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json : '{"statusCode": 200}'
TimeoutInMillis: 15000
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : false
method.response.header.Access-Control-Allow-Origin : false
method.response.header.Access-Control-Allow-Credentials : false
method.response.header.Access-Control-Allow-Methods : false
OperationName: StartOPTIONS
ResourceId: !Ref mcControlApiResStart
RestApiId: !Ref mcControlApi
mcApiStopGetMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: !Ref mcCognitoAuthoriser
HttpMethod: GET
Integration:
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : "'*'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json : "{\r\n \"command\": \"stop\",\r\n \"mcTagName\": \"$input.params('mctagname')\",\r\n \"mcTagValue\": \"$input.params('mctagvalue')\"\r\n}"
TimeoutInMillis: 15000
Type: AWS
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt mcStartStopLambda.Arn
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : false
OperationName: Stop
RequestParameters:
method.request.querystring.mctagname : false
method.request.querystring.mctagvalue : false
ResourceId: !Ref mcControlApiResStop
RestApiId: !Ref mcControlApi
mcApiStopOptionsMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin : "'*'"
method.response.header.Access-Control-Allow-Credentials : "'true'"
method.response.header.Access-Control-Allow-Methods : "'GET,OPTIONS'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json : '{"statusCode": 200}'
TimeoutInMillis: 15000
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : false
method.response.header.Access-Control-Allow-Origin : false
method.response.header.Access-Control-Allow-Credentials : false
method.response.header.Access-Control-Allow-Methods : false
OperationName: StopOPTIONS
ResourceId: !Ref mcControlApiResStop
RestApiId: !Ref mcControlApi
mcApiResizeGetMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: COGNITO_USER_POOLS
AuthorizerId: !Ref mcCognitoAuthoriser
HttpMethod: GET
Integration:
IntegrationHttpMethod: POST
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : "'*'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_TEMPLATES
RequestTemplates:
application/json : "{\r\n \"command\": \"reSize\",\r\n \"mcTagName\": \"$input.params('mctagname')\",\r\n \"mcTagValue\": \"$input.params('mctagvalue')\",\r\n \"reSizeType\": \"$input.params('resize')\"\r\n}"
TimeoutInMillis: 15000
Type: AWS
Uri: !Sub
- arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${lambdaArn}/invocations
- lambdaArn: !GetAtt mcStartStopLambda.Arn
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Origin : false
OperationName: Resize
RequestParameters:
method.request.querystring.mctagname : false
method.request.querystring.mctagvalue : false
method.request.querystring.resize : false
ResourceId: !Ref mcControlApiResResize
RestApiId: !Ref mcControlApi
mcApiResizeOptionsMethod:
Type: 'AWS::ApiGateway::Method'
Properties:
ApiKeyRequired: false
AuthorizationType: NONE
HttpMethod: OPTIONS
Integration:
IntegrationResponses:
- StatusCode: 200
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin : "'*'"
method.response.header.Access-Control-Allow-Credentials : "'true'"
method.response.header.Access-Control-Allow-Methods : "'GET,OPTIONS'"
ResponseTemplates:
application/json : ''
PassthroughBehavior: WHEN_NO_MATCH
RequestTemplates:
application/json : '{"statusCode": 200}'
TimeoutInMillis: 15000
Type: MOCK
MethodResponses:
- StatusCode: 200
ResponseModels:
application/json : 'Empty'
ResponseParameters:
method.response.header.Access-Control-Allow-Headers : false
method.response.header.Access-Control-Allow-Origin : false
method.response.header.Access-Control-Allow-Credentials : false
method.response.header.Access-Control-Allow-Methods : false
OperationName: ResizeOPTIONS
ResourceId: !Ref mcControlApiResResize
RestApiId: !Ref mcControlApi
mcStateMachineDNS:
Type: 'AWS::StepFunctions::StateMachine'
Properties:
Definition:
Comment: Step machine to update DNS while minimising Lambda execution runtime
StartAt: getIpUpdateDNS
States:
getIpUpdateDNS:
Type: Task
Resource: '${mcUpdateDns}'
Retry:
- ErrorEquals:
- noIpFound
IntervalSeconds: 3
MaxAttempts: 10
BackoffRate: 1
- ErrorEquals:
- States.TaskFailed
IntervalSeconds: 30
MaxAttempts: 2
BackoffRate: 2
- ErrorEquals:
- States.ALL
IntervalSeconds: 5
MaxAttempts: 5
BackoffRate: 2
End: true
DefinitionSubstitutions:
mcUpdateDns: !GetAtt mcUpdateDnsLambda.Arn
RoleArn: !GetAtt mcStateMachineIamRole.Arn
StateMachineName: !Sub ${AWS::StackName}-mcStateMachine
StateMachineType: STANDARD
mcCognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
AdminCreateUserConfig:
AllowAdminCreateUserOnly: True
AutoVerifiedAttributes:
- email
DeviceConfiguration:
ChallengeRequiredOnNewDevice: True
DeviceOnlyRememberedOnUserPrompt: True
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: True
RequireNumbers: True
RequireSymbols: True
RequireUppercase: True
TemporaryPasswordValidityDays: 3
Schema:
- Name: email
Required: true
UsernameAttributes:
- email
UsernameConfiguration:
CaseSensitive: false
mcUserPoolDomain:
Type: AWS::Cognito::UserPoolDomain
Properties:
Domain: !Join
- ''
- !Split
- '.cloudfront.net'
- !GetAtt mcCloudFront.DomainName
UserPoolId: !Ref mcCognitoUserPool
mcCognitoAppClient:
Type: AWS::Cognito::UserPoolClient
Properties:
AccessTokenValidity: 1
IdTokenValidity: 1
AllowedOAuthFlows:
- code
AllowedOAuthFlowsUserPoolClient: true
AllowedOAuthScopes:
- openid
- aws.cognito.signin.user.admin
CallbackURLs:
- !Sub https://${mcCloudFront.DomainName}/signed_in.html
DefaultRedirectURI: !Sub https://${mcCloudFront.DomainName}/signed_in.html
ExplicitAuthFlows:
- ALLOW_REFRESH_TOKEN_AUTH
- ALLOW_USER_SRP_AUTH
- ALLOW_CUSTOM_AUTH
GenerateSecret: false
LogoutURLs:
- !Sub https://${mcCloudFront.DomainName}/logout.html
PreventUserExistenceErrors: ENABLED
SupportedIdentityProviders:
- COGNITO
UserPoolId: !Ref mcCognitoUserPool
mcStateMachineIamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: 'sts:AssumeRole'
Description: State machine execution role
Path: /service-role/
Policies:
- PolicyName: mcStateMachineInline
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'lambda:InvokeFunction'
Resource:
- !GetAtt mcUpdateDnsLambda.Arn
mcCustomResourceLambdaIamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Description: Role for custom resource to allow lambda function to execute
Path: /service-role/
Policies:
- PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- 'logs:CreateLogGroup'
- 'logs:CreateLogStream'
- 'logs:PutLogEvents'
Resource: 'arn:aws:logs:*:*:*'
- Effect: Allow
Action:
- 's3:ListBucket'
Resource:
- !GetAtt mcLambdaSourceFilesS3.Arn
- !GetAtt mcS3Bucket.Arn
- !GetAtt mcLoggingBucket.Arn
- Effect: Allow
Action:
- 's3:PutObject'
- 's3:DeleteObject'
- 's3:GetObject'
Resource:
- !Sub
- ${bucket}/*
- bucket: !GetAtt mcLambdaSourceFilesS3.Arn
- !Sub
- ${bucket}/*
- bucket: !GetAtt mcS3Bucket.Arn
- !Sub
- ${bucket}/*
- bucket: !GetAtt mcLoggingBucket.Arn
PolicyName: mcCustomResourceInline
mcUpdateDnsLambdaIamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Description: Execution role for mcStartStop Lambda function
Path: /service-role/
Policies:
- PolicyName: mcUpdateDnsInline
PolicyDocument:
!Sub
- |
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action":[
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "route53:ChangeResourceRecordSets",
"Resource": "arn:aws:route53:::hostedzone/${hostedZone}"
},
{
"Effect": "Allow",
"Action":[
"ec2:DescribeInstances",
"ec2:DescribeTags"
],
"Resource": "*"
}
]
}
- hostedZone: !Ref HostedZoneId
mcStartStopLambdaIamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: 'sts:AssumeRole'
Description: Execution role for mcStartStop Lambda function
Path: /service-role/
Policies:
- PolicyName: mcStartStopInline
PolicyDocument:
!Sub
- |
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action":[
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "states:StartExecution",
"Resource": "arn:aws:states:${AWS::Region}:${AWS::AccountId}:stateMachine:${AWS::StackName}-mcStateMachine"
},
{
"Effect": "Allow",
"Action":[
"ec2:DescribeInstances",
"ec2:DescribeTags",
"ec2:DescribeInstanceTypes"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action":[
"ec2:StartInstances",
"ec2:ModifyInstanceAttribute",
"ec2:StopInstances"
],
"Resource": "arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*",
"Condition": {
"StringEquals": {
"ec2:ResourceTag/${tagname}": "${tagvalue}"
}
}
}
]
}
- tagname: !Ref IdTagName
tagvalue: !Ref IdTagValue
mcBackupIamRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: backup.amazonaws.com
Action: 'sts:AssumeRole'
Description: Role for custom resource to allow lambda function to execute
Path: /service-role/
Policies:
- PolicyName: mcBackupInline
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- ec2:CreateTags
- ec2:DeleteSnapshot
Resource: arn:aws:ec2:*::snapshot/*
- Effect: Allow
Action:
- ec2:CreateImage
- ec2:DeregisterImage
Resource: "*"
- Effect: Allow
Action:
- ec2:CopyImage
- ec2:CopySnapshot
Resource: "*"
- Effect: Allow
Action:
- ec2:CreateTags
Resource: arn:aws:ec2:*:*:image/*
- Effect: Allow
Action:
- ec2:DescribeSnapshots
- ec2:DescribeTags
- ec2:DescribeImages
- ec2:DescribeInstances
- ec2:DescribeInstanceAttribute
- ec2:DescribeInstanceCreditSpecifications
- ec2:DescribeNetworkInterfaces
- ec2:DescribeElasticGpus
- ec2:DescribeSpotInstanceRequests
Resource: "*"
- Effect: Allow
Action:
- ec2:CreateSnapshot
- ec2:DeleteSnapshot
- ec2:DescribeVolumes
- ec2:DescribeSnapshots
Resource:
- arn:aws:ec2:*::snapshot/*
- arn:aws:ec2:*:*:volume/*
- Action:
- tag:GetResources
Resource: "*"
Effect: Allow
- Effect: Allow
Action:
- backup:DescribeBackupVault
- backup:CopyIntoBackupVault
Resource: arn:aws:backup:*:*:backup-vault:*