Skip to content

Commit ca4ccdb

Browse files
committed
Split log_config and log_delivery
1 parent e5e8a5c commit ca4ccdb

File tree

6 files changed

+110
-61
lines changed

6 files changed

+110
-61
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ No modules.
514514
| <a name="input_create_archives"></a> [create\_archives](#input\_create\_archives) | Controls whether EventBridge Archive resources should be created | `bool` | `false` | no |
515515
| <a name="input_create_bus"></a> [create\_bus](#input\_create\_bus) | Controls whether EventBridge Bus resource should be created | `bool` | `true` | no |
516516
| <a name="input_create_connections"></a> [create\_connections](#input\_create\_connections) | Controls whether EventBridge Connection resources should be created | `bool` | `false` | no |
517-
| <a name="input_create_logging"></a> [create\_logging](#input\_create\_logging) | Controls whether EventBridge Logging resources should be created | `bool` | `true` | no |
517+
| <a name="input_create_log_delivery"></a> [create\_log\_delivery](#input\_create\_log\_delivery) | Controls whether EventBridge log delivery resources should be created | `bool` | `true` | no |
518+
| <a name="input_create_log_delivery_source"></a> [create\_log\_delivery\_source](#input\_create\_log\_delivery\_source) | Controls whether EventBridge log delivery source resource should be created | `bool` | `true` | no |
518519
| <a name="input_create_permissions"></a> [create\_permissions](#input\_create\_permissions) | Controls whether EventBridge Permission resources should be created | `bool` | `true` | no |
519520
| <a name="input_create_pipe_role_only"></a> [create\_pipe\_role\_only](#input\_create\_pipe\_role\_only) | Controls whether an IAM role should be created for the pipes only | `bool` | `false` | no |
520521
| <a name="input_create_pipes"></a> [create\_pipes](#input\_create\_pipes) | Controls whether EventBridge Pipes resources should be created | `bool` | `true` | no |
@@ -532,8 +533,9 @@ No modules.
532533
| <a name="input_kinesis_target_arns"></a> [kinesis\_target\_arns](#input\_kinesis\_target\_arns) | The Amazon Resource Name (ARN) of the Kinesis Streams you want to use as EventBridge targets | `list(string)` | `[]` | no |
533534
| <a name="input_kms_key_identifier"></a> [kms\_key\_identifier](#input\_kms\_key\_identifier) | The identifier of the AWS KMS customer managed key for EventBridge to use, if you choose to use a customer managed key to encrypt events on this event bus. The identifier can be the key Amazon Resource Name (ARN), KeyId, key alias, or key alias ARN. | `string` | `null` | no |
534535
| <a name="input_lambda_target_arns"></a> [lambda\_target\_arns](#input\_lambda\_target\_arns) | The Amazon Resource Name (ARN) of the Lambda Functions you want to use as EventBridge targets | `list(string)` | `[]` | no |
536+
| <a name="input_log_config"></a> [log\_config](#input\_log\_config) | The configuration block for the EventBridge bus log config settings | <pre>object({<br/> include_detail = string<br/> level = string<br/> })</pre> | `null` | no |
537+
| <a name="input_log_delivery"></a> [log\_delivery](#input\_log\_delivery) | Map of the configuration block for the EventBridge bus log delivery settings (key is the type of log delivery: cloudwatch\_logs, s3, firehose) | <pre>map(object({<br/> enabled = optional(bool, true)<br/> destination_arn = string<br/> source_name = optional(string)<br/> name = optional(string)<br/> output_format = optional(string)<br/> field_delimiter = optional(string)<br/> record_fields = optional(list(string))<br/> s3_delivery_configuration = optional(object({<br/> enable_hive_compatible_path = optional(bool)<br/> suffix_path = optional(string)<br/> }))<br/> }))</pre> | `{}` | no |
535538
| <a name="input_log_delivery_source_name"></a> [log\_delivery\_source\_name](#input\_log\_delivery\_source\_name) | Name of log delivery source | `string` | `null` | no |
536-
| <a name="input_logging"></a> [logging](#input\_logging) | The configuration block for the EventBridge bus logging | <pre>object({<br/> include_detail = optional(string)<br/> level = optional(string)<br/><br/> cloudwatch_logs = optional(object({<br/> enabled = optional(bool, false)<br/> name = optional(string)<br/> arn = string<br/> field_delimiter = optional(string)<br/> record_fields = optional(list(string))<br/> }))<br/><br/> s3 = optional(object({<br/> enabled = optional(bool, false)<br/> name = optional(string)<br/> arn = string<br/> field_delimiter = optional(string)<br/> record_fields = optional(list(string))<br/> s3_delivery_configuration = optional(object({<br/> enable_hive_compatible_path = optional(bool)<br/> suffix_path = optional(string)<br/> }))<br/> }))<br/><br/> firehose = optional(object({<br/> enabled = optional(bool, false)<br/> name = optional(string)<br/> arn = string<br/> field_delimiter = optional(string)<br/> record_fields = optional(list(string))<br/> }))<br/> })</pre> | `null` | no |
537539
| <a name="input_number_of_policies"></a> [number\_of\_policies](#input\_number\_of\_policies) | Number of policies to attach to IAM role | `number` | `0` | no |
538540
| <a name="input_number_of_policy_jsons"></a> [number\_of\_policy\_jsons](#input\_number\_of\_policy\_jsons) | Number of policies JSON to attach to IAM role | `number` | `0` | no |
539541
| <a name="input_permissions"></a> [permissions](#input\_permissions) | A map of objects with EventBridge Permission definitions. | `map(any)` | `{}` | no |
@@ -579,7 +581,8 @@ No modules.
579581
| <a name="output_eventbridge_connection_ids"></a> [eventbridge\_connection\_ids](#output\_eventbridge\_connection\_ids) | The EventBridge Connection IDs |
580582
| <a name="output_eventbridge_connections"></a> [eventbridge\_connections](#output\_eventbridge\_connections) | The EventBridge Connections created and their attributes |
581583
| <a name="output_eventbridge_iam_roles"></a> [eventbridge\_iam\_roles](#output\_eventbridge\_iam\_roles) | The EventBridge IAM roles created and their attributes |
582-
| <a name="output_eventbridge_log_delivery_source"></a> [eventbridge\_log\_delivery\_source](#output\_eventbridge\_log\_delivery\_source) | The EventBridge Bus CloudWatch Log Delivery Source created and their attributes |
584+
| <a name="output_eventbridge_log_delivery_source_arn"></a> [eventbridge\_log\_delivery\_source\_arn](#output\_eventbridge\_log\_delivery\_source\_arn) | The EventBridge Bus CloudWatch Log Delivery Source ARN |
585+
| <a name="output_eventbridge_log_delivery_source_name"></a> [eventbridge\_log\_delivery\_source\_name](#output\_eventbridge\_log\_delivery\_source\_name) | The EventBridge Bus CloudWatch Log Delivery Source Name |
583586
| <a name="output_eventbridge_permission_ids"></a> [eventbridge\_permission\_ids](#output\_eventbridge\_permission\_ids) | The EventBridge Permission IDs |
584587
| <a name="output_eventbridge_permissions"></a> [eventbridge\_permissions](#output\_eventbridge\_permissions) | The EventBridge Permissions created and their attributes |
585588
| <a name="output_eventbridge_pipe_arns"></a> [eventbridge\_pipe\_arns](#output\_eventbridge\_pipe\_arns) | The EventBridge Pipes ARNs |

examples/with-bus-logging/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ $ terraform apply
3333
|------|--------|---------|
3434
| <a name="module_cloudwatch_log_group"></a> [cloudwatch\_log\_group](#module\_cloudwatch\_log\_group) | terraform-aws-modules/cloudwatch/aws//modules/log-group | ~> 5.0 |
3535
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | n/a |
36+
| <a name="module_eventbridge_external"></a> [eventbridge\_external](#module\_eventbridge\_external) | ../../ | n/a |
37+
| <a name="module_eventbridge_log_delivery_only"></a> [eventbridge\_log\_delivery\_only](#module\_eventbridge\_log\_delivery\_only) | ../../ | n/a |
3638
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 5.0 |
3739

3840
## Resources

examples/with-bus-logging/main.tf

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,57 @@ module "eventbridge" {
1616

1717
bus_name = random_pet.this.id
1818

19-
logging = {
19+
log_config = {
2020
include_detail = "FULL"
2121
level = "INFO"
22+
}
23+
24+
log_delivery = {
25+
cloudwatch_logs = {
26+
destination_arn = module.cloudwatch_log_group.cloudwatch_log_group_arn
27+
}
28+
s3 = {
29+
destination_arn = module.s3_bucket.s3_bucket_arn
30+
}
31+
}
32+
}
33+
34+
# External EventBridge bus with log delivery attached to the bus
35+
module "eventbridge_external" {
36+
source = "../../"
37+
38+
create_bus = true
39+
40+
bus_name = "${random_pet.this.id}-external-bus"
41+
42+
log_config = {
43+
include_detail = "FULL"
44+
level = "TRACE"
45+
}
46+
}
47+
48+
module "eventbridge_log_delivery_only" {
49+
source = "../../"
50+
51+
create_bus = false
52+
create_role = false
53+
54+
bus_name = module.eventbridge_external.eventbridge_bus_name
55+
56+
create_log_delivery_source = false
57+
58+
log_delivery = {
2259
cloudwatch_logs = {
23-
enabled = true
24-
arn = module.cloudwatch_log_group.cloudwatch_log_group_arn
60+
destination_arn = module.cloudwatch_log_group.cloudwatch_log_group_arn
61+
source_name = module.eventbridge_external.eventbridge_log_delivery_source_name
2562
}
2663
s3 = {
27-
enabled = true
28-
arn = module.s3_bucket.s3_bucket_arn
64+
destination_arn = module.s3_bucket.s3_bucket_arn
65+
source_name = module.eventbridge_external.eventbridge_log_delivery_source_name
2966
}
3067
}
68+
69+
depends_on = [module.eventbridge_external]
3170
}
3271

3372
#################
@@ -99,7 +138,8 @@ data "aws_iam_policy_document" "bucket_policy" {
99138
test = "ArnLike"
100139
variable = "aws:SourceArn"
101140
values = [
102-
module.eventbridge.eventbridge_log_delivery_source[0].arn
141+
module.eventbridge.eventbridge_log_delivery_source_arn,
142+
module.eventbridge_external.eventbridge_log_delivery_source_arn
103143
]
104144
}
105145
}

main.tf

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ locals {
5454
})
5555
])
5656

57-
create_logging = var.create && var.create_bus && var.create_logging && var.logging != null
57+
create_log_delivery = var.create && var.create_log_delivery
5858
}
5959

6060
data "aws_cloudwatch_event_bus" "this" {
@@ -75,66 +75,68 @@ resource "aws_cloudwatch_event_bus" "this" {
7575

7676
dynamic "dead_letter_config" {
7777
for_each = length(var.dead_letter_config) > 0 ? [var.dead_letter_config] : []
78+
7879
content {
7980
arn = try(dead_letter_config.value.arn, null)
8081
}
8182
}
8283

8384
dynamic "log_config" {
84-
for_each = var.logging != null ? [var.logging] : []
85+
for_each = var.log_config != null ? [var.log_config] : []
86+
8587
content {
86-
include_detail = log_config.value.include_detail
87-
level = log_config.value.level
88+
include_detail = try(log_config.value.include_detail, null)
89+
level = try(upper(log_config.value.level), null)
8890
}
8991
}
9092

9193
tags = var.tags
9294
}
9395

9496
resource "aws_cloudwatch_log_delivery_source" "this" {
95-
count = local.create_logging ? 1 : 0
97+
count = local.create_log_delivery && var.create_log_delivery_source ? 1 : 0
9698

9799
region = var.region
98100

99101
name = coalesce(var.log_delivery_source_name, var.bus_name)
100-
log_type = "${upper(var.logging.level)}_LOGS"
101-
resource_arn = aws_cloudwatch_event_bus.this[0].arn
102+
log_type = format("%s_LOGS", try(contains(["INFO", "ERROR", "TRACE"], upper(var.log_config.level)), false) ? upper(var.log_config.level) : "ERROR")
103+
resource_arn = var.create_bus ? aws_cloudwatch_event_bus.this[0].arn : data.aws_cloudwatch_event_bus.this[0].arn
102104

103105
tags = var.tags
104106
}
105107

106108
resource "aws_cloudwatch_log_delivery_destination" "this" {
107-
for_each = { for k, v in var.logging : k => v if(local.create_logging && contains(["s3", "cloudwatch_logs", "firehose"], k) && try(v.enabled, true) && v != null) }
109+
for_each = { for k, v in var.log_delivery : k => v if(local.create_log_delivery && try(v.enabled, true)) }
108110

109111
region = var.region
110112

111113
name = coalesce(each.value.name, "${var.bus_name}-${each.key}")
112-
output_format = try(each.value.output_format, null)
114+
output_format = each.value.output_format
113115

114116
delivery_destination_configuration {
115-
destination_resource_arn = each.value.arn
117+
destination_resource_arn = each.value.destination_arn
116118
}
117119

118120
tags = var.tags
119121
}
120122

121123
resource "aws_cloudwatch_log_delivery" "this" {
122-
for_each = { for k, v in var.logging : k => v if(local.create_logging && contains(["s3", "cloudwatch_logs", "firehose"], k) && try(v.enabled, true) && v != null) }
124+
for_each = { for k, v in var.log_delivery : k => v if(local.create_log_delivery && try(v.enabled, true)) }
123125

124126
region = var.region
125127

126-
delivery_source_name = aws_cloudwatch_log_delivery_source.this[0].name
128+
delivery_source_name = var.create_log_delivery_source ? aws_cloudwatch_log_delivery_source.this[0].name : each.value.source_name
127129
delivery_destination_arn = aws_cloudwatch_log_delivery_destination.this[each.key].arn
128130

129131
field_delimiter = each.value.field_delimiter
130132
record_fields = each.value.record_fields
131133

132134
dynamic "s3_delivery_configuration" {
133-
for_each = try(each.value.s3_delivery_configuration, null) != null ? [true] : []
135+
for_each = each.value.s3_delivery_configuration != null ? [each.value.s3_delivery_configuration] : []
134136

135137
content {
136-
enable_hive_compatible_path = each.value.s3_delivery_configuration.enable_hive_compatible_path
137-
suffix_path = each.value.s3_delivery_configuration.suffix_path
138+
enable_hive_compatible_path = s3_delivery_configuration.value.enable_hive_compatible_path
139+
suffix_path = s3_delivery_configuration.value.suffix_path
138140
}
139141
}
140142

outputs.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,15 @@ output "eventbridge_pipes" {
161161
value = aws_pipes_pipe.this
162162
}
163163

164-
output "eventbridge_log_delivery_source" {
165-
description = "The EventBridge Bus CloudWatch Log Delivery Source created and their attributes"
166-
value = aws_cloudwatch_log_delivery_source.this
164+
# EventBridge Log Delivery Source
165+
output "eventbridge_log_delivery_source_arn" {
166+
description = "The EventBridge Bus CloudWatch Log Delivery Source ARN"
167+
value = try(aws_cloudwatch_log_delivery_source.this[0].arn, "")
168+
}
169+
170+
output "eventbridge_log_delivery_source_name" {
171+
description = "The EventBridge Bus CloudWatch Log Delivery Source Name"
172+
value = try(aws_cloudwatch_log_delivery_source.this[0].name, "")
167173
}
168174

169175
# IAM Roles

variables.tf

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,14 @@ variable "create_pipes" {
118118
default = true
119119
}
120120

121-
variable "create_logging" {
122-
description = "Controls whether EventBridge Logging resources should be created"
121+
variable "create_log_delivery_source" {
122+
description = "Controls whether EventBridge log delivery source resource should be created"
123+
type = bool
124+
default = true
125+
}
126+
127+
variable "create_log_delivery" {
128+
description = "Controls whether EventBridge log delivery resources should be created"
123129
type = bool
124130
default = true
125131
}
@@ -144,43 +150,33 @@ variable "bus_description" {
144150
default = null
145151
}
146152

147-
variable "logging" {
148-
description = "The configuration block for the EventBridge bus logging"
153+
variable "log_config" {
154+
description = "The configuration block for the EventBridge bus log config settings"
149155
type = object({
150-
include_detail = optional(string)
151-
level = optional(string)
152-
153-
cloudwatch_logs = optional(object({
154-
enabled = optional(bool, false)
155-
name = optional(string)
156-
arn = string
157-
field_delimiter = optional(string)
158-
record_fields = optional(list(string))
159-
}))
160-
161-
s3 = optional(object({
162-
enabled = optional(bool, false)
163-
name = optional(string)
164-
arn = string
165-
field_delimiter = optional(string)
166-
record_fields = optional(list(string))
167-
s3_delivery_configuration = optional(object({
168-
enable_hive_compatible_path = optional(bool)
169-
suffix_path = optional(string)
170-
}))
171-
}))
172-
173-
firehose = optional(object({
174-
enabled = optional(bool, false)
175-
name = optional(string)
176-
arn = string
177-
field_delimiter = optional(string)
178-
record_fields = optional(list(string))
179-
}))
156+
include_detail = string
157+
level = string
180158
})
181159
default = null
182160
}
183161

162+
variable "log_delivery" {
163+
description = "Map of the configuration block for the EventBridge bus log delivery settings (key is the type of log delivery: cloudwatch_logs, s3, firehose)"
164+
type = map(object({
165+
enabled = optional(bool, true)
166+
destination_arn = string
167+
source_name = optional(string)
168+
name = optional(string)
169+
output_format = optional(string)
170+
field_delimiter = optional(string)
171+
record_fields = optional(list(string))
172+
s3_delivery_configuration = optional(object({
173+
enable_hive_compatible_path = optional(bool)
174+
suffix_path = optional(string)
175+
}))
176+
}))
177+
default = {}
178+
}
179+
184180
variable "log_delivery_source_name" {
185181
description = "Name of log delivery source"
186182
type = string

0 commit comments

Comments
 (0)