diff --git a/infrastructure/modules/eventbridge/README.md b/infrastructure/modules/eventbridge/README.md index f858fc6a..29064c09 100644 --- a/infrastructure/modules/eventbridge/README.md +++ b/infrastructure/modules/eventbridge/README.md @@ -150,6 +150,38 @@ With a context ID of `bcss-test-application-integration-3`, this creates the gro `bcss-test-application-integration-3-hours-oracle-stop`, and the role `/bcss/bcss/test/bcss-test-application-integration-3-hours`. +### Bus name behaviour when disabled + +`enabled = false` prevents the module from creating resources, but the wrapped +EventBridge module still looks up a bus. The named bus must therefore already +exist in the configured AWS account and region, and the caller must be allowed +to describe it. `create_bus = false` also looks up the named bus rather than +creating one. + +The `bus_name` fallback depends on whether the module is enabled: + +- When enabled, an omitted `bus_name` defaults to the context ID. Set + `create_bus = false` and `bus_name = "default"` to use the existing default + bus, as in the scheduler example above. +- When disabled, an omitted `bus_name` defaults to `"default"`. Set + `bus_name` to another name only when that existing bus should be looked up. +- An explicitly supplied `bus_name` is retained in either mode. + +For example, this disables resource creation while retaining a valid lookup of +the existing default bus: + + module "optional_events" { + source = "git::https://github.com/NHSDigital/screening-terraform-modules-aws.git//infrastructure/modules/eventbridge?ref=" + + service = "bcss" + environment = "test" + name = "optional-events" + enabled = false + + create_bus = false + bus_name = "default" + } + ## Conventions - Pass a pinned release ref and supply the required customer-managed KMS keys. @@ -230,7 +262,7 @@ Wildcards and aliases are not accepted for this policy. | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | | [aws\_region](#input\_aws\_region) | The AWS region | `string` | `"eu-west-2"` | no | | [bus\_description](#input\_bus\_description) | Event bus description | `string` | `null` | no | -| [bus\_name](#input\_bus\_name) | A unique name for your EventBridge Bus. Must be unique per AWS account and region. Defaults to whatever the tags module produces | `string` | `null` | no | +| [bus\_name](#input\_bus\_name) | Name of the EventBridge bus. Defaults to the context ID when enabled and to the existing default bus when disabled. Must be unique per AWS account and region when creating a bus. | `string` | `null` | no | | [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 | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"project": null,
"regex_replace_chars": null,
"region": null,
"service": null,
"stack": null,
"tags": {},
"terraform_source": null,
"workspace": null
}
| no | | [create\_archives](#input\_create\_archives) | Controls whether EventBridge Archive resources should be created | `bool` | `false` | no | diff --git a/infrastructure/modules/eventbridge/locals.tf b/infrastructure/modules/eventbridge/locals.tf index 3463522d..225a27a3 100644 --- a/infrastructure/modules/eventbridge/locals.tf +++ b/infrastructure/modules/eventbridge/locals.tf @@ -1,15 +1,7 @@ locals { # allow bus name to be overridden without touching tags - bus_name = ( - # `module.this.id` is `""` (an error) when `module.this.enabled` is false - # `coalesce()` errors if all arguments are null - module.this.enabled - ? coalesce( - var.bus_name, - module.this.enabled ? module.this.id : null, - ) - : null - ) + # The child still looks up a bus when disabled, so always pass an existing bus name. + bus_name = coalesce(var.bus_name, module.this.enabled ? module.this.id : "default") # The community module otherwise names the role after the bus, giving "default" on the default bus. role_name = module.this.enabled ? coalesce(var.role_name, module.this.id) : null diff --git a/infrastructure/modules/eventbridge/tests/eventbridge.tftest.hcl b/infrastructure/modules/eventbridge/tests/eventbridge.tftest.hcl index 39fb26b6..0451b578 100644 --- a/infrastructure/modules/eventbridge/tests/eventbridge.tftest.hcl +++ b/infrastructure/modules/eventbridge/tests/eventbridge.tftest.hcl @@ -38,6 +38,34 @@ variables { } } +run "disabled_module_keeps_existing_bus_name" { + command = plan + + variables { + enabled = false + bus_name = "existing-bus" + } + + assert { + condition = local.bus_name == var.bus_name + error_message = "A disabled module must preserve the explicitly configured bus name for the child lookup." + } +} + +run "disabled_module_defaults_bus_name" { + command = plan + + variables { + enabled = false + bus_name = null + } + + assert { + condition = local.bus_name == "default" + error_message = "A disabled module without an explicit bus name must use the default bus." + } +} + run "schedule_names_are_context_prefixed" { command = plan diff --git a/infrastructure/modules/eventbridge/variables.tf b/infrastructure/modules/eventbridge/variables.tf index bbabf9b5..e260afed 100644 --- a/infrastructure/modules/eventbridge/variables.tf +++ b/infrastructure/modules/eventbridge/variables.tf @@ -113,7 +113,7 @@ variable "append_pipe_postfix" { ################################################################ variable "bus_name" { - description = "A unique name for your EventBridge Bus. Must be unique per AWS account and region. Defaults to whatever the tags module produces" + description = "Name of the EventBridge bus. Defaults to the context ID when enabled and to the existing default bus when disabled. Must be unique per AWS account and region when creating a bus." type = string nullable = true default = null