Skip to content

Commit 8a5af3e

Browse files
jimrazmusantonbabenko
authored andcommitted
Support conditional creation for the database too. (#36)
* Support conditional creation for the database too. * Database instance outputs need to accommodate the case where count is zero. * Declare variables that allow conditional creation of the database, parameter group, and subnet group. Pass them from main.tf to the nested modules. * Consistently accommodate boolean like arguments.
1 parent 33ff6b7 commit 8a5af3e

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module "db_subnet_group" {
55
source = "./modules/db_subnet_group"
66

7+
count = "${var.create_db_subnet_group}"
78
identifier = "${var.identifier}"
89
name_prefix = "${var.identifier}-"
910
subnet_ids = ["${var.subnet_ids}"]
@@ -17,6 +18,7 @@ module "db_subnet_group" {
1718
module "db_parameter_group" {
1819
source = "./modules/db_parameter_group"
1920

21+
count = "${var.create_db_parameter_group}"
2022
identifier = "${var.identifier}"
2123
name_prefix = "${var.identifier}-"
2224
family = "${var.family}"
@@ -32,8 +34,8 @@ module "db_parameter_group" {
3234
module "db_instance" {
3335
source = "./modules/db_instance"
3436

35-
identifier = "${var.identifier}"
36-
37+
count = "${var.create_db_instance}"
38+
identifier = "${var.identifier}"
3739
engine = "${var.engine}"
3840
engine_version = "${var.engine_version}"
3941
instance_class = "${var.instance_class}"

modules/db_instance/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ resource "aws_iam_role_policy_attachment" "enhanced_monitoring" {
1717
}
1818

1919
resource "aws_db_instance" "this" {
20+
count = "${var.count ? 1 : 0}"
21+
2022
identifier = "${var.identifier}"
2123

2224
engine = "${var.engine}"

modules/db_instance/outputs.tf

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
11
# DB instance
22
output "this_db_instance_address" {
33
description = "The address of the RDS instance"
4-
value = "${aws_db_instance.this.address}"
4+
value = "${element(concat(aws_db_instance.this.*.address, list("")), 0)}"
55
}
66

77
output "this_db_instance_arn" {
88
description = "The ARN of the RDS instance"
9-
value = "${aws_db_instance.this.arn}"
9+
value = "${element(concat(aws_db_instance.this.*.arn, list("")), 0)}"
1010
}
1111

1212
output "this_db_instance_availability_zone" {
1313
description = "The availability zone of the RDS instance"
14-
value = "${aws_db_instance.this.availability_zone}"
14+
value = "${element(concat(aws_db_instance.this.*.availability_zone, list("")), 0)}"
1515
}
1616

1717
output "this_db_instance_endpoint" {
1818
description = "The connection endpoint"
19-
value = "${aws_db_instance.this.endpoint}"
19+
value = "${element(concat(aws_db_instance.this.*.endpoint, list("")), 0)}"
2020
}
2121

2222
output "this_db_instance_hosted_zone_id" {
2323
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
24-
value = "${aws_db_instance.this.hosted_zone_id}"
24+
value = "${element(concat(aws_db_instance.this.*.hosted_zone_id, list("")), 0)}"
2525
}
2626

2727
output "this_db_instance_id" {
2828
description = "The RDS instance ID"
29-
value = "${aws_db_instance.this.id}"
29+
value = "${element(concat(aws_db_instance.this.*.id, list("")), 0)}"
3030
}
3131

3232
output "this_db_instance_resource_id" {
3333
description = "The RDS Resource ID of this instance"
34-
value = "${aws_db_instance.this.resource_id}"
34+
value = "${element(concat(aws_db_instance.this.*.resource_id, list("")), 0)}"
3535
}
3636

3737
output "this_db_instance_status" {
3838
description = "The RDS instance status"
39-
value = "${aws_db_instance.this.status}"
39+
value = "${element(concat(aws_db_instance.this.*.status, list("")), 0)}"
4040
}
4141

4242
output "this_db_instance_name" {
4343
description = "The database name"
44-
value = "${aws_db_instance.this.name}"
44+
value = "${element(concat(aws_db_instance.this.*.name, list("")), 0)}"
4545
}
4646

4747
output "this_db_instance_username" {
4848
description = "The master username for the database"
49-
value = "${aws_db_instance.this.username}"
49+
value = "${element(concat(aws_db_instance.this.*.username, list("")), 0)}"
5050
}
5151

5252
output "this_db_instance_password" {
@@ -56,5 +56,5 @@ output "this_db_instance_password" {
5656

5757
output "this_db_instance_port" {
5858
description = "The database port"
59-
value = "${aws_db_instance.this.port}"
59+
value = "${element(concat(aws_db_instance.this.*.port, list("")), 0)}"
6060
}

modules/db_instance/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "count" {
2+
description = "Whether to create this resource or not?"
3+
default = 1
4+
}
5+
16
variable "identifier" {
27
description = "The name of the RDS instance, if omitted, Terraform will assign a random, unique identifier"
38
}

modules/db_parameter_group/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# DB parameter group
33
#####################
44
resource "aws_db_parameter_group" "this" {
5-
count = "${var.count}"
5+
count = "${var.count ? 1 : 0}"
66

77
name_prefix = "${var.name_prefix}"
88
description = "Database parameter group for ${var.identifier}"

modules/db_subnet_group/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# DB subnet group
33
##################
44
resource "aws_db_subnet_group" "this" {
5-
count = "${var.count}"
5+
count = "${var.count ? 1 : 0}"
66

77
name_prefix = "${var.name_prefix}"
88
description = "Database subnet group for ${var.identifier}"

variables.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,19 @@ variable "parameters" {
185185
description = "A list of DB parameters (map) to apply"
186186
default = []
187187
}
188+
189+
variable "create_db_subnet_group" {
190+
description = "Whether to create a database subnet group"
191+
default = true
192+
}
193+
194+
variable "create_db_parameter_group" {
195+
description = "Whether to create a database parameter group"
196+
default = true
197+
}
198+
199+
variable "create_db_instance" {
200+
description = "Whether to create a database instance"
201+
default = true
202+
}
203+

0 commit comments

Comments
 (0)