section-2_1.tf.TODO 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # TODO
  2. # this needs to be split into two modules:
  3. # 1) Set up the centralized key and s3 bucket
  4. # 2) Set up the logging from the client
  5. data "aws_caller_identity" "current" {}
  6. data "template_file" "cloudtrail_kms" {
  7. template = file("${path.module}/templates/cloudtrail_kms_policy.json.tpl")
  8. vars {
  9. aws_account_id = data.aws_caller_identity.current.account_id
  10. }
  11. }
  12. resource "aws_kms_key" "cloudtrail" {
  13. description = "Encrypt/Decrypt cloudtrail logs"
  14. deletion_window_in_days = 30
  15. is_enabled = true
  16. enable_key_rotation = true
  17. policy = var.cloudtrail_kms_policy != "" ? var.cloudtrail_kms_policy : data.template_file.cloudtrail_kms.rendered}
  18. tags = merge(var.standard_tags, var.tags)
  19. }
  20. resource "aws_kms_alias" "cloudtrail" {
  21. name = "alias/${var.resource_name_prefix}-cloudtrail"
  22. target_key_id = "${aws_kms_key.cloudtrail.key_id}"
  23. }
  24. resource "aws_s3_bucket" "dps-mdr-cloudtrail" {
  25. bucket = "${lookup(local.workspace-dps-s3-cloudtrail-bucket,terraform.workspace,"")}"
  26. acl = "private"
  27. region = "us-east-1"
  28. policy = "${file("${path.module}/templates/${lookup(local.workspace-dps-s3-cloudtrail-bucket-policy,terraform.workspace,"")}")}"
  29. tags {
  30. Billing = "MSSP - MSOC Infrastrucutre"
  31. }
  32. }
  33. resource "aws_cloudtrail" "cloudtrail" {
  34. name = "${var.resource_name_prefix}-trail"
  35. s3_bucket_name = "${lookup(local.workspace-dps-s3-cloudtrail-bucket,terraform.workspace,"")}"
  36. is_multi_region_trail = true
  37. include_global_service_events = true
  38. enable_log_file_validation = true
  39. kms_key_id = "${aws_kms_key.cloudtrail.arn}"
  40. cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.aws-cis-logs.arn}"
  41. cloud_watch_logs_role_arn = "${aws_iam_role.cloudtrail_cloudwatchlogs_role.arn}"
  42. event_selector {
  43. read_write_type = "${var.clodtrail_event_selector_type}"
  44. include_management_events = true
  45. data_resource {
  46. type = "AWS::S3::Object"
  47. values = ["arn:aws:s3"]
  48. }
  49. data_resource {
  50. type = "AWS::Lambda::Function"
  51. values = ["arn:aws:lambda"]
  52. }
  53. }
  54. tags = merge(var.standard_tags, var.tags)
  55. }
  56. # CloudTrail check
  57. ## IAM Policy
  58. data "template_file" "cloudtrail_status_check_policy" {
  59. template = "${file("${path.module}/templates/lambda_cloudtrail_status_check_policy.json.tpl")}"
  60. }
  61. resource "aws_iam_role" "cloudtrail_status_check" {
  62. provider = "aws.iam_admin"
  63. name = "${var.resource_name_prefix}-cloudtrail-status-check"
  64. assume_role_policy = "${data.template_file.iam_lambda_assume_role_policy.rendered}"
  65. }
  66. resource "aws_iam_role_policy" "cloudtrail_status_check" {
  67. provider = "aws.iam_admin"
  68. name = "${var.resource_name_prefix}-lambda-cloudtrail-status-check"
  69. role = "${aws_iam_role.cloudtrail_status_check.id}"
  70. policy = "${data.template_file.cloudtrail_status_check_policy.rendered}"
  71. }
  72. ## /IAM Policy
  73. ## Create the function
  74. data "archive_file" "cloudtrail_status_check" {
  75. type = "zip"
  76. source_file = "${path.module}/files/cloudtrail_status_check.py"
  77. output_path = "${var.temp_artifacts_dir}/cloudtrail_status_check.zip"
  78. }
  79. resource "aws_lambda_function" "cloudtrail_status_check" {
  80. filename = "${var.temp_artifacts_dir}/cloudtrail_status_check.zip"
  81. function_name = "${var.resource_name_prefix}-cloudtrail-status-check"
  82. role = "${aws_iam_role.cloudtrail_status_check.arn}"
  83. handler = "cloudtrail_status_check.lambda_handler"
  84. source_code_hash = "${data.archive_file.cloudtrail_status_check.output_base64sha256}"
  85. runtime = "python2.7"
  86. timeout = "${var.lambda_timeout}"
  87. tags = merge(var.standard_tags, var.tags)
  88. }
  89. ## /Create the function
  90. ## Schedule the lambda function
  91. resource "aws_cloudwatch_event_rule" "cloudtrail_status_check" {
  92. name = "${var.resource_name_prefix}-cloudtrail-status-check"
  93. description = "remove expiring access keys"
  94. schedule_expression = "${var.lambda_cron_schedule}"
  95. }
  96. resource "aws_cloudwatch_event_target" "cloudtrail_status_check" {
  97. rule = "${aws_cloudwatch_event_rule.cloudtrail_status_check.name}"
  98. target_id = "${var.resource_name_prefix}-cloudtrail-status-check"
  99. arn = "${aws_lambda_function.cloudtrail_status_check.arn}"
  100. }
  101. resource "aws_lambda_permission" "cloudtrail_status_check" {
  102. statement_id = "AllowExecutionFromCloudWatch"
  103. action = "lambda:InvokeFunction"
  104. function_name = "${aws_lambda_function.cloudtrail_status_check.function_name}"
  105. principal = "events.amazonaws.com"
  106. source_arn = "${aws_cloudwatch_event_rule.cloudtrail_status_check.arn}"
  107. }
  108. ## /Schedule the lambda function
  109. # /# CloudTrail check