Skip to content

Commit bd098f6

Browse files
authored
fix: added a fix to avoid errors in DA when using existing resources (#267)
1 parent a9a90ef commit bd098f6

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

solutions/standard/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
# Event Notifications solution
22

3-
This solution that configures the following infrastructure:
3+
When `existing_en_instance_crn` is not passed, this solution configures the following infrastructure:
44

5-
- Creates a resource group, if one is not passed in.
6-
- Provisions and configures an Event Notifications instance.
7-
- Provisions an IBM Cloud Object Storage instance to connect to an Event Notifications instance and collect events that fail delivery.
8-
- Configures KMS encryption by using an existing root key. Optionally creates a key ring and key in an existing instance.
5+
- a resource group, if one is not passed in.
6+
- optionally a KMS key ring and key for IBM Event Notifications encryption
7+
- optionally a KMS key ring and key for IBM Cloud Object Storage encryption
8+
- optionally an IBM Cloud Object Storage instance
9+
- optionally an IBM Cloud Object Storage bucket to collect events that fail delivery
10+
- an IBM Event Notifications instance
11+
12+
When `existing_en_instance_crn` is passed, this solution ignores ALL other inputs and sets the outputs based on the CRN.
13+
14+
- required inputs MUST still be set, but will be ignored.
915

1016
:exclamation: **Important:** This solution is not intended to be called by one or more other modules because it contains a provider configuration and is not compatible with the `for_each`, `count`, and `depends_on` arguments. For more information, see [Providers Within Modules](https://developer.hashicorp.com/terraform/language/modules/develop/providers).

solutions/standard/main.tf

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
########################################################################################################################
44

55
module "resource_group" {
6+
count = var.existing_en_instance_crn == null ? 1 : 0
67
source = "terraform-ibm-modules/resource-group/ibm"
78
version = "1.1.6"
89
resource_group_name = var.use_existing_resource_group == false ? (var.prefix != null ? "${var.prefix}-${var.resource_group_name}" : var.resource_group_name) : null
@@ -14,6 +15,8 @@ module "resource_group" {
1415
#######################################################################################################################
1516

1617
locals {
18+
# If a KMS key is passed, or an existing EN CRN is passed; do not create keys
19+
create_kms_keys = var.existing_kms_root_key_crn != null || var.existing_en_instance_crn != null ? false : true
1720
parsed_existing_kms_root_key_crn = var.existing_kms_root_key_crn != null ? split(":", var.existing_kms_root_key_crn) : []
1821
existing_kms_root_key_id = length(local.parsed_existing_kms_root_key_crn) > 0 ? local.parsed_existing_kms_root_key_crn[length(local.parsed_existing_kms_root_key_crn) - 1] : null
1922
parsed_existing_kms_instance_crn = var.existing_kms_instance_crn != null ? split(":", var.existing_kms_instance_crn) : []
@@ -73,7 +76,7 @@ module "kms" {
7376
providers = {
7477
ibm = ibm.kms
7578
}
76-
count = var.existing_kms_root_key_crn != null ? 0 : 1 # no need to create any KMS resources if passing an existing key
79+
count = local.create_kms_keys ? 1 : 0
7780
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
7881
version = "4.15.13"
7982
create_key_protect_instance = false
@@ -118,24 +121,26 @@ module "kms" {
118121
#######################################################################################################################
119122

120123
locals {
124+
# If a bucket namme is passed, or an existing EN CRN is passed; do not create bucket (or instance)
125+
create_cos_bucket = var.existing_cos_bucket_name != null || var.existing_en_instance_crn != null ? false : true
121126
# tflint-ignore: terraform_unused_declarations
122127
validate_cos_regions = var.cos_bucket_region != null && var.cross_region_location != null ? tobool("Cannot provide values for var.cos_bucket_region and var.cross_region_location") : true
123-
cos_bucket_name = var.existing_cos_bucket_name != null ? var.existing_cos_bucket_name : (var.prefix != null ? "${var.prefix}-${var.cos_bucket_name}" : var.cos_bucket_name)
124-
cos_bucket_name_with_suffix = var.existing_cos_bucket_name != null ? var.existing_cos_bucket_name : module.cos[0].bucket_name
128+
cos_bucket_name = var.existing_cos_bucket_name != null ? var.existing_cos_bucket_name : local.create_cos_bucket ? (var.prefix != null ? "${var.prefix}-${var.cos_bucket_name}" : var.cos_bucket_name) : null
129+
cos_bucket_name_with_suffix = var.existing_cos_bucket_name != null ? var.existing_cos_bucket_name : local.create_cos_bucket ? module.cos[0].bucket_name : null
125130
cos_bucket_region = var.cos_bucket_region != null ? var.cos_bucket_region : var.cross_region_location != null ? null : var.region
126131
cos_instance_name = var.prefix != null ? "${var.prefix}-${var.cos_instance_name}" : var.cos_instance_name
127132
}
128133

129134
module "cos" {
130-
count = var.existing_cos_bucket_name != null ? 0 : 1
135+
count = local.create_cos_bucket ? 1 : 0
131136
source = "terraform-ibm-modules/cos/ibm"
132137
version = "8.11.13"
133138
create_cos_instance = var.existing_cos_instance_crn == null ? true : false
134-
create_cos_bucket = var.existing_cos_bucket_name == null ? true : false
139+
create_cos_bucket = local.create_cos_bucket
135140
existing_cos_instance_id = var.existing_cos_instance_crn
136141
skip_iam_authorization_policy = local.create_cross_account_auth_policy || var.skip_cos_kms_auth_policy
137142
add_bucket_name_suffix = var.add_bucket_name_suffix
138-
resource_group_id = module.resource_group.resource_group_id
143+
resource_group_id = module.resource_group[0].resource_group_id
139144
region = local.cos_bucket_region
140145
cross_region_location = var.cross_region_location
141146
cos_instance_name = local.cos_instance_name
@@ -158,9 +163,8 @@ module "cos" {
158163
########################################################################################################################
159164

160165
locals {
161-
# KMS Related
162-
existing_kms_instance_crn = var.existing_kms_instance_crn != null ? var.existing_kms_instance_crn : null
163-
cos_endpoint = var.existing_cos_bucket_name == null ? "https://${module.cos[0].s3_endpoint_public}" : var.existing_cos_endpoint
166+
# COS Related
167+
cos_endpoint = var.existing_cos_bucket_name == null ? (local.create_cos_bucket ? "https://${module.cos[0].s3_endpoint_public}" : null) : var.existing_cos_endpoint
164168
# Event Notification Related
165169
parsed_existing_en_instance_crn = var.existing_en_instance_crn != null ? split(":", var.existing_en_instance_crn) : []
166170
existing_en_guid = length(local.parsed_existing_en_instance_crn) > 0 ? local.parsed_existing_en_instance_crn[7] : null
@@ -174,7 +178,7 @@ data "ibm_resource_instance" "existing_en" {
174178
module "event_notifications" {
175179
count = var.existing_en_instance_crn != null ? 0 : 1
176180
source = "../.."
177-
resource_group_id = module.resource_group.resource_group_id
181+
resource_group_id = module.resource_group[0].resource_group_id
178182
region = var.region
179183
name = var.prefix != null ? "${var.prefix}-${var.event_notification_name}" : var.event_notification_name
180184
plan = var.service_plan
@@ -184,7 +188,7 @@ module "event_notifications" {
184188
# KMS Related
185189
kms_encryption_enabled = true
186190
kms_endpoint_url = var.kms_endpoint_url
187-
existing_kms_instance_crn = local.existing_kms_instance_crn
191+
existing_kms_instance_crn = var.existing_kms_instance_crn
188192
root_key_id = local.en_kms_key_id
189193
skip_en_kms_auth_policy = local.create_cross_account_auth_policy || var.skip_en_kms_auth_policy
190194
# COS Related

solutions/standard/moved.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ moved {
77
from = module.event_notifications
88
to = module.event_notifications[0]
99
}
10+
11+
moved {
12+
from = module.resource_group
13+
to = module.resource_group[0]
14+
}

0 commit comments

Comments
 (0)