-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.yaml
856 lines (813 loc) · 34.2 KB
/
template.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
AWSTemplateFormatVersion: "2010-09-09"
Description: Deploy a UIUC Bastion Host with automation.
Parameters:
SourceURL:
Type: String
Description: HTTPS URL to the UIUC Bastion Host git repository.
AllowedPattern: "^https://.+"
ConstraintDescription: Must be an HTTPS URL to a git repository.
SourceBranch:
Type: String
Description: The name of the branch to build.
Default: main
MinLength: 3
TerraformMode:
Type: String
Description: Mode of operation for the terraform commands.
AllowedValues:
- Apply (Create/Update)
- Destroy
- All
StateBucketName:
Type: String
Description: Name of the S3 bucket to store terraform state.
AllowedPattern: "^[a-z0-9][a-z0-9.-]+[a-z0-9.]$"
ConstraintDescription: Must be a valid S3 bucket name.
StateObjectKey:
Type: String
Description: Object key (path) for the terraform state in the bucket. This must be unique for each deployment.
Default: "bastion/terraform.tfstate"
AllowedPattern: "^[^/].*"
ConstraintDescription: Must be a valid S3 object key.
StateLockTableName:
Type: String
Description: Name of a DynamoDB table for terraform state locking.
Default: terraform
MinLength: 3
Service:
Type: String
Description: Service Catalog name for these resources.
Contact:
Type: String
Description: Service email address.
DataClassification:
Type: String
Description: Data Classification value for what's stored and available through this host.
Default: Internal
AllowedValues: [ Public, Internal, Sensitive, HighRisk ]
Environment:
Type: String
Description: Environment name (prod, test, dev, or poc).
Default: ""
AllowedValues:
- ""
- Prod
- Production
- Test
- Dev
- Development
- DevTest
- POC
- prod
- production
- test
- dev
- development
- devtest
- poc
Hostname:
Type: String
Description: Hostname of the bastion host that you will associate with the IP.
AllowedPattern: "^[a-zA-Z0-9]([a-zA-Z0-9.-]{0,61}[a-zA-Z0-9])?\\.[a-zA-Z]{2,}$"
ConstraintDescription: Must be a validly formatted hostname.
InstanceType:
Type: String
Description: Type of the instance to launch, which affects cost and features.
Default: t3.micro
SSHKeyName:
Type: AWS::EC2::KeyPair::KeyName
Description: EC2 SSH KeyPair for allowing access via the builtin account (ec2-user).
EnhancedMonitoring:
Type: String
Description: Use enahanced/detailed monitoring on supported resources.
Default: "false"
AllowedValues: [ "true", "false" ]
FalconSensorPackage:
Type: String
Description: S3 URL (s3://bucket/path/to/sensor.rpm) to download the CrowdStrike Falcon Sensor (optional).
Default: ""
AllowedPattern: "^(s3://[a-z0-9][a-z0-9.-]+[a-z0-9.]/.+\\.rpm)?$"
ConstraintDescription: The S3 URL must be empty or of the form "s3://bucket/path/to/sensor.rpm".
ShellIdleTimeout:
Type: Number
Description: Number of seconds before an idle shell is disconnected.
Default: 900
MinValue: 0
AllowedCIDRs:
Type: CommaDelimitedList
Description: List of IPv4 CIDRs allowed to SSH to the bastion host.
Default: "0.0.0.0/0"
AllowedPattern: "^\\d+\\.\\d+\\.\\d+\\.\\d+/\\d+$"
ConstraintDescription: "Values must be in CIDR form: XXX.XXX.XXX.XXX/YY"
PublicSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Subnet names for public access where the primary IP will be.
InternalSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Subnet names to use for internal resources unreachable from outside the VPC.
ExtraSecurityGroups:
Type: CommaDelimitedList
Description: List of security groups to associate with the primary interface. The default one includes ingress SSH and all egress traffic.
Default: ""
ExtraENIs:
Type: String
Description: List of extra ENIs to attach to the bastion host (in HCL format). You can configure what routes this ENI is used for by provising prefix list names or IDs and/or VPC IDs.
Default: ""
ExtraEFS:
Type: String
Description: Map of extra EFS's to mount to the bastion host (in HCL format).
Default: ""
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label: Source Code
Parameters:
- SourceURL
- SourceBranch
- Label: Terraform
Parameters:
- TerraformMode
- StateBucketName
- StateObjectKey
- StateLockTableName
- Label: Tagging
Parameters:
- Service
- Contact
- DataClassification
- Environment
- Label: Instance
Parameters:
- Hostname
- InstanceType
- SSHKeyName
- EnhancedMonitoring
- FalconSensorPackage
- ShellIdleTimeout
- Label: Networking
Parameters:
- AllowedCIDRs
- PublicSubnets
- InternalSubnets
- ExtraSecurityGroups
- ExtraENIs
- ExtraEFS
Conditions:
HasDestroy: !Or
- !Equals [ !Ref TerraformMode, "Destroy" ]
- !Equals [ !Ref TerraformMode, "All" ]
HasApply: !Or
- !Equals [ !Ref TerraformMode, "Apply (Create/Update)" ]
- !Equals [ !Ref TerraformMode, "All" ]
Resources:
TerraformRole:
Type: AWS::IAM::Role
Properties:
Description: Allow CodeBuild to run terraform to deploy the bastion host.
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- codebuild.amazonaws.com
Action: [ 'sts:AssumeRole' ]
Path: /
ManagedPolicyArns:
- !Ref TerraformDeployReadOnlyPolicy
- !Ref TerraformDeployReadWritePolicy
- !Ref TerraformDeploySecurityPolicy
Policies:
- PolicyName: codebuild
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogStream"
- "logs:PutLogEvents"
Resource:
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${ApplyProjectLogs}
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${ApplyProjectLogs}:*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${DestroyProjectLogs}
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:${DestroyProjectLogs}:*
- PolicyName: terraform
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: "s3:GetBucketLocation"
Resource: "*"
- Effect: Allow
Action: "s3:ListBucket"
Resource: !Sub "arn:aws:s3:::${StateBucketName}"
- Effect: Allow
Action:
- "s3:GetObject"
- "s3:PutObject"
Resource: !Sub "arn:aws:s3:::${StateBucketName}/${StateObjectKey}"
- Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:DeleteItem"
Resource: !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${StateLockTableName}"
- PolicyName: parameters
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ReadOnly
Effect: Allow
Action:
- "ssm:DescribeParameters"
- "ssm:ListTagsForResource"
- "kms:ListKeys"
Resource: "*"
- Sid: ReadWrite
Effect: Allow
Action:
- "ssm:GetParameter*"
- "ssm:PutParameter"
- "ssm:*Tags*"
Resource:
- !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/ssh/*"
- Sid: KMSReadWrite
Effect: Allow
Action:
- "kms:Decrypt"
- "kms:Encrypt"
- "kms:GenerateDataKey"
Resource:
- !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*"
Condition:
ArnLike:
"kms:EncryptionContext:PARAMETER_ARN":
- !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/ssh/*"
Tags:
- { Key: Service, Value: !Ref Service }
- { Key: Contact, Value: !Ref Contact }
- { Key: Environment, Value: !Ref Environment }
- { Key: Project, Value: !Ref "AWS::StackName" }
TerraformDeployReadOnlyPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${AWS::StackName}-DeployReadOnly-${AWS::Region}"
Description: Policy to deploy resources in the terraform.
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ReadOnly
Effect: Allow
Action:
- "ec2:DescribeAddresses*"
- "ec2:DescribeImages"
- "ec2:DescribeInstanceTypes"
- "ec2:DescribeLaunchTemplate*"
- "ec2:DescribeManagedPrefixLists"
- "ec2:DescribeNetworkInterfaces"
- "ec2:DescribePrefixLists"
- "ec2:DescribeSecurityGroup*"
- "ec2:DescribeStaleSecurityGroups"
- "ec2:DescribeSubnets"
- "ec2:DescribeVpc*"
- "ec2:GetManagedPrefixList*"
- "autoscaling:Describe*"
- "logs:DescribeLogGroups"
- "logs:DescribeResourcePolicies"
- "logs:TestMetricFilter"
- "elasticfilesystem:Describe*"
- "elasticfilesystem:ListTagsForResource"
- "iam:ListInstanceProfiles"
- "iam:ListPolicies"
- "iam:ListRoles"
- "kms:ListAliases"
- "kms:ListKeys"
- "s3:GetAccountPublicAccessBlock"
- "s3:ListAllMyBuckets"
- "lambda:GetAccountSettings"
- "lambda:ListCodeSigningConfigs"
- "lambda:ListEventSourceMappings"
- "lambda:ListFunctions*"
- "lambda:ListLayer*"
- "events:DescribeRule"
- "events:ListEventBuses"
- "events:ListRule*"
- "events:TestEventPattern"
- "ssm:DescribeAssociation*"
- "ssm:DescribeAvailablePatches"
- "ssm:DescribeDocument*"
- "ssm:DescribeEffective*"
- "ssm:DescribeInstance*"
- "ssm:DescribeMaintenanceWindow*"
- "ssm:DescribePatchBaselines"
- "ssm:DescribePatchGroup*"
- "ssm:DescribePatchProperties"
- "ssm:GetDefaultPatchBaseline"
- "ssm:GetDocument"
- "ssm:GetInventory*"
- "ssm:GetMaintenanceWindow*"
- "ssm:GetParameter*"
- "ssm:GetPatchBaseline*"
- "ssm:ListAssociation*"
- "ssm:ListDocument*"
- "ssm:ListInstanceAssociations"
- "ssm:ListInventoryEntries"
- "ssm:ListTagsForResource"
Resource: '*'
TerraformDeployReadWritePolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${AWS::StackName}-DeployReadWrite-${AWS::Region}"
Description: Policy to deploy resources in the terraform.
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ReadWrite
Effect: Allow
Action:
- "ec2:CreateLaunchTemplate*"
- "ec2:DeleteLaunchTemplate*"
- "ec2:ModifyLaunchTemplate"
- "ec2:AuthorizeSecurityGroup*"
- "ec2:CreateSecurityGroup"
- "ec2:DeleteSecurityGroup"
- "ec2:RevokeSecurityGroup*"
- "ec2:UpdateSecurityGroupRuleDescriptions*"
- "ec2:CreateTags"
- "ec2:DeleteTags"
- "ec2:RunInstances"
- "autoscaling:*"
- "logs:AssociateKmsKey"
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:DeleteLogGroup"
- "logs:DeleteLogStream"
- "logs:DeleteMetricFilter"
- "logs:DeleteRetentionPolicy"
- "logs:DeleteSubscriptionFilter"
- "logs:DescribeLogStreams"
- "logs:DescribeMetricFilters"
- "logs:DescribeSubscriptionFilters"
- "logs:DisassociateKmsKey"
- "logs:ListTagsLogGroup"
- "logs:PutMetricFilter"
- "logs:PutRetentionPolicy"
- "logs:PutSubscriptionFilter"
- "logs:TagLogGroup"
- "logs:UntagLogGroup"
- "elasticfilesystem:Create*"
- "elasticfilesystem:DeleteAccessPoint"
- "elasticfilesystem:DeleteFileSystemPolicy"
- "elasticfilesystem:DeleteMountTarget"
- "elasticfilesystem:DeleteTags"
- "elasticfilesystem:ModifyMountTargetSecurityGroups"
- "elasticfilesystem:PutBackupPolicy"
- "elasticfilesystem:PutFileSystemPolicy"
- "elasticfilesystem:PutLifecycleConfiguration"
- "elasticfilesystem:TagResource"
- "elasticfilesystem:UntagResource"
- "elasticfilesystem:UpdateFileSystem"
- "s3:*"
- "lambda:*"
- "ssm:*"
Resource:
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:elastic-ip/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:launch-template/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:volume/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:network-interface/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:security-group/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:subnet/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:vpc/*
- !Sub arn:aws:ec2:${AWS::Region}::image/*
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:key-pair/${SSHKeyName}
- !Sub arn:aws:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/${AWS::StackName}
- !Sub arn:aws:autoscaling:${AWS::Region}:${AWS::AccountId}:autoScalingGroup:*:autoScalingGroupName/${AWS::StackName}-*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}:*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}-*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/${AWS::StackName}-*:*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/${AWS::StackName}/*
- !Sub arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/${AWS::StackName}/*:*
- !Sub arn:aws:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:file-system/*
- !Sub arn:aws:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:access-point/*
- !Sub arn:aws:s3:::${AWS::StackName}
- !Sub arn:aws:s3:::${AWS::StackName}-*
- !Sub arn:aws:s3:::${AWS::StackName}/*
- !Sub arn:aws:s3:::${AWS::StackName}-*/*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}:*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*:*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:${AWS::StackName}
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:${AWS::StackName}-*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:${AWS::StackName}:*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:${AWS::StackName}-*:*
- !Sub arn:aws:ssm:${AWS::Region}:*:document/*
- !Sub arn:aws:ssm:${AWS::Region}:*:patchbaseline/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:association/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:maintenancewindow/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/outputs/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:windowtarget/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:windowtask/*
- !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:task/*
Condition:
StringEqualsIfExists:
"aws:RequestTag/Project": !Ref "AWS::StackName"
"aws:ResourceTag/Project": !Ref "AWS::StackName"
"ec2:Owner": amazon
ArnLikeIfExists:
"ec2:InstanceProfile":
- !Sub "arn:aws:iam::${AWS::AccountId}:instance-profile/${AWS::StackName}"
- !Sub "arn:aws:iam::${AWS::AccountId}:instance-profile/${AWS::StackName}-*"
- Sid: EC2Global
Effect: Allow
Action:
- "ec2:AllocateAddress"
- "ec2:DisassociateAddress"
- "ec2:ModifyAddressAttribute"
- "ec2:ResetAddressAttribute"
Resource: "*"
- Sid: LambdaEventSourceMappings
Effect: Allow
Action:
- "lambda:CreateEventSourceMapping"
- "lambda:DeleteEventSourceMapping"
- "lambda:GetEventSourceMapping"
- "lambda:UpdateEventSourceMapping"
Resource: "*"
Condition:
ArnLike:
"lambda:FunctionArn":
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}:*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*:*
- Sid: EventsReadWrite
Effect: Allow
Action:
- "events:DeleteRule"
- "events:DisableRule"
- "events:EnableRule"
- "events:ListTagsForResource"
- "events:ListTargetsByRule"
- "events:PutRule"
- "events:PutTargets"
- "events:RemoveTargets"
- "events:TagResource"
- "events:UntagResource"
Resource:
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${AWS::StackName}
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/${AWS::StackName}-*
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/default/${AWS::StackName}
- !Sub arn:aws:events:${AWS::Region}:${AWS::AccountId}:rule/default/${AWS::StackName}-*
Condition:
ArnLikeIfExists:
"events:TargetArn":
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}:*
- !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${AWS::StackName}-*:*
- Sid: SSMReadWrite
Effect: Allow
Action:
- "ssm:CreateMaintenanceWindow"
- "ssm:CreatePatchBaseline"
Resource: "*"
Condition:
StringEqualsIfExists:
"aws:RequestTag/Project": !Ref "AWS::StackName"
"aws:ResourceTag/Project": !Ref "AWS::StackName"
- Sid: SSMGlobalReadWrite
Effect: Allow
Action:
- "ssm:AddTagsToResource"
- "ssm:RemoveTagsFromResource"
Resource: "*"
TerraformDeploySecurityPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${AWS::StackName}-DeploySecurity-${AWS::Region}"
Description: Policy to deploy resources in the terraform.
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: ReadWrite
Effect: Allow
Action:
- "iam:AddRoleToInstanceProfile"
- "iam:AttachRolePolicy"
- "iam:CreateInstanceProfile"
- "iam:CreatePolicy*"
- "iam:CreateRole"
- "iam:DeleteInstanceProfile"
- "iam:DeletePolicy*"
- "iam:DeleteRole*"
- "iam:DetachRolePolicy"
- "iam:GetInstanceProfile"
- "iam:GetPolicy*"
- "iam:GetRole*"
- "iam:ListAttachedRolePolicies"
- "iam:ListEntitiesForPolicy"
- "iam:ListInstanceProfileTags"
- "iam:ListInstanceProfilesForRole"
- "iam:ListPolicy*"
- "iam:ListRolePolicies"
- "iam:ListRoleTags"
- "iam:PassRole"
- "iam:PutRole*"
- "iam:RemoveRoleFromInstanceProfile"
- "iam:SetDefaultPolicyVersion"
- "iam:TagInstanceProfile"
- "iam:TagPolicy"
- "iam:TagRole"
- "iam:UntagInstanceProfile"
- "iam:UntagPolicy"
- "iam:UntagRole"
- "iam:UpdateAssumeRolePolicy"
- "iam:UpdateRole*"
- "kms:CreateAlias"
- "kms:DeleteAlias"
- "kms:DescribeKey"
- "kms:DisableKey"
- "kms:DisableKeyRotation"
- "kms:EnableKey"
- "kms:EnableKeyRotation"
- "kms:GetKey*"
- "kms:ListGrants"
- "kms:ListKeyPolicies"
- "kms:ListResourceTags"
- "kms:ListRetirableGrants"
- "kms:PutKeyPolicy"
- "kms:ScheduleKeyDeletion"
- "kms:TagResource"
- "kms:UntagResource"
- "kms:UpdateAlias"
- "kms:UpdateKeyDescription"
Resource:
- !Sub arn:aws:iam::${AWS::AccountId}:instance-profile/${AWS::StackName}
- !Sub arn:aws:iam::${AWS::AccountId}:instance-profile/${AWS::StackName}-*
- !Sub arn:aws:iam::${AWS::AccountId}:role/${AWS::StackName}
- !Sub arn:aws:iam::${AWS::AccountId}:role/${AWS::StackName}-*
- !Sub arn:aws:iam::${AWS::AccountId}:role/${AWS::StackName}/*
- !Sub arn:aws:iam::${AWS::AccountId}:policy/${AWS::StackName}
- !Sub arn:aws:iam::${AWS::AccountId}:policy/${AWS::StackName}-*
- !Sub arn:aws:iam::${AWS::AccountId}:policy/${AWS::StackName}/*
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/${AWS::StackName}
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/${AWS::StackName}/*
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:alias/${AWS::StackName}-*
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*
Condition:
StringEqualsIfExists:
"aws:RequestTag/Project": !Ref "AWS::StackName"
"aws:ResourceTag/Project": !Ref "AWS::StackName"
- Sid: KMSCreate
Effect: Allow
Action: "kms:CreateKey"
Resource: "*"
- Sid: KMSGrants
Effect: Allow
Action:
- "kms:CreateGrant"
- "kms:RetireGrant"
- "kms:RevokeGrant"
Resource:
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*
Condition:
Bool:
"kms:GrantIsForAWSResource": "true"
- Sid: KMSReadWrite
Effect: Allow
Action:
- "kms:Decrypt"
- "kms:Encrypt"
- "kms:GenerateDataKey"
Resource:
- !Sub "arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/*"
Condition:
ArnLike:
"kms:EncryptionContext:PARAMETER_ARN":
- !Sub "arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${AWS::StackName}/outputs/*"
TerraformDestroyManagedPolicy:
Condition: HasDestroy
Type: AWS::IAM::ManagedPolicy
Properties:
ManagedPolicyName: !Sub "${AWS::StackName}-Destroy-${AWS::Region}"
Description: Policy to destroy protected resources in the terraform.
Roles: [ !Ref TerraformRole ]
PolicyDocument:
Version: "2012-10-17"
Statement:
- Sid: Delete
Effect: Allow
Action:
- "ec2:ReleaseAddress"
- "elasticfilesystem:DeleteFileSystem"
Resource:
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:elastic-ip/*
- !Sub arn:aws:elasticfilesystem:${AWS::Region}:${AWS::AccountId}:file-system/*
Condition:
StringEqualsIfExists:
"aws:RequestTag/Project": !Ref "AWS::StackName"
"aws:ResourceTag/Project": !Ref "AWS::StackName"
ApplyProjectLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/codebuild/${AWS::StackName}-APPLY"
RetentionInDays: 7
ApplyProject:
Type: AWS::CodeBuild::Project
Condition: HasApply
DependsOn:
- TerraformDeployReadOnlyPolicy
- TerraformDeployReadWritePolicy
- TerraformDeploySecurityPolicy
Properties:
Name: !Sub "${AWS::StackName}-APPLY"
Description: Apply changes to the UIUC Bastion Host terraform.
Source:
Type: NO_SOURCE
BuildSpec: |
version: 0.2
env:
shell: bash
variables:
TERRAFORM_VERSION: 1.4.5
TF_INPUT: "false"
TF_IN_AUTOMATION: "true"
TF_CLI_ARGS: "-no-color"
phases:
install:
runtime-versions:
python: 3.10
commands:
- '[[ -e .local/bin ]] || mkdir -p .local/bin'
- 'export PATH="$CODEBUILD_SRC_DIR/.local/bin:$PATH"'
- |
if [[ ! -e .local/bin/terraform ]]; then
curl --silent --fail --location https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip | zcat > .local/bin/terraform
chmod a+rx .local/bin/terraform
fi
- 'git clone --depth 1 -b "$SOURCE_BRANCH" $SOURCE_URL $CODEBUILD_SRC_DIR/repo'
- 'chmod a+rx $CODEBUILD_SRC_DIR/repo/codebuild/bin/*'
- 'export TF_DATA_DIR="$CODEBUILD_SRC_DIR/.terraform"'
- 'mkdir -p $TF_DATA_DIR || :'
build:
on-failure: ABORT
commands:
- '$CODEBUILD_SRC_DIR/repo/codebuild/bin/update-hostkeys.sh "$TF_VAR_project" "$TF_VAR_hostname"'
- '$CODEBUILD_SRC_DIR/repo/codebuild/bin/deploy.sh $CODEBUILD_SRC_DIR/repo/terraform/'
cache:
paths:
- .terraform/**/*
ConcurrentBuildLimit: 1
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:6.0
EnvironmentVariables:
- { Name: AWS_REGION, Value: !Ref "AWS::Region" }
- { Name: SOURCE_URL, Value: !Ref SourceURL }
- { Name: SOURCE_BRANCH, Value: !Ref SourceBranch }
- { Name: STATE_BUCKET_NAME, Value: !Ref StateBucketName }
- { Name: STATE_OBJECT_KEY, Value: !Ref StateObjectKey }
- { Name: STATE_LOCK_TABLE_NAME, Value: !Ref StateLockTableName }
- { Name: TF_VAR_service, Value: !Ref Service }
- { Name: TF_VAR_contact, Value: !Ref Contact }
- { Name: TF_VAR_data_classification, Value: !Ref DataClassification }
- { Name: TF_VAR_environment, Value: !Ref Environment }
- { Name: TF_VAR_project, Value: !Ref "AWS::StackName" }
- { Name: TF_VAR_hostname, Value: !Ref Hostname }
- { Name: TF_VAR_instance_type, Value: !Ref InstanceType }
- { Name: TF_VAR_key_name, Value: !Ref SSHKeyName }
- { Name: TF_VAR_enhanced_monitoring, Value: !Ref EnhancedMonitoring }
- { Name: TF_VAR_falcon_sensor_package, Value: !Ref FalconSensorPackage }
- { Name: TF_VAR_shell_idle_timeout, Value: !Ref ShellIdleTimeout }
- Name: TF_VAR_allowed_cidrs
Value: "{}"
- Name: TF_VAR_allowed_cidrs_codebuild
Value: !Join [ ",", !Ref AllowedCIDRs ]
- Name: TF_VAR_public_subnets
Value: !Join [ ",", !Ref PublicSubnets ]
- Name: TF_VAR_internal_subnets
Value: !Join [ ",", !Ref InternalSubnets ]
- Name: TF_VAR_extra_security_groups
Value: !Join [ ",", !Ref ExtraSecurityGroups ]
- { Name: TF_VAR_extra_enis, Value: !Ref ExtraENIs }
- { Name: TF_VAR_extra_efs, Value: !Ref ExtraEFS }
TimeoutInMinutes: 60
Artifacts:
Type: NO_ARTIFACTS
Cache:
Type: LOCAL
Modes: [ LOCAL_CUSTOM_CACHE ]
LogsConfig:
CloudWatchLogs:
Status: ENABLED
GroupName: !Ref ApplyProjectLogs
ServiceRole: !Ref TerraformRole
Tags:
- { Key: Service, Value: !Ref Service }
- { Key: Contact, Value: !Ref Contact }
- { Key: Environment, Value: !Ref Environment }
- { Key: Project, Value: !Ref "AWS::StackName" }
DestroyProjectLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/codebuild/${AWS::StackName}-DESTROY"
RetentionInDays: 7
DestroyProject:
Type: AWS::CodeBuild::Project
Condition: HasDestroy
DependsOn:
- TerraformDeployReadOnlyPolicy
- TerraformDeployReadWritePolicy
- TerraformDeploySecurityPolicy
- TerraformDestroyManagedPolicy
Properties:
Name: !Sub "${AWS::StackName}-DESTROY"
Description: Removes/Destroys the UIUC Bastion Host terraform.
Source:
Type: NO_SOURCE
BuildSpec: |
version: 0.2
env:
shell: bash
variables:
TERRAFORM_VERSION: 1.4.5
TF_INPUT: "false"
TF_IN_AUTOMATION: "true"
TF_CLI_ARGS: "-no-color"
TF_CLI_ARGS_plan: "-destroy"
phases:
install:
runtime-versions:
python: 3.10
commands:
- '[[ -e .local/bin ]] || mkdir -p .local/bin'
- 'export PATH="$CODEBUILD_SRC_DIR/.local/bin:$PATH"'
- |
if [[ ! -e .local/bin/terraform ]]; then
curl --silent --fail --location https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip | zcat > .local/bin/terraform
chmod a+rx .local/bin/terraform
fi
- 'git clone --depth 1 -b "$SOURCE_BRANCH" $SOURCE_URL $CODEBUILD_SRC_DIR/repo'
- 'chmod a+rx $CODEBUILD_SRC_DIR/repo/codebuild/bin/*'
- 'export TF_DATA_DIR="$CODEBUILD_SRC_DIR/.terraform"'
- 'mkdir -p $TF_DATA_DIR || :'
build:
on-failure: ABORT
commands:
- '$CODEBUILD_SRC_DIR/repo/codebuild/bin/deploy.sh $CODEBUILD_SRC_DIR/repo/terraform/'
cache:
paths:
- .terraform/**/*
ConcurrentBuildLimit: 1
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Type: LINUX_CONTAINER
Image: aws/codebuild/standard:6.0
EnvironmentVariables:
- { Name: AWS_REGION, Value: !Ref "AWS::Region" }
- { Name: SOURCE_URL, Value: !Ref SourceURL }
- { Name: SOURCE_BRANCH, Value: !Ref SourceBranch }
- { Name: STATE_BUCKET_NAME, Value: !Ref StateBucketName }
- { Name: STATE_OBJECT_KEY, Value: !Ref StateObjectKey }
- { Name: STATE_LOCK_TABLE_NAME, Value: !Ref StateLockTableName }
- { Name: TF_VAR_service, Value: !Ref Service }
- { Name: TF_VAR_contact, Value: !Ref Contact }
- { Name: TF_VAR_data_classification, Value: !Ref DataClassification }
- { Name: TF_VAR_environment, Value: !Ref Environment }
- { Name: TF_VAR_project, Value: !Ref "AWS::StackName" }
- { Name: TF_VAR_hostname, Value: !Ref Hostname }
- { Name: TF_VAR_instance_type, Value: !Ref InstanceType }
- { Name: TF_VAR_key_name, Value: !Ref SSHKeyName }
- { Name: TF_VAR_enhanced_monitoring, Value: !Ref EnhancedMonitoring }
- { Name: TF_VAR_falcon_sensor_package, Value: !Ref FalconSensorPackage }
- { Name: TF_VAR_shell_idle_timeout, Value: !Ref ShellIdleTimeout }
- Name: TF_VAR_allowed_cidrs
Value: "{}"
- Name: TF_VAR_allowed_cidrs_codebuild
Value: !Join [ ",", !Ref AllowedCIDRs ]
- Name: TF_VAR_public_subnets
Value: !Join [ ",", !Ref PublicSubnets ]
- Name: TF_VAR_internal_subnets
Value: !Join [ ",", !Ref InternalSubnets ]
- { Name: TF_VAR_extra_enis, Value: !Ref ExtraENIs }
- { Name: TF_VAR_extra_efs, Value: !Ref ExtraEFS }
TimeoutInMinutes: 60
Artifacts:
Type: NO_ARTIFACTS
Cache:
Type: LOCAL
Modes: [ LOCAL_CUSTOM_CACHE ]
LogsConfig:
CloudWatchLogs:
Status: ENABLED
GroupName: !Ref DestroyProjectLogs
ServiceRole: !Ref TerraformRole
Tags:
- { Key: Service, Value: !Ref Service }
- { Key: Contact, Value: !Ref Contact }
- { Key: Environment, Value: !Ref Environment }
- { Key: Project, Value: !Ref "AWS::StackName" }