From 7eded181e70516c3d20769dc4ec8d49b896c5804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AD=A6=E8=94=B5=E4=B8=80=E5=BC=98?= Date: Sun, 15 Nov 2026 01:42:07 +0900 Subject: [PATCH] Add CloudTrail delivery to CloudWatch Logs --- envs/prod/main.tf | 7 +++ .../apply-tests/osaka_apply.tftest.hcl | 19 ++++++ .../audit_logging_encryption/cloudtrail.tf | 8 ++- .../cloudtrail_cloudwatch.tf | 61 +++++++++++++++++++ modules/audit_logging_encryption/locals.tf | 46 ++++++++++++++ .../tests/osaka_plan.tftest.hcl | 19 ++++++ modules/audit_logging_encryption/variables.tf | 11 ++++ 7 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 modules/audit_logging_encryption/cloudtrail_cloudwatch.tf diff --git a/envs/prod/main.tf b/envs/prod/main.tf index 3f2f4b9..0d1f5da 100644 --- a/envs/prod/main.tf +++ b/envs/prod/main.tf @@ -83,6 +83,13 @@ module "audit_logging_encryption" { kms_alias_name = "alias/vault-kms" log_groups = local.log_groups + cloudtrail_cloudwatch_logs = { + log_group_name = "/aws/cloudtrail/management-events" + retention_in_days = 30 + role_name = "CloudTrail-CloudWatch-Logs-management-events" + policy_name = "CloudTrail-CloudWatch-Logs-management-events" + } + billing_alarm_name = "awsbilling-AWS-Service-Charges-total" billing_alarm_action_arns = ["arn:aws:sns:us-east-1:${var.account_id}:NotifyMe"] } diff --git a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl index 283ead5..7a84a8b 100644 --- a/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl +++ b/modules/audit_logging_encryption/apply-tests/osaka_apply.tftest.hcl @@ -63,6 +63,25 @@ run "apply_group1_osaka" { error_message = "The Osaka test CloudTrail trail must be single-Region." } + assert { + condition = ( + length(aws_cloudwatch_log_group.cloudtrail_protected) == 0 && + length(aws_cloudwatch_log_group.cloudtrail_unprotected) == 1 && + aws_cloudwatch_log_group.cloudtrail_unprotected[0].retention_in_days == 30 + ) + error_message = "The Osaka test must create one unprotected CloudTrail log group with 30-day retention." + } + + assert { + condition = ( + length(aws_iam_role.cloudtrail_cloudwatch_protected) == 0 && + length(aws_iam_role.cloudtrail_cloudwatch_unprotected) == 1 && + length(aws_iam_role_policy.cloudtrail_cloudwatch_protected) == 0 && + length(aws_iam_role_policy.cloudtrail_cloudwatch_unprotected) == 1 + ) + error_message = "The Osaka test must use unprotected CloudTrail delivery IAM resources." + } + assert { condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7 error_message = "The Osaka test KMS key must use the minimum seven-day deletion window." diff --git a/modules/audit_logging_encryption/cloudtrail.tf b/modules/audit_logging_encryption/cloudtrail.tf index 37f99ee..3b289f2 100644 --- a/modules/audit_logging_encryption/cloudtrail.tf +++ b/modules/audit_logging_encryption/cloudtrail.tf @@ -6,6 +6,8 @@ resource "aws_cloudtrail" "management_events" { is_organization_trail = false enable_log_file_validation = true enable_logging = true + cloud_watch_logs_group_arn = "${local.cloudtrail_log_group_arn}:*" + cloud_watch_logs_role_arn = local.cloudtrail_cloudwatch_role_arn advanced_event_selector { name = "Management events selector" @@ -16,5 +18,9 @@ resource "aws_cloudtrail" "management_events" { } } - depends_on = [aws_s3_bucket_policy.this["cloudtrail"]] + depends_on = [ + aws_s3_bucket_policy.this["cloudtrail"], + aws_iam_role_policy.cloudtrail_cloudwatch_protected, + aws_iam_role_policy.cloudtrail_cloudwatch_unprotected, + ] } diff --git a/modules/audit_logging_encryption/cloudtrail_cloudwatch.tf b/modules/audit_logging_encryption/cloudtrail_cloudwatch.tf new file mode 100644 index 0000000..51580b5 --- /dev/null +++ b/modules/audit_logging_encryption/cloudtrail_cloudwatch.tf @@ -0,0 +1,61 @@ +resource "aws_cloudwatch_log_group" "cloudtrail_protected" { + count = var.protect_from_destroy ? 1 : 0 + + name = local.cloudtrail_log_group_name + retention_in_days = var.cloudtrail_cloudwatch_logs.retention_in_days + log_group_class = "STANDARD" + + lifecycle { + prevent_destroy = true + } +} + +resource "aws_cloudwatch_log_group" "cloudtrail_unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + name = local.cloudtrail_log_group_name + retention_in_days = var.cloudtrail_cloudwatch_logs.retention_in_days + log_group_class = "STANDARD" +} + +resource "aws_iam_role" "cloudtrail_cloudwatch_protected" { + count = var.protect_from_destroy ? 1 : 0 + + name = local.cloudtrail_cloudwatch_role_config_name + path = "/service-role/" + max_session_duration = 3600 + assume_role_policy = local.cloudtrail_cloudwatch_assume_role_policy + + lifecycle { + prevent_destroy = true + } +} + +resource "aws_iam_role" "cloudtrail_cloudwatch_unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + name = local.cloudtrail_cloudwatch_role_config_name + path = "/service-role/" + max_session_duration = 3600 + assume_role_policy = local.cloudtrail_cloudwatch_assume_role_policy +} + +resource "aws_iam_role_policy" "cloudtrail_cloudwatch_protected" { + count = var.protect_from_destroy ? 1 : 0 + + name = local.cloudtrail_cloudwatch_policy_name + role = local.cloudtrail_cloudwatch_role_name + policy = local.cloudtrail_cloudwatch_role_policy + + lifecycle { + prevent_destroy = true + } +} + +resource "aws_iam_role_policy" "cloudtrail_cloudwatch_unprotected" { + count = var.protect_from_destroy ? 0 : 1 + + name = local.cloudtrail_cloudwatch_policy_name + role = local.cloudtrail_cloudwatch_role_name + policy = local.cloudtrail_cloudwatch_role_policy +} diff --git a/modules/audit_logging_encryption/locals.tf b/modules/audit_logging_encryption/locals.tf index e55896f..7f4a678 100644 --- a/modules/audit_logging_encryption/locals.tf +++ b/modules/audit_logging_encryption/locals.tf @@ -21,6 +21,52 @@ locals { trail_arn = "arn:aws:cloudtrail:${var.region}:${var.account_id}:trail/${var.trail_name}" + cloudtrail_log_group_name = coalesce( + var.cloudtrail_cloudwatch_logs.log_group_name, + "${var.name_prefix}-cloudtrail", + ) + cloudtrail_cloudwatch_role_config_name = coalesce( + var.cloudtrail_cloudwatch_logs.role_name, + "${var.name_prefix}-cloudtrail-cloudwatch", + ) + cloudtrail_cloudwatch_policy_name = coalesce( + var.cloudtrail_cloudwatch_logs.policy_name, + "${var.name_prefix}-cloudtrail-cloudwatch", + ) + + cloudtrail_log_group_arn = var.protect_from_destroy ? aws_cloudwatch_log_group.cloudtrail_protected[0].arn : aws_cloudwatch_log_group.cloudtrail_unprotected[0].arn + cloudtrail_cloudwatch_role_arn = var.protect_from_destroy ? aws_iam_role.cloudtrail_cloudwatch_protected[0].arn : aws_iam_role.cloudtrail_cloudwatch_unprotected[0].arn + cloudtrail_cloudwatch_role_name = var.protect_from_destroy ? aws_iam_role.cloudtrail_cloudwatch_protected[0].name : aws_iam_role.cloudtrail_cloudwatch_unprotected[0].name + + cloudtrail_cloudwatch_assume_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [{ + Effect = "Allow" + Principal = { + Service = "cloudtrail.amazonaws.com" + } + Action = "sts:AssumeRole" + }] + }) + + cloudtrail_cloudwatch_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Sid = "AWSCloudTrailCreateLogStream20141101" + Effect = "Allow" + Action = "logs:CreateLogStream" + Resource = "${local.cloudtrail_log_group_arn}:log-stream:${var.account_id}_CloudTrail_*" + }, + { + Sid = "AWSCloudTrailPutLogEvents20141101" + Effect = "Allow" + Action = "logs:PutLogEvents" + Resource = "${local.cloudtrail_log_group_arn}:log-stream:${var.account_id}_CloudTrail_*" + }, + ] + }) + kms_policy = jsonencode({ Version = "2012-10-17" Id = "key-consolepolicy-3" diff --git a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl index 488e24d..6daa238 100644 --- a/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl +++ b/modules/audit_logging_encryption/tests/osaka_plan.tftest.hcl @@ -60,6 +60,25 @@ run "plan_group1_osaka" { error_message = "The Osaka test CloudTrail trail must be single-Region." } + assert { + condition = ( + length(aws_cloudwatch_log_group.cloudtrail_protected) == 0 && + length(aws_cloudwatch_log_group.cloudtrail_unprotected) == 1 && + aws_cloudwatch_log_group.cloudtrail_unprotected[0].retention_in_days == 30 + ) + error_message = "The Osaka test must create one unprotected CloudTrail log group with 30-day retention." + } + + assert { + condition = ( + length(aws_iam_role.cloudtrail_cloudwatch_protected) == 0 && + length(aws_iam_role.cloudtrail_cloudwatch_unprotected) == 1 && + length(aws_iam_role_policy.cloudtrail_cloudwatch_protected) == 0 && + length(aws_iam_role_policy.cloudtrail_cloudwatch_unprotected) == 1 + ) + error_message = "The Osaka test must use unprotected CloudTrail delivery IAM resources." + } + assert { condition = aws_kms_key.vault_unprotected[0].deletion_window_in_days == 7 error_message = "The Osaka test KMS key must use the minimum seven-day deletion window." diff --git a/modules/audit_logging_encryption/variables.tf b/modules/audit_logging_encryption/variables.tf index 263f554..00a8258 100644 --- a/modules/audit_logging_encryption/variables.tf +++ b/modules/audit_logging_encryption/variables.tf @@ -62,6 +62,17 @@ variable "cloudtrail_is_multi_region" { default = true } +variable "cloudtrail_cloudwatch_logs" { + description = "CloudWatch Logs delivery settings for the CloudTrail trail. Omitted names are derived from name_prefix for isolated tests." + type = object({ + log_group_name = optional(string) + retention_in_days = optional(number, 30) + role_name = optional(string) + policy_name = optional(string) + }) + default = {} +} + variable "kms_alias_name" { description = "Alias of the customer managed Vault key." type = string