-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.tf
1232 lines (1200 loc) · 53.7 KB
/
main.tf
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
## Enables a GuardDuty Detector in your Region Specified by Provider.tf
resource "aws_guardduty_detector" "GuardDuty_Detector" {
enable = true
finding_publishing_frequency = "${var.GuardDuty_Finding_Publishing_Frequency}"
}
## Creates a CMK to use for Encrypting CloudTrail Logs
resource "aws_kms_key" "CloudTrail_Customer_CMK" {
description = "CloudTrail Encryption - Managed by Terraform"
deletion_window_in_days = "${var.CloudTrail_CMK_Deletion_Window}"
enable_key_rotation = true
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "Key policy created by CloudTrail",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {"AWS": [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root",
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/${aws_iam_user.KMS_Key_Admin_IAM_User.name}"
]},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow CloudTrail to encrypt logs",
"Effect": "Allow",
"Principal": {"Service": ["cloudtrail.amazonaws.com"]},
"Action": "kms:GenerateDataKey*",
"Resource": "*",
"Condition": {"StringLike": {"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"}}
},
{
"Sid": "Allow CloudTrail to describe key",
"Effect": "Allow",
"Principal": {"Service": ["cloudtrail.amazonaws.com"]},
"Action": "kms:DescribeKey",
"Resource": "*"
},
{
"Sid": "Allow principals in the account to decrypt log files",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": [
"kms:Decrypt",
"kms:ReEncryptFrom"
],
"Resource": "*",
"Condition": {
"StringEquals": {"kms:CallerAccount": "${data.aws_caller_identity.current.account_id}"},
"StringLike": {"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"}
}
},
{
"Sid": "Allow alias creation during setup",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": "kms:CreateAlias",
"Resource": "*",
"Condition": {"StringEquals": {
"kms:ViaService": "ec2.region.amazonaws.com",
"kms:CallerAccount": "${data.aws_caller_identity.current.account_id}"
}}
},
{
"Sid": "Enable cross account log decryption",
"Effect": "Allow",
"Principal": {"AWS": "*"},
"Action": [
"kms:Decrypt",
"kms:ReEncryptFrom"
],
"Resource": "*",
"Condition": {
"StringEquals": {"kms:CallerAccount": "${data.aws_caller_identity.current.account_id}"},
"StringLike": {"kms:EncryptionContext:aws:cloudtrail:arn": "arn:aws:cloudtrail:*:${data.aws_caller_identity.current.account_id}:trail/*"}
}
}
]
}
POLICY
}
## Creates a KMS Key for using to Encrypt AWS Config Recordings sent to SNS
resource "aws_kms_key" "Config_Recorder_SNS_Customer_CMK" {
description = "Encrypt AWS Config Recordings sent to SNS - Manged by Terraform"
deletion_window_in_days = "${var.Config_Recorder_SNS_Customer_CMK_Deletion_Window}"
enable_key_rotation = true
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "key-consolepolicy-3",
"Statement": [
{
"Sid": "Enable IAM User Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
},
"Action": "kms:*",
"Resource": "*"
},
{
"Sid": "Allow access for Key Administrators",
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:user/${aws_iam_user.KMS_Key_Admin_IAM_User.name}"
]
},
"Action": [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
],
"Resource": "*"
},
{
"Sid": "Allow config access",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.Config_IAM_Role.arn}"
},
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey",
"kms:Encrypt",
"kms:Describe",
"kms:Get*"
],
"Resource": "*"
},
{
"Sid": "Allow sns access",
"Effect": "Allow",
"Principal": {
"Service": "sns.amazonaws.com"
},
"Action": [
"kms:Decrypt",
"kms:GenerateDataKey*",
"kms:Describe",
"kms:Get*",
"kms:Encrypt"
],
"Resource": "*"
}
]
}
POLICY
}
resource "aws_kms_alias" "CloudTrail_Key_Alias" {
name = "alias/${var.CloudTrail_Key_Alias_Name}"
target_key_id = "${aws_kms_key.CloudTrail_Customer_CMK.arn}"
}
resource "aws_kms_alias" "Config_SNS_Key_Alias" {
name = "alias/${var.Config_SNS_Key_Alias_Name}"
target_key_id = "${aws_kms_key.Config_Recorder_SNS_Customer_CMK.arn}"
}
## not specifiying 'resource_group_arn' in Assessment Target will apply to all EC2 Instances w/ Inspector Agent for
resource "aws_inspector_assessment_target" "Inspector_Assessment_Target_All" {
name = "${var.Inspector_Assessment_Target_All_Group_Name}"
}
## Uses LIST Variables for Rule Packages
resource "aws_inspector_assessment_template" "Inspector_Assessment_Template" {
name = "${var.Inspector_Assessment_Template_Name}"
target_arn = "${aws_inspector_assessment_target.Inspector_Assessment_Target_All.arn}"
duration = 3600
rules_package_arns = "${var.Inspector_Assessment_Rules_Packages_USEast1}"
}
## Creates S3 Bucket to upload Lambda Function ZIPs into for later usage
## Bucket is Versioned, Logged via CT, Logged for HTTP Access Logs and Uses SSE-S3 Encryption
resource "aws_s3_bucket" "Lambda_Artifacts_S3_Bucket" {
bucket = "${var.Lambda_Artifacts_S3_Bucket_Name}"
acl = "private"
versioning {
enabled = true
}
logging {
target_bucket = "${aws_s3_bucket.Server_Access_Log_S3_Bucket.id}"
target_prefix = "LambdaAccess/"
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
## Uploads the GuardDuty Log Parsing Lambda Function to the Created S3 Bucket
resource "aws_s3_bucket_object" "GuardDuty_Log_Parsing_Lambda_Object_Upload" {
bucket = "${aws_s3_bucket.Lambda_Artifacts_S3_Bucket.id}"
key = "CMDS-Lambdas/gd-sorter.zip"
source = "${var.Path_To_Lambda_Upload}/gd-sorter.zip"
}
## Creates Lambda Function to Parse out GuardDuty Findings for crawling with Glue & Querying with Athena
## X-Ray Tracing is Enabled to get Traces for Debug & APM
resource "aws_lambda_function" "Lambda_Function_GuardDuty_Log_Parsing" {
s3_bucket = "${aws_s3_bucket.Lambda_Artifacts_S3_Bucket.id}"
s3_key = "${aws_s3_bucket_object.GuardDuty_Log_Parsing_Lambda_Object_Upload.id}"
function_name = "${var.GuardDuty_LogParsing_Function_Name}"
description = "Lambda Function to Parse out Findings from GuardDuty for eventual consumption by Glue"
role = "${aws_iam_role.Lambda_Function_GuardDuty_Log_Parsing_IAM_Role.arn}"
handler = "gd-sorter.lambda_handler"
runtime = "python3.6"
memory_size = "${var.GuardDuty_LogParsing_FunctionMemory}"
timeout = "${var.GuardDuty_LogParsing_FunctionTimeout}"
tracing_config {
mode = "Active"
}
}
## Lambda Execution Role for GuardDuty Parsing Function
resource "aws_iam_role" "Lambda_Function_GuardDuty_Log_Parsing_IAM_Role" {
name = "${var.GuardDuty_LogParsing_Function_Name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "GDLogParsing_Lambda_Attach_LambdaExecute" {
role = "${aws_iam_role.Lambda_Function_GuardDuty_Log_Parsing_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AWSLambdaExecute.arn}"
}
resource "aws_iam_role_policy_attachment" "GDLogParsing_Lambda_Attach_AWSXrayWriteOnlyAccess" {
role = "${aws_iam_role.Lambda_Function_GuardDuty_Log_Parsing_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AWSXrayWriteOnlyAccess.arn}"
}
## Allows Source Invocation from S3 to fire off GuardDuty Parsing Function
resource "aws_lambda_permission" "GuardDuty_Lambda_Bucket_Invocation_Permission" {
statement_id = "AllowExecutionFromS3Bucket"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.Lambda_Function_GuardDuty_Log_Parsing.arn}"
principal = "s3.amazonaws.com"
source_arn = "${aws_s3_bucket.GuardDuty_Finding_KDF_Logs_Bucket.arn}"
}
## Uploads the Inspector auto remediation Lambda file into S3
resource "aws_s3_bucket_object" "Inspector_Remediation_Lambda_Object_Upload" {
bucket = "${aws_s3_bucket.Lambda_Artifacts_S3_Bucket.id}"
key = "CMDS-Lambdas/lambda-auto-remediate.zip"
source = "${var.Path_To_Lambda_Upload}/lambda-auto-remediate.zip"
}
## Creates a Lambda function that is invoked from SNS to call Systems Manager Run Command to run updates on Debian and Amazon Linux based instances
## SNS Topic will be configured from the Inspector end (not yet supported via Terraform) to emit on Findings to invoke the function
## The Function will invoke multiple times per function hence the high memory & timeout
## Systems Manager Run Command log outputs will show multiple failures (false negative) due to the concurrency limitations
resource "aws_lambda_function" "Lambda_Function_Inspector_Remediation" {
s3_bucket = "${aws_s3_bucket.Lambda_Artifacts_S3_Bucket.id}"
s3_key = "${aws_s3_bucket_object.Inspector_Remediation_Lambda_Object_Upload.id}"
function_name = "${var.Inspector_Remediation_Function_Name}"
description = "Invokes SSM Run Command based on Inspector Findings from SNS"
role = "${aws_iam_role.Lambda_Function_Inspector_Remediation_IAMRole.arn}"
handler = "lambda-auto-remediate.lambda_handler"
runtime = "python2.7"
memory_size = "${var.Inspector_Remediation_Function_Memory}"
timeout = "${var.Inspector_Remediation_Function_Timeout}"
tracing_config {
mode = "Active"
}
}
resource "aws_iam_role" "Lambda_Function_Inspector_Remediation_IAMRole" {
name = "${var.Inspector_Remediation_Function_Name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "Remediation_Lambda_Attach_InspectorRO" {
role = "${aws_iam_role.Lambda_Function_Inspector_Remediation_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AmazonInspectorReadOnlyAccess.arn}"
}
resource "aws_iam_role_policy_attachment" "Remediation_Lambda_Attach_SSMFullAccess" {
role = "${aws_iam_role.Lambda_Function_Inspector_Remediation_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AmazonSSMFullAccess.arn}"
}
resource "aws_iam_role_policy_attachment" "Remediation_Lambda_Attach_AWSXrayWriteOnlyAccess" {
role = "${aws_iam_role.Lambda_Function_Inspector_Remediation_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AWSXrayWriteOnlyAccess.arn}"
}
resource "aws_iam_role_policy_attachment" "Remediation_Lambda_Attach_BasicLambdaExec" {
role = "${aws_iam_role.Lambda_Function_Inspector_Remediation_IAMRole.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AWSLambdaBasicExecutionRole.arn}"
}
## SNS Topic that you will subscribed SNS to -- the Policy is created in an external Data document
## You can optionally add encryption to this SNS topic
resource "aws_sns_topic" "Inspector_Remediation_SNS_Topic" {
name = "${var.Inspector_Remediation_SNS_Topic_Name}"
}
## Within the Data Policy a Variable is passed for the ROOT Principal for the Inspector Service
## Inspector's Service Account will emit the findings based on telemetry and needs Access to publish
resource "aws_sns_topic_policy" "Inspector_Remediation_SNS_Topic_Policy" {
arn = "${aws_sns_topic.Inspector_Remediation_SNS_Topic.arn}"
policy = "${data.aws_iam_policy_document.Inspector_Remediation_SNS_Topic_Policy_Data.json}"
}
## Gives SNS permission to invoke the Vulnerability Patching Function
resource "aws_lambda_permission" "Inspector_Remediation_SNS_Lambda_Permission" {
statement_id = "AllowExecutionFromSNS"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.Lambda_Function_Inspector_Remediation.function_name}"
principal = "sns.amazonaws.com"
source_arn = "${aws_sns_topic.Inspector_Remediation_SNS_Topic.arn}"
}
## Subs Lambda to the SNS policy to get invoked
resource "aws_sns_topic_subscription" "Inspector_Remediation_SNS_Subscription" {
topic_arn = "${aws_sns_topic.Inspector_Remediation_SNS_Topic.arn}"
protocol = "lambda"
endpoint = "${aws_lambda_function.Lambda_Function_Inspector_Remediation.arn}"
}
## Creates an AWS Config Configuration Recorder -- this is a Regional resource and config must not have been enabled
## Recording Group configuration will support all resoruces types to include IAM policies, roles, etc
## The Name is actually abstracted from the user, it is only used by the API, will be "default" if not
resource "aws_config_configuration_recorder" "Config_Configuration_Recorder" {
name = "${var.Config_Configuration_Recorder_Name}"
role_arn = "${aws_iam_role.Config_IAM_Role.arn}"
recording_group = {
all_supported = true
include_global_resource_types = true
}
}
## Delivery Channel allows Config to emit Configuration History & State to S3
## SNS is optional but you can subscribe downstream services such as Lambda to it
resource "aws_config_delivery_channel" "Config_Configuration_Delivery_Channel" {
name = "${var.Config_Configuration_Delivery_Channel_Name}"
s3_bucket_name = "${aws_s3_bucket.Config_Artifacts_S3_Bucket.bucket}"
sns_topic_arn = "${aws_sns_topic.Config_SNS_Topic.id}"
}
## This Terraform Resource will turn the Configuration Recorder "on"
## Depends_On is passed to avoid a race condition
resource "aws_config_configuration_recorder_status" "Config_Configuration_Recorder_Status" {
name = "${aws_config_configuration_recorder.Config_Configuration_Recorder.name}"
is_enabled = true
depends_on = ["aws_config_delivery_channel.Config_Configuration_Delivery_Channel"]
}
## Config will publish near real time Configuration changes into SNS
## This SNS topic is encrypted via the KMS key set earlier -- any downstream services will also need access to it
resource "aws_sns_topic" "Config_SNS_Topic" {
name = "${var.Config_SNS_Topic_Name}"
kms_master_key_id = "${aws_kms_key.Config_Recorder_SNS_Customer_CMK.id}"
policy = <<POLICY
{
"Id": "Policy_ID",
"Statement": [
{
"Sid": "AWSConfigSNSPolicy",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.Config_IAM_Role.arn}"
},
"Action": "SNS:Publish",
"Resource": "arn:aws:sns::${data.aws_caller_identity.current.account_id}:${var.Config_SNS_Topic_Name}"
}
]
}
POLICY
}
## This is the S3 Bucket that Config will send the Config State files into
resource "aws_s3_bucket" "Config_Artifacts_S3_Bucket" {
bucket = "${var.Config_Artifacts_S3_Bucket_Name}"
acl = "private"
versioning {
enabled = true
}
logging {
target_bucket = "${aws_s3_bucket.Server_Access_Log_S3_Bucket.id}"
target_prefix = "configaccess/"
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
## sts Assume Role for config -- attaching the default config service policy via Data resoruce
resource "aws_iam_role" "Config_IAM_Role" {
name = "${var.Config_Configuration_Recorder_Name}-role"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "config.amazonaws.com"
},
"Effect": "Allow",
"Sid": "thisissid028129128"
}
]
}
POLICY
}
resource "aws_iam_role_policy_attachment" "Config_Role_Managed_Policy_Attachment" {
role = "${aws_iam_role.Config_IAM_Role.name}"
policy_arn = "${data.aws_iam_policy.Data_Policy_AWSConfigRole.arn}"
}
## Policy is needed for the Delivery Channel -- gives access to the Config S3 Bucket & SNS Topic
resource "aws_iam_role_policy" "Config_Role_Policy" {
name = "${var.Config_Configuration_Recorder_Name}-policy"
role = "${aws_iam_role.Config_IAM_Role.id}"
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1554678006121",
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"${aws_s3_bucket.Config_Artifacts_S3_Bucket.arn}",
"${aws_s3_bucket.Config_Artifacts_S3_Bucket.arn}/*"
]
},
{
"Sid": "Stmt1554678078717",
"Action": [
"sns:Publish"
],
"Effect": "Allow",
"Resource": "${aws_sns_topic.Config_SNS_Topic.id}"
}
]
}
POLICY
}
## This S3 Bucket will collect HTTP Access Logs from all other defined S3 Buckets
## Do not set access logging on itself otherwise you will have a large (and expensive) volume of logs in your bucket
resource "aws_s3_bucket" "Server_Access_Log_S3_Bucket" {
bucket = "${var.Server_Access_Log_S3_Bucket_Name}"
acl = "log-delivery-write"
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
## No arguments are support for the Security Hub account -- Terraform simply calls the API to enable it
## Config Recorder & Delivery Channel needs to be present in the account before Security Hub can be enabled programmatically
resource "aws_securityhub_account" "Security_Hub_Enabled" {}
## This SNS Topic is where the CloudWatch Alarms will publish their findings to regarding the CIS AWS Benchmarks rules defined by Config
## Both the Metric Filter & Alarms plus a subscribed-to SNS Topic are needed to achieve compliance
## CIS_Compliance_ prefixes for Terraform Resource names will be referenced multiple times -- this was the basis behind the ComplianceMachineDon'tStop project
## It is also encrypted by the Config SNS Key -- despite the name (Would be confusing either way since not all SNS topics in this file are encrypted)
resource "aws_sns_topic" "CIS_Compliance_Alerts_SNS_Topic" {
name = "${var.CIS_Compliance_Alerts_SNS_Topic_Name}"
kms_master_key_id = "${aws_kms_key.Config_Recorder_SNS_Customer_CMK.id}"
}
## This CloudWatch Logs Group is for CloudTrail to publish API Logs too, it is also called CIS Compliance since that is another CIS Benchmark
## It actually makes up 4 checks -- Encrypted, Logged, Validated, Global
resource "aws_cloudwatch_log_group" "CIS_Compliance_CloudWatch_LogsGroup" {
name = "${var.CIS_Compliance_CloudWatch_LogsGroup_Name}"
}
resource "aws_iam_role" "CloudWatch_LogsGroup_IAM_Role" {
name = "${var.CIS_Compliance_CloudWatch_LogsGroup_Name}-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy" "CIS_Compliance_CloudWatch_LogsGroup_Policy" {
name = "${var.CIS_Compliance_CloudWatch_LogsGroup_Name}-policy"
role = "${aws_iam_role.CloudWatch_LogsGroup_IAM_Role.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams"
],
"Effect": "Allow",
"Resource": "${aws_cloudwatch_log_group.CIS_Compliance_CloudWatch_LogsGroup.arn}*"
}
]
}
EOF
}
## This CloudTrail Trail is called CIS Compliance as it was made with the Security Hub CIS benchmarks in mind
## This Trail is Global (multi-regional) and has encrypion, validation and CloudWatch delivery configured to be in Compliance
## This Trail also logs all Object-level Data Events for Lambda & S3 for enhanced auditing capabilities
resource "aws_cloudtrail" "CIS_Compliance_CloudTrail_Trail" {
name = "${var.CIS_Compliance_CloudTrail_Trail_Name}"
s3_bucket_name = "${aws_s3_bucket.CIS_Compliance_CloudTrail_Logs_S3_Bucket.id}"
include_global_service_events = true
is_multi_region_trail = true
enable_log_file_validation = true
kms_key_id = "${aws_kms_key.CloudTrail_Customer_CMK.arn}"
cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.CIS_Compliance_CloudWatch_LogsGroup.arn}"
cloud_watch_logs_role_arn = "${aws_iam_role.CloudWatch_LogsGroup_IAM_Role.arn}"
event_selector {
read_write_type = "All"
include_management_events = true
data_resource {
type = "AWS::Lambda::Function"
values = ["arn:aws:lambda"]
}
}
event_selector {
read_write_type = "All"
include_management_events = true
data_resource {
type = "AWS::S3::Object"
values = ["arn:aws:s3:::"]
}
}
}
## CloudTrail at a minimum needs a S3 bucket to set the API Logs too
## The default created CloudTrail Bucket Policy is attached in-line of this Resource
resource "aws_s3_bucket" "CIS_Compliance_CloudTrail_Logs_S3_Bucket" {
bucket = "${var.CIS_Compliance_CloudTrail_Logs_S3_Bucket_Name}"
acl = "private"
versioning {
enabled = true
}
logging {
target_bucket = "${aws_s3_bucket.Server_Access_Log_S3_Bucket.id}"
target_prefix = "cloudtrailaccess/"
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::${var.CIS_Compliance_CloudTrail_Logs_S3_Bucket_Name}"
},
{
"Sid": "AWSCloudTrailWrite",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::${var.CIS_Compliance_CloudTrail_Logs_S3_Bucket_Name}/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}
]
}
POLICY
}
## These next resources are the CloudWatch Log Metric Filter & associated Alarms to be in compliance with CIS Benchmarks
resource "aws_cloudwatch_log_metric_filter" "CIS_Unauthorized_API_Calls_Metric_Filter" {
name = "CIS-UnauthorizedAPICalls"
pattern = "{ ($.errorCode = \"*UnauthorizedOperation\") || ($.errorCode = \"AccessDenied*\") }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-UnauthorizedAPICalls"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Unauthorized_API_Calls_CW_Alarm" {
alarm_name = "CIS-3.1-UnauthorizedAPICalls"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Unauthorized_API_Calls_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring unauthorized API calls will help reveal application errors and may reduce time to detect malicious activity."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_No_MFA_Console_Signin_Metric_Filter" {
name = "CIS-ConsoleSigninWithoutMFA"
pattern = "{ ($.eventName = \"ConsoleLogin\") && ($.additionalEventData.MFAUsed != \"Yes\") }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-ConsoleSigninWithoutMFA"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_No_MFA_Console_Signin_CW_Alarm" {
alarm_name = "CIS-3.2-ConsoleSigninWithoutMFA"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_No_MFA_Console_Signin_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring for single-factor console logins will increase visibility into accounts that are not protected by MFA."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Root_Account_Use_Metric_Filter" {
name = "CIS-RootAccountUsage"
pattern = "{ $.userIdentity.type = \"Root\" && $.userIdentity.invokedBy NOT EXISTS && $.eventType != \"AwsServiceEvent\" }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-RootAccountUsage"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Root_Account_Use_CW_Alarm" {
alarm_name = "CIS-3.3-RootAccountUsage"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Root_Account_Use_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring for root account logins will provide visibility into the use of a fully privileged account and an opportunity to reduce the use of it."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_IAM_Policy_Change_Metric_Filter" {
name = "CIS-IAMPolicyChanges"
pattern = "{($.eventName=DeleteGroupPolicy)||($.eventName=DeleteRolePolicy)||($.eventName=DeleteUserPolicy)||($.eventName=PutGroupPolicy)||($.eventName=PutRolePolicy)||($.eventName=PutUserPolicy)||($.eventName=CreatePolicy)||($.eventName=DeletePolicy)||($.eventName=CreatePolicyVersion)||($.eventName=DeletePolicyVersion)||($.eventName=AttachRolePolicy)||($.eventName=DetachRolePolicy)||($.eventName=AttachUserPolicy)||($.eventName=DetachUserPolicy)||($.eventName=AttachGroupPolicy)||($.eventName=DetachGroupPolicy)}"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-IAMPolicyChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_IAM_Policy_Change_CW_Alarm" {
alarm_name = "CIS-3.4-IAMPolicyChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_IAM_Policy_Change_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to IAM policies will help ensure authentication and authorization controls remain intact."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_CloudTrail_Config_Change_Metric_Filter" {
name = "CIS-CloudTrailChanges"
pattern = "{ ($.eventName = CreateTrail) || ($.eventName = UpdateTrail) || ($.eventName = DeleteTrail) || ($.eventName = StartLogging) || ($.eventName = StopLogging) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-CloudTrailChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_CloudTrail_Config_Change_CW_Alarm" {
alarm_name = "CIS-3.5-CloudTrailChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_CloudTrail_Config_Change_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to CloudTrail's configuration will help ensure sustained visibility to activities performed in the AWS account."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Console_AuthN_Failure_Metric_Filter" {
name = "CIS-ConsoleAuthenticationFailure"
pattern = "{ ($.eventName = ConsoleLogin) && ($.errorMessage = \"Failed authentication\") }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-ConsoleAuthenticationFailure"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Console_AuthN_Failure_CW_Alarm" {
alarm_name = "CIS-3.6-ConsoleAuthenticationFailure"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Console_AuthN_Failure_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring failed console logins may decrease lead time to detect an attempt to brute force a credential, which may provide an indicator, such as source IP, that can be used in other event correlation."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Disable_Or_Delete_CMK_Metric_Filter" {
name = "CIS-DisableOrDeleteCMK"
pattern = "{ ($.eventSource = kms.amazonaws.com) && (($.eventName = DisableKey) || ($.eventName = ScheduleKeyDeletion)) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-DisableOrDeleteCMK"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Disable_Or_Delete_CMK_CW_Alarm" {
alarm_name = "CIS-3.7-DisableOrDeleteCMK"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Disable_Or_Delete_CMK_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring failed console logins may decrease lead time to detect an attempt to brute force a credential, which may provide an indicator, such as source IP, that can be used in other event correlation."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_S3_Bucket_Policy_Change_Metric_Filter" {
name = "CIS-S3BucketPolicyChanges"
pattern = "{ ($.eventSource = s3.amazonaws.com) && (($.eventName = PutBucketAcl) || ($.eventName = PutBucketPolicy) || ($.eventName = PutBucketCors) || ($.eventName = PutBucketLifecycle) || ($.eventName = PutBucketReplication) || ($.eventName = DeleteBucketPolicy) || ($.eventName = DeleteBucketCors) || ($.eventName = DeleteBucketLifecycle) || ($.eventName = DeleteBucketReplication)) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-S3BucketPolicyChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_S3_Bucket_Policy_Change_CW_Alarm" {
alarm_name = "CIS-3.8-S3BucketPolicyChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_S3_Bucket_Policy_Change_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to S3 bucket policies may reduce time to detect and correct permissive policies on sensitive S3 buckets."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_AWS_Config_Change_Metric_Filter" {
name = "CIS-AWSConfigChanges"
pattern = "{ ($.eventSource = config.amazonaws.com) && (($.eventName=StopConfigurationRecorder)||($.eventName=DeleteDeliveryChannel)||($.eventName=PutDeliveryChannel)||($.eventName=PutConfigurationRecorder)) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-AWSConfigChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_AWS_Config_Change_CW_Alarm" {
alarm_name = "CIS-3.9-AWSConfigChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_AWS_Config_Change_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to AWS Config configuration will help ensure sustained visibility of configuration items within the AWS account."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Security_Group_Changes_Metric_Filter" {
name = "CIS-SecurityGroupChanges"
pattern = "{ ($.eventName = AuthorizeSecurityGroupIngress) || ($.eventName = AuthorizeSecurityGroupEgress) || ($.eventName = RevokeSecurityGroupIngress) || ($.eventName = RevokeSecurityGroupEgress) || ($.eventName = CreateSecurityGroup) || ($.eventName = DeleteSecurityGroup)}"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-SecurityGroupChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Security_Group_Changes_CW_Alarm" {
alarm_name = "CIS-3.10-SecurityGroupChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Security_Group_Changes_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to security group will help ensure that resources and services are not unintentionally exposed."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Network_ACL_Changes_Metric_Filter" {
name = "CIS-NetworkACLChanges"
pattern = "{ ($.eventName = CreateNetworkAcl) || ($.eventName = CreateNetworkAclEntry) || ($.eventName = DeleteNetworkAcl) || ($.eventName = DeleteNetworkAclEntry) || ($.eventName = ReplaceNetworkAclEntry) || ($.eventName = ReplaceNetworkAclAssociation) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-NetworkACLChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Network_ACL_Changes_CW_Alarm" {
alarm_name = "CIS-3.11-NetworkACLChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Network_ACL_Changes_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to NACLs will help ensure that AWS resources and services are not unintentionally exposed."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Network_Gateway_Changes_Metric_Filter" {
name = "CIS-NetworkGatewayChanges"
pattern = "{ ($.eventName = CreateCustomerGateway) || ($.eventName = DeleteCustomerGateway) || ($.eventName = AttachInternetGateway) || ($.eventName = CreateInternetGateway) || ($.eventName = DeleteInternetGateway) || ($.eventName = DetachInternetGateway) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-NetworkGatewayChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Network_Gateway_Changes_CW_Alarm" {
alarm_name = "CIS-3.12-NetworkGatewayChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Network_Gateway_Changes_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to network gateways will help ensure that all ingress/egress traffic traverses the VPC border via a controlled path."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_Route_Table_Changes_Metric_Filter" {
name = "CIS-RouteTableChanges"
pattern = "{ ($.eventName = CreateRoute) || ($.eventName = CreateRouteTable) || ($.eventName = ReplaceRoute) || ($.eventName = ReplaceRouteTableAssociation) || ($.eventName = DeleteRouteTable) || ($.eventName = DeleteRoute) || ($.eventName = DisassociateRouteTable) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-RouteTableChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_Route_Table_Changes_CW_Alarm" {
alarm_name = "CIS-3.13-RouteTableChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_Route_Table_Changes_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to route tables will help ensure that all VPC traffic flows through an expected path."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
resource "aws_cloudwatch_log_metric_filter" "CIS_VPC_Changes_Metric_Filter" {
name = "CIS-VPCChanges"
pattern = "{ ($.eventName = CreateVpc) || ($.eventName = DeleteVpc) || ($.eventName = ModifyVpcAttribute) || ($.eventName = AcceptVpcPeeringConnection) || ($.eventName = CreateVpcPeeringConnection) || ($.eventName = DeleteVpcPeeringConnection) || ($.eventName = RejectVpcPeeringConnection) || ($.eventName = AttachClassicLinkVpc) || ($.eventName = DetachClassicLinkVpc) || ($.eventName = DisableVpcClassicLink) || ($.eventName = EnableVpcClassicLink) }"
log_group_name = "${aws_cloudwatch_log_group.CISComplianceCloudWatchLogsGroupName.name}"
metric_transformation {
name = "CIS-VPCChanges"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
value = "1"
}
}
resource "aws_cloudwatch_metric_alarm" "CIS_VPC_Changes_CW_Alarm" {
alarm_name = "CIS-3.14-VPCChanges"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "${aws_cloudwatch_log_metric_filter.CIS_VPC_Changes_Metric_Filter.id}"
namespace = "${var.CIS_Metric_Alarm_Namespace}"
period = "300"
statistic = "Sum"
threshold = "1"
alarm_description = "Monitoring changes to VPC will help ensure that all VPC traffic flows through an expected path."
alarm_actions = ["${aws_sns_topic.CIS_Compliance_Alerts_SNS_Topic.arn}"]
insufficient_data_actions = []
}
## This is the end of the CIS Compliance Cloudwatch Alarms & Metric Filters section
## A crypto-office Group and associated IAM entities (policy, users, etc) were created to add something other than Root into the Key Adminstrators for the CMKs
## It is not in good practice to rely on Root for high level administrative tasks -- and key adminstration should be led by a crypto officer of some sort anyway
## If you know the term COMSEC Custodian -- this is essentially what it is
resource "aws_iam_group" "KMS_Key_Admin_IAM_Group" {
name = "${var.KMS_Key_Admin_IAM_Group_Name}"
}
resource "aws_iam_policy" "KMS_Key_Admin_IAM_Policy" {
name = "${var.KMS_Key_Admin_IAM_Group_Name}-policy"
path = "/"
description = "Allows Admin Privs for KMS to the KMS Key Admin Group - Managed by Terraform"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "kms:*",
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_user" "KMS_Key_Admin_IAM_User" {
name = "${var.KMS_Key_Admin_IAM_User_Name}"
}
resource "aws_iam_group_policy_attachment" "KMS_Key_Admin_Group_IAM_Policy_Attachment" {
group = "${aws_iam_group.KMS_Key_Admin_IAM_Group.name}"
policy_arn = "${aws_iam_policy.KMS_Key_Admin_IAM_Policy.arn}"
}
resource "aws_iam_group_membership" "KMS_Key_Admin_IAM_Group_Membership" {
name = "${var.KMS_Key_Admin_IAM_User_Name}-membership"
users = ["${aws_iam_user.KMS_Key_Admin_IAM_User.name}"]