Skip to content

Commit db8cde0

Browse files
goruhacoderabbitai[bot]github-actions[bot]
authored
Fix cloudtrail s3 bucket name (#82)
* Update S3 bucket resources and add cloudtrail_label module * Add query_override variable for Datadog archive Added a new variable 'query_override' for customizable query in Datadog archive. * Refactor query assignment in main.tf * Update src/variables.tf Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * Update query_override variable type and nullability Changed query_override variable type to string and made it nullable. * Apply suggestion from @github-actions[bot] Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Update main.tf --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 9767707 commit db8cde0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/main.tf

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ locals {
2626
ordered_ids = concat(local.non_catchall_ids, local.catchall_id)
2727

2828
policy = local.enabled ? jsondecode(data.aws_iam_policy_document.default[0].json) : null
29+
30+
# default datadog_logs_archive query.
31+
default_query = join(" OR ", concat([join(":", ["env", var.stage]), join(":", ["account", local.aws_account_id])], var.additional_query_tags))
32+
query = var.query_override == null ? local.default_query : var.query_override
2933
}
3034

3135
# We use the http data source due to lack of a data source for datadog_logs_archive_order
@@ -61,7 +65,7 @@ data "aws_iam_policy_document" "default" {
6165
]
6266

6367
resources = [
64-
"arn:${local.aws_partition}:s3:::${module.this.id}-cloudtrail",
68+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}",
6569
]
6670
}
6771

@@ -80,7 +84,7 @@ data "aws_iam_policy_document" "default" {
8084
]
8185

8286
resources = [
83-
"arn:${local.aws_partition}:s3:::${module.this.id}-cloudtrail/*",
87+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}/*",
8488
]
8589

8690
condition {
@@ -115,7 +119,7 @@ data "aws_iam_policy_document" "default" {
115119
]
116120

117121
resources = [
118-
"arn:${local.aws_partition}:s3:::${module.this.id}-cloudtrail/*",
122+
"arn:${local.aws_partition}:s3:::${module.cloudtrail_label.id}/*",
119123
]
120124

121125
condition {
@@ -216,6 +220,15 @@ module "archive_bucket" {
216220
context = module.this.context
217221
}
218222

223+
module "cloudtrail_label" {
224+
source = "cloudposse/label/null"
225+
version = "0.25.0" # requires Terraform >= 0.13.0
226+
227+
name = "datadog-logs-archive-cloudtrail"
228+
context = module.this.context
229+
}
230+
231+
219232
module "cloudtrail_s3_bucket" {
220233
source = "cloudposse/s3-bucket/aws"
221234
version = "4.10.0"
@@ -224,7 +237,6 @@ module "cloudtrail_s3_bucket" {
224237

225238
count = local.enabled ? 1 : 0
226239

227-
name = "datadog-logs-archive-cloudtrail"
228240
acl = "private"
229241
enabled = local.enabled
230242
force_destroy = var.s3_force_destroy
@@ -282,7 +294,7 @@ module "cloudtrail_s3_bucket" {
282294
# https://github.com/hashicorp/terraform/issues/5613
283295
allow_ssl_requests_only = false
284296

285-
context = module.this.context
297+
context = module.cloudtrail_label.context
286298
}
287299

288300
module "cloudtrail" {
@@ -330,7 +342,7 @@ resource "datadog_logs_archive" "logs_archive" {
330342
name = var.stage
331343
include_tags = true
332344
rehydration_tags = ["rehydrated:true"]
333-
query = join(" OR ", concat([join(":", ["env", var.stage]), join(":", ["account", local.aws_account_id])], var.additional_query_tags))
345+
query = local.query
334346

335347
s3_archive {
336348
bucket = module.archive_bucket[0].bucket_id

src/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ variable "region" {
33
description = "AWS Region"
44
}
55

6+
variable "query_override" {
7+
type = string
8+
nullable = true
9+
description = "Override query for datadog archive. If null would be used query 'env:{stage} OR account:{aws account id} OR {additional_query_tags}'"
10+
}
11+
612
variable "additional_query_tags" {
713
type = list(any)
814
description = "Additional tags to be used in the query for this archive"

0 commit comments

Comments
 (0)