Skip to content

Commit 3a2c89e

Browse files
author
sourabh
committed
correcting ELB module to create IAM role and SNS topic based on condition.
1 parent e331262 commit 3a2c89e

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

aws/elb/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 ELB logs fr
3535
| auto\_enable\_access\_logs\_options | filter - provide a regex to filter the ELB for which access logs should be enabled. Empty means all resources. For eg :- 'Type': 'application'\|'type': 'application', will enable access logs for Application load balancer only.<br> remove\_on\_delete\_stack - provide true if you would like to disable access logging when you destroy the terraform resources. | <pre>object({<br> filter = string<br> remove_on_delete_stack = bool<br> })</pre> | <pre>{<br> "filter": "",<br> "remove_on_delete_stack": true<br>}</pre> | no |
3636
| 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 Elb Collector <Random ID>",<br> "description": "This collector is created using Sumo Logic terraform AWS ELB module to collect AWS elb logs.",<br> "fields": {}<br>}</pre> | no |
3737
| create\_collector | Provide "true" if you would like to create the Sumo Logic Collector. | `bool` | n/a | yes |
38-
| source\_details | Provide details for the Sumo Logic ELB 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": "elb-logs-random-id",<br> "create_bucket": true,<br> "force_destroy_bucket": true,<br> "path_expression": "*AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION-NAME>/*"<br> },<br> "collector_id": "",<br> "cutoff_relative_time": "-1d",<br> "description": "This source is created using Sumo Logic terraform AWS elb module to collect AWS elb logs.",<br> "fields": {},<br> "iam_role_arn": "",<br> "paused": false,<br> "scan_interval": 300000,<br> "sns_topic_arn": "",<br> "source_category": "Labs/aws/elb",<br> "source_name": "Elb Source",<br> "sumo_account_id": 926226587429<br>}</pre> | no |
38+
| source\_details | Provide details for the Sumo Logic ELB 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": "elb-logs-random-id",<br> "create_bucket": true,<br> "force_destroy_bucket": true,<br> "path_expression": "*AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION-NAME>/*"<br> },<br> "collector_id": "",<br> "cutoff_relative_time": "-1d",<br> "description": "This source is created using Sumo Logic terraform AWS elb module to collect AWS elb 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/elb",<br> "source_name": "Elb 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/elb/elb.tf

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

2727
resource "aws_sns_topic" "sns_topic" {
28-
for_each = toset(local.create_sns_topic ? ["sns_topic"] : [])
28+
for_each = toset(var.source_details.sns_topic_details.create_sns_topic ? ["sns_topic"] : [])
2929

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

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

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

@@ -48,7 +48,7 @@ resource "aws_s3_bucket_notification" "bucket_notification" {
4848
}
4949

5050
resource "aws_iam_role" "source_iam_role" {
51-
for_each = toset(local.create_iam_role ? ["source_iam_role"] : [])
51+
for_each = toset(var.source_details.iam_details.create_iam_role ? ["source_iam_role"] : [])
5252

5353
name = "SumoLogic-Terraform-Elb-Module-${random_string.aws_random.id}"
5454
path = "/"
@@ -63,7 +63,7 @@ resource "aws_iam_role" "source_iam_role" {
6363
}
6464

6565
resource "aws_iam_policy" "iam_policy" {
66-
for_each = toset(local.create_iam_role ? ["iam_policy"] : [])
66+
for_each = toset(var.source_details.iam_details.create_iam_role ? ["iam_policy"] : [])
6767

6868
name = "SumoLogicElbSource-${random_string.aws_random.id}"
6969
policy = templatefile("${path.module}/templates/sumologic_source_policy.tmpl", {
@@ -102,7 +102,7 @@ resource "sumologic_elb_source" "source" {
102102
scan_interval = var.source_details.scan_interval
103103
authentication {
104104
type = "AWSRoleBasedAuthentication"
105-
role_arn = local.create_iam_role ? aws_iam_role.source_iam_role["source_iam_role"].arn : var.source_details.iam_role_arn
105+
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
106106
}
107107

108108
path {
@@ -130,7 +130,7 @@ resource "aws_sns_topic_subscription" "subscription" {
130130
endpoint = sumologic_elb_source.source.url
131131
endpoint_auto_confirms = true
132132
protocol = "https"
133-
topic_arn = local.create_sns_topic ? aws_sns_topic.sns_topic["sns_topic"].arn : var.source_details.sns_topic_arn
133+
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
134134
}
135135

136136
# Reason to use the SAM app, is to have single source of truth for Auto Enable access logs functionality.

aws/elb/locals.tf

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

13-
# Create IAM role condition if no IAM ROLE ARN is provided.
14-
create_iam_role = var.source_details.iam_role_arn != "" ? false : true
15-
16-
# Create SNS topic condition if no SNS topic arn is provided.
17-
create_sns_topic = var.source_details.sns_topic_arn != "" ? false : true
18-
1913
# Auto enable should be called if input is anything other than None.
2014
auto_enable_access_logs = var.auto_enable_access_logs != "None" ? true : false
2115

aws/elb/outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ 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

2121
output "aws_iam_role" {
22-
value = local.create_iam_role ? aws_iam_role.source_iam_role : {}
22+
value = var.source_details.iam_details.create_iam_role ? aws_iam_role.source_iam_role : {}
2323
description = "AWS IAM role with permission to allow Sumo Logic to read logs from S3 Bucket."
2424
}
2525

aws/elb/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 ELB 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/elb/elb_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,14 @@ func TestWithExistingResourcesValues(t *testing.T) {
109109
},
110110
"sumo_account_id": "926226587429",
111111
"collector_id": COLLECTOR_ID,
112-
"iam_role_arn": IAM_ROLE,
113-
"sns_topic_arn": SNS_TOPIC,
112+
"iam_details": map[string]interface{}{
113+
"create_iam_role": false,
114+
"iam_role_arn": IAM_ROLE,
115+
},
116+
"sns_topic_details": map[string]interface{}{
117+
"create_sns_topic": false,
118+
"sns_topic_arn": SNS_TOPIC,
119+
},
114120
},
115121
}
116122

@@ -130,7 +136,7 @@ func TestWithExistingResourcesValues(t *testing.T) {
130136
"OrgId": common.SumologicOrganizationId,
131137
"BucketName": BUCKET_NAME,
132138
"PathExpression": PATH_EXPRESSION,
133-
"RandomString": outputs["random_string"].(map[string]interface{})["id"].(string),
139+
"RandomString": outputs["random_string"].(map[string]interface{})["id"].(string),
134140
}
135141
// Assert if the outputs are actually created in AWS and Sumo Logic.
136142
// This also checks if your expectation are matched with the outputs, so provide an JSON with expected outputs.
@@ -177,8 +183,14 @@ func TestWithExistingCollectorIAMNewSNSResources(t *testing.T) {
177183
},
178184
"sumo_account_id": "926226587429",
179185
"collector_id": COLLECTOR_ID,
180-
"iam_role_arn": IAM_ROLE,
181-
"sns_topic_arn": "",
186+
"iam_details": map[string]interface{}{
187+
"create_iam_role": false,
188+
"iam_role_arn": IAM_ROLE,
189+
},
190+
"sns_topic_details": map[string]interface{}{
191+
"create_sns_topic": true,
192+
"sns_topic_arn": nil,
193+
},
182194
},
183195
}
184196

@@ -198,7 +210,7 @@ func TestWithExistingCollectorIAMNewSNSResources(t *testing.T) {
198210
"OrgId": common.SumologicOrganizationId,
199211
"BucketName": BUCKET_NAME,
200212
"PathExpression": PATH_EXPRESSION,
201-
"RandomString": outputs["random_string"].(map[string]interface{})["id"].(string),
213+
"RandomString": outputs["random_string"].(map[string]interface{})["id"].(string),
202214
}
203215
// Assert if the outputs are actually created in AWS and Sumo Logic.
204216
// This also checks if your expectation are matched with the outputs, so provide an JSON with expected outputs.

0 commit comments

Comments
 (0)