generated from cloudposse-terraform-components/template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add KMS encryption support for CloudTrail logs #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milldr
merged 3 commits into
cloudposse-terraform-components:main
from
johncblandii:feat/PP-51-datadog-logs-archive
Nov 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # KMS key policy document for CloudTrail encryption | ||
| data "aws_iam_policy_document" "cloudtrail_kms_key_policy" { | ||
| count = local.enabled && var.cloudtrail_enable_kms_encryption && var.cloudtrail_create_kms_key && var.cloudtrail_kms_key_arn == null ? 1 : 0 | ||
|
|
||
| # Enable IAM User Permissions | ||
| statement { | ||
| sid = "Enable IAM User Permissions" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["arn:${local.aws_partition}:iam::${local.aws_account_id}:root"] | ||
| } | ||
| actions = [ | ||
| "kms:*" | ||
| ] | ||
| resources = ["*"] | ||
| } | ||
|
|
||
| # Allow CloudTrail to encrypt logs | ||
| statement { | ||
| sid = "Allow CloudTrail to encrypt logs" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com"] | ||
| } | ||
| actions = [ | ||
| "kms:GenerateDataKey*" | ||
| ] | ||
| resources = ["*"] | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "aws:SourceArn" | ||
| values = ["arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/${module.this.id}"] | ||
| } | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| values = ["arn:${local.aws_partition}:cloudtrail:${var.region}:${local.aws_account_id}:trail/${module.this.id}"] | ||
| } | ||
| } | ||
|
|
||
| # Allow CloudTrail to describe key | ||
| statement { | ||
| sid = "Allow CloudTrail to describe key" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com"] | ||
| } | ||
| actions = [ | ||
| "kms:DescribeKey" | ||
| ] | ||
| resources = ["*"] | ||
| } | ||
|
|
||
| # Allow principals in the account to decrypt log files | ||
| statement { | ||
| sid = "Allow principals in the account to decrypt log files" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["arn:${local.aws_partition}:iam::${local.aws_account_id}:root"] | ||
| } | ||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:ReEncryptFrom" | ||
| ] | ||
| resources = ["*"] | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:CallerAccount" | ||
| values = [local.aws_account_id] | ||
| } | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| values = ["arn:${local.aws_partition}:cloudtrail:*:${local.aws_account_id}:trail/*"] | ||
| } | ||
| } | ||
|
|
||
| # Allow alias creation during resource creation | ||
| statement { | ||
| sid = "Allow alias creation during resource creation" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["arn:${local.aws_partition}:iam::${local.aws_account_id}:root"] | ||
| } | ||
| actions = [ | ||
| "kms:CreateAlias" | ||
| ] | ||
| resources = ["*"] | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:CallerAccount" | ||
| values = [local.aws_account_id] | ||
| } | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:ViaService" | ||
| values = ["ec2.${var.region}.amazonaws.com"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # KMS key for CloudTrail encryption | ||
| resource "aws_kms_key" "cloudtrail" { | ||
| count = local.enabled && var.cloudtrail_enable_kms_encryption && var.cloudtrail_create_kms_key && var.cloudtrail_kms_key_arn == null ? 1 : 0 | ||
|
|
||
| description = "KMS key for CloudTrail log encryption - ${module.this.id}" | ||
| deletion_window_in_days = var.cloudtrail_kms_key_deletion_window_in_days | ||
| enable_key_rotation = var.cloudtrail_kms_key_enable_rotation | ||
| policy = data.aws_iam_policy_document.cloudtrail_kms_key_policy[0].json | ||
|
|
||
| tags = merge( | ||
| module.this.tags, | ||
| { | ||
| Name = "${module.this.id}-cloudtrail" | ||
| managed-by = "terraform" | ||
| env = var.stage | ||
| service = "datadog-logs-archive" | ||
| part-of = "observability" | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| # KMS key alias for easier identification | ||
| resource "aws_kms_alias" "cloudtrail" { | ||
| count = local.enabled && var.cloudtrail_enable_kms_encryption && var.cloudtrail_create_kms_key && var.cloudtrail_kms_key_arn == null ? 1 : 0 | ||
|
|
||
| name = "alias/${module.this.id}-cloudtrail" | ||
| target_key_id = aws_kms_key.cloudtrail[0].key_id | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.