Skip to content

Commit e331262

Browse files
author
sourabh
committed
updating CloudTrail to add condition for IAM role and SNS topic. This is required in order to make sure terraform can create resources in 'terraform plan'. issue - https://discuss.hashicorp.com/t/for-each-value-depends-on-resource-attributes-that-cannot-be-determined-until-apply/6061
1 parent a96459e commit e331262

File tree

6 files changed

+66
-30
lines changed

6 files changed

+66
-30
lines changed

aws/cloudtrail/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ This module is used to create AWS and Sumo Logic resource to collect CloudTrail
3535
| collector\_details | Provide details for the Sumo Logic collector. If not provided, then defaults will be used. | <pre>object({<br> collector_name = string<br> description = string<br> fields = map(string)<br> })</pre> | <pre>{<br> "collector_name": "SumoLogic CloudTrail Collector <Random ID>",<br> "description": "This collector is created using Sumo Logic terraform AWS cloudtrail module to collect AWS cloudtrail logs.",<br> "fields": {}<br>}</pre> | no |
3636
| create\_collector | Provide "true" if you would like to create the Sumo Logic Collector. | `bool` | n/a | yes |
3737
| create\_trail | Provide "true" if you would like to create the AWS CloudTrail. If the bucket is created by the module, module by default creates the AWS cloudtrail. | `bool` | n/a | yes |
38-
| source\_details | Provide details for the Sumo Logic CloudTrail source. If not provided, then defaults will be used. | <pre>object({<br> source_name = string<br> source_category = string<br> collector_id = string<br> description = string<br> bucket_details = object({<br> create_bucket = bool<br> bucket_name = string<br> path_expression = string<br> force_destroy_bucket = bool<br> })<br> paused = bool<br> scan_interval = string<br> sumo_account_id = number<br> cutoff_relative_time = string<br> fields = map(string)<br> iam_role_arn = string<br> sns_topic_arn = string<br> })</pre> | <pre>{<br> "bucket_details": {<br> "bucket_name": "cloudtrail-logs-random-id",<br> "create_bucket": true,<br> "force_destroy_bucket": true,<br> "path_expression": "AWSLogs/<ACCOUNT-ID>/CloudTrail/<REGION-NAME>/*"<br> },<br> "collector_id": "",<br> "cutoff_relative_time": "-1d",<br> "description": "This source is created using Sumo Logic terraform AWS cloudtrail module to collect AWS cloudtrail logs.",<br> "fields": {},<br> "iam_role_arn": "",<br> "paused": false,<br> "scan_interval": 300000,<br> "sns_topic_arn": "",<br> "source_category": "Labs/aws/cloudtrail",<br> "source_name": "CloudTrail Source",<br> "sumo_account_id": 926226587429<br>}</pre> | no |
38+
| source\_details | Provide details for the Sumo Logic CloudTrail source. If not provided, then defaults will be used. | <pre>object({<br> source_name = string<br> source_category = string<br> collector_id = string<br> description = string<br> bucket_details = object({<br> create_bucket = bool<br> bucket_name = string<br> path_expression = string<br> force_destroy_bucket = bool<br> })<br> paused = bool<br> scan_interval = string<br> sumo_account_id = number<br> cutoff_relative_time = string<br> fields = map(string)<br> iam_details = object({<br> create_iam_role = bool<br> iam_role_arn = string<br> })<br> sns_topic_details = object({<br> create_sns_topic = bool<br> sns_topic_arn = string<br> })<br> })</pre> | <pre>{<br> "bucket_details": {<br> "bucket_name": "cloudtrail-logs-random-id",<br> "create_bucket": true,<br> "force_destroy_bucket": true,<br> "path_expression": "AWSLogs/<ACCOUNT-ID>/CloudTrail/<REGION-NAME>/*"<br> },<br> "collector_id": "",<br> "cutoff_relative_time": "-1d",<br> "description": "This source is created using Sumo Logic terraform AWS cloudtrail module to collect AWS cloudtrail logs.",<br> "fields": {},<br> "iam_details": {<br> "create_iam_role": true,<br> "iam_role_arn": null<br> },<br> "paused": false,<br> "scan_interval": 300000,<br> "sns_topic_details": {<br> "create_sns_topic": true,<br> "sns_topic_arn": null<br> },<br> "source_category": "Labs/aws/cloudtrail",<br> "source_name": "CloudTrail Source",<br> "sumo_account_id": 926226587429<br>}</pre> | no |
3939
| sumologic\_organization\_id | Appears on the Account Overview page that displays information about your Sumo Logic organization. Used for IAM Role in Sumo Logic AWS Sources. | `string` | n/a | yes |
4040

4141
## Outputs

aws/cloudtrail/cloudtrail.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ resource "aws_s3_bucket" "s3_bucket" {
2424
}
2525

2626
resource "aws_sns_topic" "sns_topic" {
27-
for_each = toset(local.create_sns_topic ? ["sns_topic"] : [])
27+
for_each = toset(var.source_details.sns_topic_details.create_sns_topic ? ["sns_topic"] : [])
2828

2929
name = "SumoLogic-Terraform-CloudTrail-Module-${random_string.aws_random.id}"
3030
policy = templatefile("${path.module}/templates/sns_topic_policy.tmpl", {
@@ -36,7 +36,7 @@ resource "aws_sns_topic" "sns_topic" {
3636
}
3737

3838
resource "aws_s3_bucket_notification" "bucket_notification" {
39-
for_each = toset(local.create_sns_topic && var.source_details.bucket_details.create_bucket ? ["bucket_notification"] : [])
39+
for_each = toset(var.source_details.sns_topic_details.create_sns_topic && var.source_details.bucket_details.create_bucket ? ["bucket_notification"] : [])
4040

4141
bucket = aws_s3_bucket.s3_bucket["s3_bucket"].id
4242

@@ -57,7 +57,7 @@ resource "aws_cloudtrail" "cloudtrail" {
5757
}
5858

5959
resource "aws_iam_role" "source_iam_role" {
60-
for_each = toset(local.create_iam_role ? ["source_iam_role"] : [])
60+
for_each = toset(var.source_details.iam_details.create_iam_role ? ["source_iam_role"] : [])
6161

6262
name = "SumoLogic-Terraform-CloudTrail-Module-${random_string.aws_random.id}"
6363
path = "/"
@@ -72,7 +72,7 @@ resource "aws_iam_role" "source_iam_role" {
7272
}
7373

7474
resource "aws_iam_policy" "iam_policy" {
75-
for_each = toset(local.create_iam_role ? ["iam_policy"] : [])
75+
for_each = toset(var.source_details.iam_details.create_iam_role ? ["iam_policy"] : [])
7676

7777
name = "SumoLogicCloudTrailSource-${random_string.aws_random.id}"
7878
policy = templatefile("${path.module}/templates/sumologic_source_policy.tmpl", {
@@ -111,7 +111,7 @@ resource "sumologic_cloudtrail_source" "source" {
111111
scan_interval = var.source_details.scan_interval
112112
authentication {
113113
type = "AWSRoleBasedAuthentication"
114-
role_arn = local.create_iam_role ? aws_iam_role.source_iam_role["source_iam_role"].arn : var.source_details.iam_role_arn
114+
role_arn = var.source_details.iam_details.create_iam_role ? aws_iam_role.source_iam_role["source_iam_role"].arn : var.source_details.iam_details.iam_role_arn
115115
}
116116

117117
path {
@@ -139,5 +139,5 @@ resource "aws_sns_topic_subscription" "subscription" {
139139
endpoint = sumologic_cloudtrail_source.source.url
140140
endpoint_auto_confirms = true
141141
protocol = "https"
142-
topic_arn = local.create_sns_topic ? aws_sns_topic.sns_topic["sns_topic"].arn : var.source_details.sns_topic_arn
142+
topic_arn = var.source_details.sns_topic_details.create_sns_topic ? aws_sns_topic.sns_topic["sns_topic"].arn : var.source_details.sns_topic_details.sns_topic_arn
143143
}

aws/cloudtrail/locals.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ locals {
1313
# Get the default bucket name when no bucket is provided and create_bucket is true.
1414
bucket_name = var.source_details.bucket_details.create_bucket && var.source_details.bucket_details.bucket_name == "cloudtrail-logs-random-id" ? "cloudtrail-logs-${random_string.aws_random.id}" : var.source_details.bucket_details.bucket_name
1515

16-
# Create IAM role condition if no IAM ROLE ARN is provided.
17-
create_iam_role = var.source_details.iam_role_arn != "" ? false : true
18-
19-
# Create SNS topic condition if no SNS topic arn is provided.
20-
create_sns_topic = var.source_details.sns_topic_arn != "" ? false : true
21-
2216
# Trail should be created when we create the bucket. If we do not create the bucket, user should have capability to create and not create trail.
2317
create_trail = var.source_details.bucket_details.create_bucket ? true : var.create_trail
2418

aws/cloudtrail/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ output "aws_s3_bucket" {
99
}
1010

1111
output "aws_sns_topic" {
12-
value = local.create_sns_topic ? aws_sns_topic.sns_topic : {}
12+
value = var.source_details.sns_topic_details.create_sns_topic ? aws_sns_topic.sns_topic : {}
1313
description = "AWS SNS topic attached to the AWS S3 bucket."
1414
}
1515

1616
output "aws_s3_bucket_notification" {
17-
value = local.create_sns_topic && var.source_details.bucket_details.create_bucket ? aws_s3_bucket_notification.bucket_notification : {}
17+
value = var.source_details.sns_topic_details.create_sns_topic && var.source_details.bucket_details.create_bucket ? aws_s3_bucket_notification.bucket_notification : {}
1818
description = "AWS S3 Bucket Notification attached to the AWS S3 Bucket"
1919
}
2020

@@ -24,7 +24,7 @@ output "aws_cloudtrail" {
2424
}
2525

2626
output "aws_iam_role" {
27-
value = local.create_iam_role ? aws_iam_role.source_iam_role : {}
27+
value = var.source_details.iam_details.create_iam_role ? aws_iam_role.source_iam_role : {}
2828
description = "AWS IAM role with permission to allow Sumo Logic to read logs from S3 Bucket."
2929
}
3030

aws/cloudtrail/variables.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ variable "source_details" {
3434
sumo_account_id = number
3535
cutoff_relative_time = string
3636
fields = map(string)
37-
iam_role_arn = string
38-
sns_topic_arn = string
37+
iam_details = object({
38+
create_iam_role = bool
39+
iam_role_arn = string
40+
})
41+
sns_topic_details = object({
42+
create_sns_topic = bool
43+
sns_topic_arn = string
44+
})
3945
})
4046
description = "Provide details for the Sumo Logic CloudTrail source. If not provided, then defaults will be used."
4147
default = {
@@ -54,8 +60,14 @@ variable "source_details" {
5460
sumo_account_id = 926226587429
5561
cutoff_relative_time = "-1d"
5662
fields = {}
57-
iam_role_arn = ""
58-
sns_topic_arn = ""
63+
iam_details = {
64+
create_iam_role = true
65+
iam_role_arn = null
66+
}
67+
sns_topic_details = {
68+
create_sns_topic = true
69+
sns_topic_arn = null
70+
}
5971
}
6072
validation {
6173
condition = can(regex("[a-z0-9-.]{3,63}$", var.source_details.bucket_details.bucket_name))

terratest/aws/cloudtrail/cloudtrail_test.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,14 @@ func TestWithExistingBucketTrailNewCollectorSNSIAM(t *testing.T) {
110110
},
111111
"sumo_account_id": "926226587429",
112112
"collector_id": "",
113-
"iam_role_arn": "",
114-
"sns_topic_arn": "",
113+
"iam_details": map[string]interface{}{
114+
"create_iam_role": true,
115+
"iam_role_arn": nil,
116+
},
117+
"sns_topic_details": map[string]interface{}{
118+
"create_sns_topic": true,
119+
"sns_topic_arn": nil,
120+
},
115121
},
116122
}
117123

@@ -173,8 +179,14 @@ func TestWithExistingBucketTrailCollectorSNSIAM(t *testing.T) {
173179
},
174180
"sumo_account_id": "926226587429",
175181
"collector_id": COLLECTOR_ID,
176-
"iam_role_arn": IAM_ROLE,
177-
"sns_topic_arn": SNS_TOPIC,
182+
"iam_details": map[string]interface{}{
183+
"create_iam_role": false,
184+
"iam_role_arn": IAM_ROLE,
185+
},
186+
"sns_topic_details": map[string]interface{}{
187+
"create_sns_topic": false,
188+
"sns_topic_arn": SNS_TOPIC,
189+
},
178190
},
179191
}
180192

@@ -241,8 +253,14 @@ func TestWithExistingTrailNewBucketCollectorSNSIAM(t *testing.T) {
241253
},
242254
"sumo_account_id": "926226587429",
243255
"collector_id": "",
244-
"iam_role_arn": "",
245-
"sns_topic_arn": "",
256+
"iam_details": map[string]interface{}{
257+
"create_iam_role": true,
258+
"iam_role_arn": nil,
259+
},
260+
"sns_topic_details": map[string]interface{}{
261+
"create_sns_topic": true,
262+
"sns_topic_arn": nil,
263+
},
246264
},
247265
}
248266

@@ -347,8 +365,14 @@ func TestUpdates(t *testing.T) {
347365
"fields": map[string]interface{}{},
348366
"sumo_account_id": "926226587429",
349367
"collector_id": "",
350-
"iam_role_arn": IAM_ROLE,
351-
"sns_topic_arn": "",
368+
"iam_details": map[string]interface{}{
369+
"create_iam_role": false,
370+
"iam_role_arn": IAM_ROLE,
371+
},
372+
"sns_topic_details": map[string]interface{}{
373+
"create_sns_topic": true,
374+
"sns_topic_arn": nil,
375+
},
352376
},
353377
}
354378

@@ -390,8 +414,14 @@ func TestUpdates(t *testing.T) {
390414
},
391415
"sumo_account_id": "926226587429",
392416
"collector_id": "",
393-
"iam_role_arn": IAM_ROLE,
394-
"sns_topic_arn": "",
417+
"iam_details": map[string]interface{}{
418+
"create_iam_role": false,
419+
"iam_role_arn": IAM_ROLE,
420+
},
421+
"sns_topic_details": map[string]interface{}{
422+
"create_sns_topic": true,
423+
"sns_topic_arn": nil,
424+
},
395425
},
396426
}
397427

0 commit comments

Comments
 (0)