Skip to content

Commit c184307

Browse files
author
Marc Portabella
committed
feat: conditional use of nameprefix for subnet and option groups
1 parent 9aff82b commit c184307

File tree

8 files changed

+87
-9
lines changed

8 files changed

+87
-9
lines changed

main.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ module "db_subnet_group" {
1515
create = local.enable_create_db_subnet_group
1616
identifier = var.identifier
1717
name_prefix = "${var.identifier}-"
18+
use_name_prefix = var.use_subnet_group_name_prefix
1819
subnet_ids = var.subnet_ids
19-
2020
tags = var.tags
2121
}
2222

@@ -42,6 +42,7 @@ module "db_option_group" {
4242
create = local.enable_create_db_option_group
4343
identifier = var.identifier
4444
name_prefix = "${var.identifier}-"
45+
use_name_prefix = var.use_option_group_name_prefix
4546
option_group_description = var.option_group_description
4647
engine_name = var.engine
4748
major_engine_version = var.major_engine_version

modules/db_option_group/main.tf

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
1+
resource "aws_db_option_group" "this_no_prefix" {
2+
count = var.create && false == var.use_name_prefix ? 1 : 0
3+
4+
option_group_description = var.option_group_description == "" ? format("Option group for %s", var.identifier) : var.option_group_description
5+
engine_name = var.engine_name
6+
major_engine_version = var.major_engine_version
7+
8+
dynamic "option" {
9+
for_each = var.options
10+
content {
11+
option_name = option.value.option_name
12+
port = lookup(option.value, "port", null)
13+
version = lookup(option.value, "version", null)
14+
db_security_group_memberships = lookup(option.value, "db_security_group_memberships", null)
15+
vpc_security_group_memberships = lookup(option.value, "vpc_security_group_memberships", null)
16+
17+
dynamic "option_settings" {
18+
for_each = lookup(option.value, "option_settings", [])
19+
content {
20+
name = lookup(option_settings.value, "name", null)
21+
value = lookup(option_settings.value, "value", null)
22+
}
23+
}
24+
}
25+
}
26+
27+
tags = merge(
28+
var.tags,
29+
{
30+
"Name" = format("%s", var.identifier)
31+
},
32+
)
33+
34+
timeouts {
35+
delete = lookup(var.timeouts, "delete", null)
36+
}
37+
38+
lifecycle {
39+
create_before_destroy = true
40+
}
41+
}
42+
143
resource "aws_db_option_group" "this" {
2-
count = var.create ? 1 : 0
44+
count = var.create && var.use_name_prefix ? 1 : 0
345

446
name_prefix = var.name_prefix
547
option_group_description = var.option_group_description == "" ? format("Option group for %s", var.identifier) : var.option_group_description
@@ -40,4 +82,3 @@ resource "aws_db_option_group" "this" {
4082
create_before_destroy = true
4183
}
4284
}
43-

modules/db_option_group/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
output "this_db_option_group_id" {
22
description = "The db option group id"
3-
value = element(concat(aws_db_option_group.this.*.id, [""]), 0)
3+
value = element(concat(aws_db_option_group.this.*.id, aws_db_option_group.this_no_prefix.*.id, [""]), 0)
44
}
55

66
output "this_db_option_group_arn" {
77
description = "The ARN of the db option group"
8-
value = element(concat(aws_db_option_group.this.*.arn, [""]), 0)
8+
value = element(concat(aws_db_option_group.this.*.arn, aws_db_option_group.this_no_prefix.*.arn, [""]), 0)
99
}
1010

modules/db_option_group/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ variable "name_prefix" {
99
type = string
1010
}
1111

12+
variable "use_name_prefix" {
13+
description = "Whether to use name_prefix or not"
14+
type = bool
15+
default = true
16+
}
17+
1218
variable "identifier" {
1319
description = "The identifier of the resource"
1420
type = string

modules/db_subnet_group/main.tf

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
resource "aws_db_subnet_group" "this_no_prefix" {
2+
count = var.create && false == var.use_name_prefix ? 1 : 0
3+
4+
description = "Database subnet group for ${var.identifier}"
5+
subnet_ids = var.subnet_ids
6+
7+
tags = merge(
8+
var.tags,
9+
{
10+
"Name" = format("%s", var.identifier)
11+
},
12+
)
13+
}
14+
15+
116
resource "aws_db_subnet_group" "this" {
2-
count = var.create ? 1 : 0
17+
count = var.create && var.use_name_prefix ? 1 : 0
318

419
name_prefix = var.name_prefix
520
description = "Database subnet group for ${var.identifier}"

modules/db_subnet_group/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
output "this_db_subnet_group_id" {
22
description = "The db subnet group name"
3-
value = element(concat(aws_db_subnet_group.this.*.id, [""]), 0)
3+
value = element(concat(aws_db_subnet_group.this.*.id, aws_db_subnet_group.this_no_prefix.*.id, [""]), 0)
44
}
55

66
output "this_db_subnet_group_arn" {
77
description = "The ARN of the db subnet group"
8-
value = element(concat(aws_db_subnet_group.this.*.arn, [""]), 0)
8+
value = element(concat(aws_db_subnet_group.this.*.arn, aws_db_subnet_group.this_no_prefix.*.arn, [""]), 0)
99
}
1010

modules/db_subnet_group/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ variable "name_prefix" {
99
type = string
1010
}
1111

12+
variable "use_name_prefix" {
13+
description = "Whether to use name_prefix or not"
14+
type = bool
15+
default = true
16+
}
17+
1218
variable "identifier" {
1319
description = "The identifier of the resource"
1420
type = string

variables.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,16 @@ variable "use_parameter_group_name_prefix" {
332332
type = bool
333333
default = true
334334
}
335-
335+
variable "use_option_group_name_prefix" {
336+
description = "Whether to use the option group name prefix or not"
337+
type = bool
338+
default = true
339+
}
340+
variable "use_subnet_group_name_prefix" {
341+
description = "Whether to use the subnet group name prefix or not"
342+
type = bool
343+
default = true
344+
}
336345
variable "performance_insights_enabled" {
337346
description = "Specifies whether Performance Insights are enabled"
338347
type = bool

0 commit comments

Comments
 (0)