Skip to content

Commit e5e8a5c

Browse files
committed
Fixed code and examples to pass
1 parent ee4ece2 commit e5e8a5c

File tree

6 files changed

+96
-144
lines changed

6 files changed

+96
-144
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.99.4
3+
rev: v1.101.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_docs
@@ -23,7 +23,7 @@ repos:
2323
- '--args=--only=terraform_workspace_remote'
2424
- id: terraform_validate
2525
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v5.0.0
26+
rev: v6.0.0
2727
hooks:
2828
- id: check-merge-conflict
2929
- id: end-of-file-fixer

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,27 @@ Terraform module to create EventBridge resources.
1414

1515
### EventBridge Complete
1616

17-
Most common use-case which creates custom bus, rules and targets.
17+
Most common use-case which creates custom bus, logging, rules and targets.
1818

1919
```hcl
2020
module "eventbridge" {
2121
source = "terraform-aws-modules/eventbridge/aws"
2222
2323
bus_name = "my-bus"
2424
25+
logging = {
26+
include_detail = "FULL"
27+
level = "INFO"
28+
cloudwatch_logs = {
29+
enabled = true
30+
arn = "arn:aws:logs:us-east-1:123456789012:log-group:my-log-group"
31+
}
32+
s3 = {
33+
enabled = true
34+
arn = "arn:aws:s3:::my-log-bucket"
35+
}
36+
}
37+
2538
rules = {
2639
orders = {
2740
description = "Capture all order data"
@@ -347,6 +360,7 @@ module "eventbridge" {
347360
create_schedule_groups = false # to control creation of EventBridge Schedule Group resources
348361
create_schedules = false # to control creation of EventBridge Schedule resources
349362
create_pipes = false # to control creation of EventBridge Pipes resources
363+
create_logging = false # to control creation of EventBridge Logging resources
350364
351365
attach_cloudwatch_policy = false
352366
attach_ecs_policy = false
@@ -368,6 +382,7 @@ module "eventbridge" {
368382
* [HTTP API Gateway](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/api-gateway-event-source) - Creates an integration with HTTP API Gateway as event source.
369383
* [Using Default Bus](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/default-bus) - Creates resources in the `default` bus.
370384
* [Archive](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-archive) - EventBridge Archives resources in various configurations.
385+
* [Logging](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-bus-logging) - EventBridge Logging resources in various configurations.
371386
* [Permissions](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-permissions) - Controls permissions to EventBridge.
372387
* [Scheduler](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-schedules) - EventBridge Scheduler which works with any bus (recommended way).
373388
* [ECS Scheduling Events](https://github.com/terraform-aws-modules/terraform-aws-eventbridge/tree/master/examples/with-ecs-scheduling) - Use default bus to schedule events on ECS.
@@ -405,12 +420,8 @@ No modules.
405420
| [aws_cloudwatch_event_permission.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_permission) | resource |
406421
| [aws_cloudwatch_event_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |
407422
| [aws_cloudwatch_event_target.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_target) | resource |
408-
| [aws_cloudwatch_log_delivery.cwlogs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource |
409-
| [aws_cloudwatch_log_delivery.firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource |
410-
| [aws_cloudwatch_log_delivery.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource |
411-
| [aws_cloudwatch_log_delivery_destination.cwlogs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource |
412-
| [aws_cloudwatch_log_delivery_destination.firehose](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource |
413-
| [aws_cloudwatch_log_delivery_destination.s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource |
423+
| [aws_cloudwatch_log_delivery.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource |
424+
| [aws_cloudwatch_log_delivery_destination.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource |
414425
| [aws_cloudwatch_log_delivery_source.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_source) | resource |
415426
| [aws_iam_policy.additional_inline](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
416427
| [aws_iam_policy.additional_json](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
@@ -495,7 +506,6 @@ No modules.
495506
| <a name="input_attach_sqs_policy"></a> [attach\_sqs\_policy](#input\_attach\_sqs\_policy) | Controls whether the SQS policy should be added to IAM role for EventBridge Target | `bool` | `false` | no |
496507
| <a name="input_attach_tracing_policy"></a> [attach\_tracing\_policy](#input\_attach\_tracing\_policy) | Controls whether X-Ray tracing policy should be added to IAM role for EventBridge | `bool` | `false` | no |
497508
| <a name="input_bus_description"></a> [bus\_description](#input\_bus\_description) | Event bus description | `string` | `null` | no |
498-
| <a name="input_bus_log_config"></a> [bus\_log\_config](#input\_bus\_log\_config) | The configuration block for the EventBridge bus logging | <pre>object({<br/> include_detail = optional(string)<br/> level = optional(string)<br/><br/> cloudwatch = optional(object({<br/> enabled = optional(bool, false)<br/> log_group_arn = optional(string)<br/> }))<br/><br/> s3 = optional(object({<br/> enabled = optional(bool, false)<br/> bucket_arn = optional(string)<br/> }))<br/><br/> firehose = optional(object({<br/> enabled = optional(bool, false)<br/> delivery_stream_arn = optional(string)<br/> }))<br/> })</pre> | `null` | no |
499509
| <a name="input_bus_name"></a> [bus\_name](#input\_bus\_name) | A unique name for your EventBridge Bus | `string` | `"default"` | no |
500510
| <a name="input_cloudwatch_target_arns"></a> [cloudwatch\_target\_arns](#input\_cloudwatch\_target\_arns) | The Amazon Resource Name (ARN) of the Cloudwatch Log Streams you want to use as EventBridge targets | `list(string)` | `[]` | no |
501511
| <a name="input_connections"></a> [connections](#input\_connections) | A map of objects with EventBridge Connection definitions. | `any` | `{}` | no |
@@ -504,6 +514,7 @@ No modules.
504514
| <a name="input_create_archives"></a> [create\_archives](#input\_create\_archives) | Controls whether EventBridge Archive resources should be created | `bool` | `false` | no |
505515
| <a name="input_create_bus"></a> [create\_bus](#input\_create\_bus) | Controls whether EventBridge Bus resource should be created | `bool` | `true` | no |
506516
| <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 |
507518
| <a name="input_create_permissions"></a> [create\_permissions](#input\_create\_permissions) | Controls whether EventBridge Permission resources should be created | `bool` | `true` | no |
508519
| <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 |
509520
| <a name="input_create_pipes"></a> [create\_pipes](#input\_create\_pipes) | Controls whether EventBridge Pipes resources should be created | `bool` | `true` | no |
@@ -521,6 +532,8 @@ No modules.
521532
| <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 |
522533
| <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 |
523534
| <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 |
535+
| <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 |
524537
| <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 |
525538
| <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 |
526539
| <a name="input_permissions"></a> [permissions](#input\_permissions) | A map of objects with EventBridge Permission definitions. | `map(any)` | `{}` | no |

examples/with-bus-logging/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ $ terraform apply
3131

3232
| Name | Source | Version |
3333
|------|--------|---------|
34-
| <a name="module_cloudwatch_log_group"></a> [cloudwatch\_log\_group](#module\_cloudwatch\_log\_group) | terraform-aws-modules/cloudwatch/aws//modules/log-group | ~> 3.0 |
34+
| <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 |
3636
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 5.0 |
3737

@@ -42,7 +42,6 @@ $ terraform apply
4242
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
4343
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
4444
| [aws_iam_policy_document.bucket_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
45-
| [aws_iam_policy_document.cwlogs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
4645

4746
## Inputs
4847

examples/with-bus-logging/main.tf

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ module "eventbridge" {
1414

1515
create_bus = true
1616

17-
bus_name = "${random_pet.this.id}-bus"
18-
bus_log_config = {
17+
bus_name = random_pet.this.id
18+
19+
logging = {
1920
include_detail = "FULL"
2021
level = "INFO"
21-
cloudwatch = {
22-
enabled = true
23-
log_group_arn = module.cloudwatch_log_group.cloudwatch_log_group_arn
22+
cloudwatch_logs = {
23+
enabled = true
24+
arn = module.cloudwatch_log_group.cloudwatch_log_group_arn
2425
}
2526
s3 = {
26-
enabled = true
27-
bucket_arn = module.s3_bucket.s3_bucket_arn
27+
enabled = true
28+
arn = module.s3_bucket.s3_bucket_arn
2829
}
2930
}
3031
}
@@ -42,41 +43,12 @@ resource "random_pet" "this" {
4243
######################
4344
module "cloudwatch_log_group" {
4445
source = "terraform-aws-modules/cloudwatch/aws//modules/log-group"
45-
version = "~> 3.0"
46+
version = "~> 5.0"
4647

4748
name = "/aws/vendedlogs/events/event-bus/${random_pet.this.id}-bus"
4849
retention_in_days = 14
4950
}
5051

51-
data "aws_iam_policy_document" "cwlogs" {
52-
statement {
53-
effect = "Allow"
54-
principals {
55-
type = "Service"
56-
identifiers = ["delivery.logs.amazonaws.com"]
57-
}
58-
actions = [
59-
"logs:CreateLogStream",
60-
"logs:PutLogEvents"
61-
]
62-
resources = [
63-
"${module.cloudwatch_log_group.arn}:log-stream:*"
64-
]
65-
condition {
66-
test = "StringEquals"
67-
variable = "aws:SourceAccount"
68-
values = [data.aws_caller_identity.current.account_id]
69-
}
70-
condition {
71-
test = "ArnLike"
72-
variable = "aws:SourceArn"
73-
values = [
74-
module.eventbridge.eventbridge_log_delivery_source.arn
75-
]
76-
}
77-
}
78-
}
79-
8052
####
8153
# S3
8254
####
@@ -85,6 +57,8 @@ module "s3_bucket" {
8557
version = "~> 5.0"
8658

8759
bucket = "${random_pet.this.id}-eventbridge-bus-logs-bucket"
60+
force_destroy = true
61+
8862
attach_policy = true
8963
policy = data.aws_iam_policy_document.bucket_policy.json
9064

@@ -125,13 +99,8 @@ data "aws_iam_policy_document" "bucket_policy" {
12599
test = "ArnLike"
126100
variable = "aws:SourceArn"
127101
values = [
128-
module.eventbridge.eventbridge_log_delivery_source.arn
102+
module.eventbridge.eventbridge_log_delivery_source[0].arn
129103
]
130104
}
131105
}
132106
}
133-
134-
#
135-
# Kinesis Fire
136-
#
137-

main.tf

Lines changed: 27 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ locals {
5353
"Name" = var.append_pipe_postfix ? "${replace(index, "_", "-")}-pipe" : index
5454
})
5555
])
56-
enabled_bus_log_type = var.bus_log_config != null ? "${upper(var.bus_log_config.level)}_LOGS" : null
56+
57+
create_logging = var.create && var.create_bus && var.create_logging && var.logging != null
5758
}
5859

5960
data "aws_cloudwatch_event_bus" "this" {
@@ -80,7 +81,7 @@ resource "aws_cloudwatch_event_bus" "this" {
8081
}
8182

8283
dynamic "log_config" {
83-
for_each = var.bus_log_config != null ? [var.bus_log_config] : []
84+
for_each = var.logging != null ? [var.logging] : []
8485
content {
8586
include_detail = log_config.value.include_detail
8687
level = log_config.value.level
@@ -91,110 +92,55 @@ resource "aws_cloudwatch_event_bus" "this" {
9192
}
9293

9394
resource "aws_cloudwatch_log_delivery_source" "this" {
94-
count = (
95-
var.create &&
96-
var.create_bus &&
97-
var.bus_log_config != null
98-
) ? 1 : 0
99-
100-
name = "EventBusSource-${var.bus_name}-${local.enabled_bus_log_type}"
101-
log_type = local.enabled_bus_log_type
102-
resource_arn = aws_cloudwatch_event_bus.this[0].arn
103-
}
95+
count = local.create_logging ? 1 : 0
10496

105-
resource "aws_cloudwatch_log_delivery_destination" "cwlogs" {
106-
count = (
107-
var.create &&
108-
var.create_bus &&
109-
var.bus_log_config != null &&
110-
var.bus_log_config.cloudwatch != null &&
111-
var.bus_log_config.cloudwatch.enabled
112-
) ? 1 : 0
113-
114-
name = "EventsDeliveryDestination-${var.bus_name}-CWLogs"
97+
region = var.region
11598

116-
delivery_destination_configuration {
117-
destination_resource_arn = var.bus_log_config.cloudwatch.log_group_arn
118-
}
99+
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
119102

120103
tags = var.tags
121104
}
122105

123-
resource "aws_cloudwatch_log_delivery" "cwlogs" {
124-
count = (
125-
var.create &&
126-
var.create_bus &&
127-
var.bus_log_config != null &&
128-
var.bus_log_config.cloudwatch != null &&
129-
var.bus_log_config.cloudwatch.enabled
130-
) ? 1 : 0
131-
132-
delivery_destination_arn = aws_cloudwatch_log_delivery_destination.cwlogs[0].arn
133-
delivery_source_name = aws_cloudwatch_log_delivery_source.this[0].name
134-
}
106+
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) }
135108

136-
resource "aws_cloudwatch_log_delivery_destination" "s3" {
137-
count = (
138-
var.create &&
139-
var.create_bus &&
140-
var.bus_log_config != null &&
141-
var.bus_log_config.s3 != null &&
142-
var.bus_log_config.s3.enabled
143-
) ? 1 : 0
109+
region = var.region
144110

145-
name = "EventsDeliveryDestination-${var.bus_name}-S3"
111+
name = coalesce(each.value.name, "${var.bus_name}-${each.key}")
112+
output_format = try(each.value.output_format, null)
146113

147114
delivery_destination_configuration {
148-
destination_resource_arn = var.bus_log_config.s3.bucket_arn
115+
destination_resource_arn = each.value.arn
149116
}
150117

151118
tags = var.tags
152119
}
153120

154-
resource "aws_cloudwatch_log_delivery" "s3" {
155-
count = (
156-
var.create &&
157-
var.create_bus &&
158-
var.bus_log_config != null &&
159-
var.bus_log_config.s3 != null &&
160-
var.bus_log_config.s3.enabled
161-
) ? 1 : 0
121+
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) }
123+
124+
region = var.region
162125

163-
delivery_destination_arn = aws_cloudwatch_log_delivery_destination.s3[0].arn
164126
delivery_source_name = aws_cloudwatch_log_delivery_source.this[0].name
165-
}
127+
delivery_destination_arn = aws_cloudwatch_log_delivery_destination.this[each.key].arn
166128

167-
resource "aws_cloudwatch_log_delivery_destination" "firehose" {
168-
count = (
169-
var.create &&
170-
var.create_bus &&
171-
var.bus_log_config != null &&
172-
var.bus_log_config.firehose != null &&
173-
var.bus_log_config.firehose.enabled
174-
) ? 1 : 0
129+
field_delimiter = each.value.field_delimiter
130+
record_fields = each.value.record_fields
175131

176-
name = "EventsDeliveryDestination-${var.bus_name}-Firehose"
132+
dynamic "s3_delivery_configuration" {
133+
for_each = try(each.value.s3_delivery_configuration, null) != null ? [true] : []
177134

178-
delivery_destination_configuration {
179-
destination_resource_arn = var.bus_log_config.firehose.delivery_stream_arn
135+
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+
}
180139
}
181140

182141
tags = var.tags
183142
}
184143

185-
resource "aws_cloudwatch_log_delivery" "firehose" {
186-
count = (
187-
var.create &&
188-
var.create_bus &&
189-
var.bus_log_config != null &&
190-
var.bus_log_config.firehose != null &&
191-
var.bus_log_config.firehose.enabled
192-
) ? 1 : 0
193-
194-
delivery_destination_arn = aws_cloudwatch_log_delivery_destination.firehose[0].arn
195-
delivery_source_name = aws_cloudwatch_log_delivery_source.this[0].name
196-
}
197-
198144
resource "aws_schemas_discoverer" "this" {
199145
count = var.create && var.create_schemas_discoverer ? 1 : 0
200146

0 commit comments

Comments
 (0)