Skip to content

Commit

Permalink
Update eventbridge-event-bus module to support default event bus (#14)
Browse files Browse the repository at this point in the history
* Update eventbridge-event-bus module to support default event bus

* Add eventbridge-rule module
  • Loading branch information
posquit0 authored Dec 20, 2023
1 parent 8b14c34 commit d66d1e2
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/labeler.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Modules
":floppy_disk: eventbridge-event-bus":
- modules/eventbridge-event-bus/**/*
":floppy_disk: eventbridge-rule":
- modules/eventbridge-rule/**/*
":floppy_disk: msk-cluster":
- modules/msk-cluster/**/*
":floppy_disk: sns-fifo-topic":
Expand Down
3 changes: 3 additions & 0 deletions .github/labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
- color: "fbca04"
description: "This issue or pull request is related to eventbridge-event-bus module."
name: ":floppy_disk: eventbridge-event-bus"
- color: "fbca04"
description: "This issue or pull request is related to eventbridge-rule module."
name: ":floppy_disk: eventbridge-rule"
- color: "fbca04"
description: "This issue or pull request is related to msk-cluster module."
name: ":floppy_disk: msk-cluster"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Terraform module which creates messaging related resources on AWS.

- [eventbridge-event-bus](./modules/eventbridge-event-bus)
- [eventbridge-rule](./modules/eventbridge-rule)
- [msk-cluster](./modules/msk-cluster)
- [sns-fifo-topic](./modules/sns-fifo-topic)
- [sns-standard-topic](./modules/sns-standard-topic)
Expand All @@ -18,6 +19,7 @@ Terraform Modules from [this package](https://github.com/tedilabs/terraform-aws-

- **AWS EventBridge (Formerly known as CloudWatch Events)**
- Event Bus
- Rule
- **AWS MSK (Managed Streaming for Apache Kafka)**
- Cluster
- **AWS SNS (Simple Notification Service)**
Expand Down
9 changes: 5 additions & 4 deletions modules/eventbridge-event-bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This module creates following resources.

- `aws_cloudwatch_event_bus`
- `aws_cloudwatch_event_bus` (optional)
- `aws_cloudwatch_event_bus_policy` (optional)
- `aws_cloudwatch_event_archive` (optional)
- `aws_schemas_discoverer` (optional)
Expand All @@ -19,7 +19,7 @@ This module creates following resources.

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.19.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.31.0 |

## Modules

Expand All @@ -35,14 +35,15 @@ This module creates following resources.
| [aws_cloudwatch_event_bus.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_bus) | resource |
| [aws_cloudwatch_event_bus_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_bus_policy) | resource |
| [aws_schemas_discoverer.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/schemas_discoverer) | resource |
| [aws_cloudwatch_event_bus.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudwatch_event_bus) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_name"></a> [name](#input\_name) | (Required) The name of the new event bus. The name of custom event bus can't contain the `/` character, but you can use the `/` character in partner event bus names. You can't use the name `default` for a custom event bus, as this name is already used for your account's default event bus. | `string` | n/a | yes |
| <a name="input_archives"></a> [archives](#input\_archives) | (Required) The configuration to create archives for the event bus. Events are continuously saved in archives, and individual events will be deleted after the retention period. An archive will persist until you manually delete it. Each block of `archives` as defined below.<br> (Required) `name` - The name of the new event archive. Maximum of 48 characters consisting of numbers, lower/upper case letters, `.`, `-`, `_`. You can't change the name of the archive after it is created.<br> (Optional) `description` - The description of the new event archive.<br> (Optional) `retention_in_days` - The maximum number of days to retain events in the new event archive. `0` is equivalent to Indefinite. The maximum is 2 billion days. Defaults to `0`.<br> (Optional) `event_pattern` - An event pattern to use to filter events sent to the archive. | <pre>list(object({<br> name = string<br> description = optional(string, "Managed by Terraform.")<br> retention_in_days = optional(number, 0)<br> event_pattern = optional(string)<br> }))</pre> | `[]` | no |
| <a name="input_archives"></a> [archives](#input\_archives) | (Required) The configuration to create archives for the event bus. Events are continuously saved in archives, and individual events will be deleted after the retention period. An archive will persist until you manually delete it. Each block of `archives` as defined below.<br> (Required) `name` - The name of the new event archive. Maximum of 48 characters consisting of numbers, lower/upper case letters, `.`, `-`, `_`. You can't change the name of the archive after it is created.<br> (Optional) `description` - The description of the new event archive.<br> (Optional) `retention_in_days` - The maximum number of days to retain events in the new event archive. `0` is equivalent to Indefinite. The maximum is 2 billion days. Defaults to `0`.<br> (Optional) `event_pattern` - An event pattern to use to filter events sent to the archive. All events from the source will be archived when `event_pattern` is not provided. | <pre>list(object({<br> name = string<br> description = optional(string, "Managed by Terraform.")<br> retention_in_days = optional(number, 0)<br> event_pattern = optional(string)<br> }))</pre> | `[]` | no |
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
| <a name="input_name"></a> [name](#input\_name) | (Optional) The name of the new event bus. The name of custom event bus can't contain the `/` character, but you can use the `/` character in partner event bus names. You can't use the name `default` for a custom event bus, as this name is already used for your account's default event bus. If the value is `default`, it will load the `default` event bus that already exists instead of creating a new one. Defaults to `default`. | `string` | `"default"` | no |
| <a name="input_policy"></a> [policy](#input\_policy) | (Optional) A valid policy JSON document. The resource-based policy defines who can access your event bus. By default, only the event bus owner can send events to the event bus. | `string` | `null` | no |
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
Expand Down
22 changes: 19 additions & 3 deletions modules/eventbridge-event-bus/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ locals {
} : {}
}

locals {
is_default = var.name == "default"
event_bus = (local.is_default
? data.aws_cloudwatch_event_bus.default[0]
: aws_cloudwatch_event_bus.this[0]
)
}


###################################################
# Event Bus on EventBridge
###################################################

data "aws_cloudwatch_event_bus" "default" {
count = local.is_default ? 1 : 0

name = "default"
}

resource "aws_cloudwatch_event_bus" "this" {
count = local.is_default ? 0 : 1

name = var.name
event_source_name = startswith(var.name, "aws.partner/") ? var.name : null

Expand All @@ -40,7 +56,7 @@ resource "aws_cloudwatch_event_bus" "this" {
resource "aws_cloudwatch_event_bus_policy" "this" {
count = var.policy != null ? 1 : 0

event_bus_name = aws_cloudwatch_event_bus.this.name
event_bus_name = local.event_bus.name
policy = var.policy
}

Expand All @@ -55,7 +71,7 @@ resource "aws_cloudwatch_event_archive" "this" {
archive.name => archive
}

event_source_arn = aws_cloudwatch_event_bus.this.arn
event_source_arn = local.event_bus.arn

name = each.key
description = each.value.description
Expand All @@ -72,7 +88,7 @@ resource "aws_cloudwatch_event_archive" "this" {
resource "aws_schemas_discoverer" "this" {
count = var.schema_discovery.enabled ? 1 : 0

source_arn = aws_cloudwatch_event_bus.this.arn
source_arn = local.event_bus.arn
description = var.schema_discovery.description

tags = merge(
Expand Down
6 changes: 3 additions & 3 deletions modules/eventbridge-event-bus/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
output "id" {
description = "The unique identifier for the event bus."
value = aws_cloudwatch_event_bus.this.id
value = local.event_bus.id
}

output "arn" {
description = "The Amazon Resource Name (ARN) of the event bus."
value = aws_cloudwatch_event_bus.this.arn
value = local.event_bus.arn
}

output "name" {
description = "The name of the event bus."
value = aws_cloudwatch_event_bus.this.name
value = local.event_bus.name
}

output "archives" {
Expand Down
5 changes: 3 additions & 2 deletions modules/eventbridge-event-bus/variables.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
variable "name" {
description = "(Required) The name of the new event bus. The name of custom event bus can't contain the `/` character, but you can use the `/` character in partner event bus names. You can't use the name `default` for a custom event bus, as this name is already used for your account's default event bus."
description = "(Optional) The name of the new event bus. The name of custom event bus can't contain the `/` character, but you can use the `/` character in partner event bus names. You can't use the name `default` for a custom event bus, as this name is already used for your account's default event bus. If the value is `default`, it will load the `default` event bus that already exists instead of creating a new one. Defaults to `default`."
type = string
default = "default"
nullable = false
}

Expand All @@ -16,7 +17,7 @@ variable "archives" {
(Required) `name` - The name of the new event archive. Maximum of 48 characters consisting of numbers, lower/upper case letters, `.`, `-`, `_`. You can't change the name of the archive after it is created.
(Optional) `description` - The description of the new event archive.
(Optional) `retention_in_days` - The maximum number of days to retain events in the new event archive. `0` is equivalent to Indefinite. The maximum is 2 billion days. Defaults to `0`.
(Optional) `event_pattern` - An event pattern to use to filter events sent to the archive.
(Optional) `event_pattern` - An event pattern to use to filter events sent to the archive. All events from the source will be archived when `event_pattern` is not provided.
EOF
type = list(object({
name = string
Expand Down
61 changes: 61 additions & 0 deletions modules/eventbridge-rule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# eventbridge-rule

This module creates following resources.

- `aws_cloudwatch_event_rule`
- `aws_cloudwatch_event_target` (optional)

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.6 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.27 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.31.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_resource_group"></a> [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.10.0 |

## Resources

| Name | Type |
|------|------|
| [aws_cloudwatch_event_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_event_rule) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_name"></a> [name](#input\_name) | (Required) A name of the rule for the event bus. | `string` | n/a | yes |
| <a name="input_description"></a> [description](#input\_description) | (Optional) The description of the rule. | `string` | `"Managed by Terraform."` | no |
| <a name="input_event_bus"></a> [event\_bus](#input\_event\_bus) | (Optional) The name or ARN of the event bus to associate with this rule. If you omit this, the `default` event bus is used. | `string` | `"default"` | no |
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
| <a name="input_state"></a> [state](#input\_state) | (Optional) The state of the rule. Valid values are `DISABLED`, `ENABLED`, and `ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS`. Defaults to `ENABLED`.<br> `DISABLED` - The rule is disabled. EventBridge does not match any events against the rule.<br> `ENABLED` - The rule is enabled. EventBridge matches events against the rule, except for Amazon Web Services management events delivered through CloudTrail.<br> `ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS` - The rule is enabled for all events, including Amazon Web Services management events delivered through CloudTrail. Management events provide visibility into management operations that are performed on resources in your Amazon Web Services account. These are also known as control plane operations. This value is only valid for rules on the default event bus or custom event buses. It does not apply to partner event buses. | `string` | `"ENABLED"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
| <a name="input_trigger"></a> [trigger](#input\_trigger) | (Required) The configuration for the rule trigger. At least one of `schedule_expression` or `event_pattern` is required. `trigger` as defined below.<br> (Optional) `event_pattern` - The event pattern to trigger when an event matching the pattern occurs. This is described in a JSON object. The `event_pattern` size is 2048 by default but it is adjustable up to 4096 characters by submitting a service quota increase request.<br> (Optional) `schedule_expression` - The scheduling expression. For example, `cron(0 20 * * ? *)` or `rate(5 minutes)`. Can only be used on the default event bus. | <pre>object({<br> event_pattern = optional(string)<br> schedule_expression = optional(string)<br> })</pre> | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_arn"></a> [arn](#output\_arn) | The Amazon Resource Name (ARN) of the rule. |
| <a name="output_description"></a> [description](#output\_description) | The description of the rule. |
| <a name="output_event_bus"></a> [event\_bus](#output\_event\_bus) | The name of the event bus. |
| <a name="output_id"></a> [id](#output\_id) | The unique identifier for the rule. |
| <a name="output_name"></a> [name](#output\_name) | The name of the rule. |
| <a name="output_state"></a> [state](#output\_state) | The state of the rule. |
| <a name="output_targets"></a> [targets](#output\_targets) | A list of archives for the event bus. |
| <a name="output_trigger"></a> [trigger](#output\_trigger) | The configuration for the rule trriger. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
58 changes: 58 additions & 0 deletions modules/eventbridge-rule/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
locals {
metadata = {
package = "terraform-aws-messaging"
version = trimspace(file("${path.module}/../../VERSION"))
module = basename(path.module)
name = var.name
}
module_tags = var.module_tags_enabled ? {
"module.terraform.io/package" = local.metadata.package
"module.terraform.io/version" = local.metadata.version
"module.terraform.io/name" = local.metadata.module
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
"module.terraform.io/instance" = local.metadata.name
} : {}
}


###################################################
# Rule of Event Bus on EventBridge
###################################################

# role_arn - (Optional) The Amazon Resource Name (ARN) associated with the role that is used for target invocation.
# INFO: Not supported attributes
# - `is_enabled`
# - `name_prefix`
resource "aws_cloudwatch_event_rule" "this" {
event_bus_name = var.event_bus

name = var.name
description = var.description
state = var.state


## Triggers
event_pattern = var.trigger.event_pattern
schedule_expression = var.trigger.schedule_expression


tags = merge(
{
"Name" = local.metadata.name
},
local.module_tags,
var.tags,
)
}


###################################################
# Rule Targets
###################################################

# resource "aws_cloudwatch_event_target" "this" {
# count = var.policy != null ? 1 : 0
#
# event_bus_name = aws_cloudwatch_event_bus.this.name
# policy = var.policy
# }
50 changes: 50 additions & 0 deletions modules/eventbridge-rule/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
output "id" {
description = "The unique identifier for the rule."
value = aws_cloudwatch_event_rule.this.id
}

output "arn" {
description = "The Amazon Resource Name (ARN) of the rule."
value = aws_cloudwatch_event_rule.this.arn
}

output "name" {
description = "The name of the rule."
value = aws_cloudwatch_event_rule.this.name
}

output "description" {
description = "The description of the rule."
value = aws_cloudwatch_event_rule.this.description
}

output "event_bus" {
description = "The name of the event bus."
value = aws_cloudwatch_event_rule.this.event_bus_name
}

output "state" {
description = "The state of the rule."
value = aws_cloudwatch_event_rule.this.state
}

output "trigger" {
description = "The configuration for the rule trriger."
value = {
event_pattern = aws_cloudwatch_event_rule.this.event_pattern
schedule_expression = aws_cloudwatch_event_rule.this.schedule_expression
}
}

output "targets" {
description = "A list of archives for the event bus."
value = [
# for target in aws_cloudwatch_event_target.this : {
# id = archive.id
# arn = archive.arn
# name = archive.name
# description = archive.description
# retention_in_days = archive.retention_days
# }
]
}
31 changes: 31 additions & 0 deletions modules/eventbridge-rule/resource-group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
locals {
resource_group_name = (var.resource_group_name != ""
? var.resource_group_name
: join(".", [
local.metadata.package,
local.metadata.module,
replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"),
])
)
}


module "resource_group" {
source = "tedilabs/misc/aws//modules/resource-group"
version = "~> 0.10.0"

count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0

name = local.resource_group_name
description = var.resource_group_description

query = {
resource_tags = local.module_tags
}

module_tags_enabled = false
tags = merge(
local.module_tags,
var.tags,
)
}
Loading

0 comments on commit d66d1e2

Please sign in to comment.