Skip to content

Commit ee14ba9

Browse files
feat: enable cross account kms (#250)
* Support an S3 bucket in a different account Allow principals in the S3 bucket's account to use the KMS Key to decrypt logs. S3 itself cannot tell us which account an arbitrary bucket is in, so the caller must provide it. Useful when using an Organization CloudTrail and a dedicated AWS account for logs, as recommended by [AWS Multi-account strategy](https://docs.aws.amazon.com/whitepapers/latest/organizing-your-aws-environment/organizing-your-aws-environment.html). * Output the KMS Key ARN So it can be used in for a bucket policy or similar. --------- Co-authored-by: Dan Russell <[email protected]>
1 parent d967a44 commit ee14ba9

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ previous invocations of the module prior to upgrading the version.
6767
| key_deletion_window_in_days | Duration in days after which the key is deleted after destruction of the resource, must be 7-30 days. Default 30 days. | `string` | `30` | no |
6868
| log_retention_days | Number of days to keep AWS logs around in specific log group. | `string` | `90` | no |
6969
| org_trail | Whether or not this is an organization trail. Only valid in master account. | `string` | `"false"` | no |
70+
| s3_bucket_account_id | (optional) The AWS account ID which owns the S3 bucket. Only include if the S3 bucket is in a different account than the CloudTrail. | `string` | `null` | no |
7071
| s3_key_prefix | S3 key prefix for CloudTrail logs | `string` | `"cloudtrail"` | no |
7172
| sns_topic_arn | ARN of the SNS topic for notification of log file delivery. | `string` | `""` | no |
7273
| tags | A mapping of tags to CloudTrail resources. | `map(string)` | `{}` | no |
@@ -79,6 +80,7 @@ previous invocations of the module prior to upgrading the version.
7980
| cloudtrail_arn | CloudTrail ARN |
8081
| cloudtrail_home_region | CloudTrail Home Region |
8182
| cloudtrail_id | CloudTrail ID |
83+
| kms_key_arn | KMS Key ARN |
8284
<!-- END_TF_DOCS -->
8385

8486
## Developer Setup

main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ data "aws_caller_identity" "current" {}
77
# The AWS partition (commercial or govcloud)
88
data "aws_partition" "current" {}
99

10+
locals {
11+
s3_bucket_account_id = var.s3_bucket_account_id != null ? var.s3_bucket_account_id : data.aws_caller_identity.current.account_id
12+
}
13+
1014
#
1115
# CloudTrail - CloudWatch
1216
#
@@ -191,7 +195,7 @@ data "aws_iam_policy_document" "cloudtrail_kms_policy_doc" {
191195
condition {
192196
test = "StringEquals"
193197
variable = "kms:CallerAccount"
194-
values = [data.aws_caller_identity.current.account_id]
198+
values = [local.s3_bucket_account_id]
195199
}
196200

197201
condition {

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ output "cloudtrail_id" {
1212
description = "CloudTrail ID"
1313
value = aws_cloudtrail.main.id
1414
}
15+
16+
output "kms_key_arn" {
17+
description = "KMS Key ARN"
18+
value = aws_kms_key.cloudtrail.arn
19+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ variable "s3_bucket_name" {
2121
type = string
2222
}
2323

24+
variable "s3_bucket_account_id" {
25+
description = "(optional) The AWS account ID which owns the S3 bucket. Only include if the S3 bucket is in a different account than the CloudTrail."
26+
default = null
27+
type = string
28+
}
29+
2430
variable "org_trail" {
2531
description = "Whether or not this is an organization trail. Only valid in master account."
2632
default = "false"

0 commit comments

Comments
 (0)