Commit 8a5af3e0 authored by Jim Razmus II's avatar Jim Razmus II Committed by Anton Babenko

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.
parent 33ff6b7b
......@@ -4,6 +4,7 @@
module "db_subnet_group" {
source = "./modules/db_subnet_group"
count = "${var.create_db_subnet_group}"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
subnet_ids = ["${var.subnet_ids}"]
......@@ -17,6 +18,7 @@ module "db_subnet_group" {
module "db_parameter_group" {
source = "./modules/db_parameter_group"
count = "${var.create_db_parameter_group}"
identifier = "${var.identifier}"
name_prefix = "${var.identifier}-"
family = "${var.family}"
......@@ -32,8 +34,8 @@ module "db_parameter_group" {
module "db_instance" {
source = "./modules/db_instance"
count = "${var.create_db_instance}"
identifier = "${var.identifier}"
engine = "${var.engine}"
engine_version = "${var.engine_version}"
instance_class = "${var.instance_class}"
......
......@@ -17,6 +17,8 @@ resource "aws_iam_role_policy_attachment" "enhanced_monitoring" {
}
resource "aws_db_instance" "this" {
count = "${var.count ? 1 : 0}"
identifier = "${var.identifier}"
engine = "${var.engine}"
......
# DB instance
output "this_db_instance_address" {
description = "The address of the RDS instance"
value = "${aws_db_instance.this.address}"
value = "${element(concat(aws_db_instance.this.*.address, list("")), 0)}"
}
output "this_db_instance_arn" {
description = "The ARN of the RDS instance"
value = "${aws_db_instance.this.arn}"
value = "${element(concat(aws_db_instance.this.*.arn, list("")), 0)}"
}
output "this_db_instance_availability_zone" {
description = "The availability zone of the RDS instance"
value = "${aws_db_instance.this.availability_zone}"
value = "${element(concat(aws_db_instance.this.*.availability_zone, list("")), 0)}"
}
output "this_db_instance_endpoint" {
description = "The connection endpoint"
value = "${aws_db_instance.this.endpoint}"
value = "${element(concat(aws_db_instance.this.*.endpoint, list("")), 0)}"
}
output "this_db_instance_hosted_zone_id" {
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
value = "${aws_db_instance.this.hosted_zone_id}"
value = "${element(concat(aws_db_instance.this.*.hosted_zone_id, list("")), 0)}"
}
output "this_db_instance_id" {
description = "The RDS instance ID"
value = "${aws_db_instance.this.id}"
value = "${element(concat(aws_db_instance.this.*.id, list("")), 0)}"
}
output "this_db_instance_resource_id" {
description = "The RDS Resource ID of this instance"
value = "${aws_db_instance.this.resource_id}"
value = "${element(concat(aws_db_instance.this.*.resource_id, list("")), 0)}"
}
output "this_db_instance_status" {
description = "The RDS instance status"
value = "${aws_db_instance.this.status}"
value = "${element(concat(aws_db_instance.this.*.status, list("")), 0)}"
}
output "this_db_instance_name" {
description = "The database name"
value = "${aws_db_instance.this.name}"
value = "${element(concat(aws_db_instance.this.*.name, list("")), 0)}"
}
output "this_db_instance_username" {
description = "The master username for the database"
value = "${aws_db_instance.this.username}"
value = "${element(concat(aws_db_instance.this.*.username, list("")), 0)}"
}
output "this_db_instance_password" {
......@@ -56,5 +56,5 @@ output "this_db_instance_password" {
output "this_db_instance_port" {
description = "The database port"
value = "${aws_db_instance.this.port}"
value = "${element(concat(aws_db_instance.this.*.port, list("")), 0)}"
}
variable "count" {
description = "Whether to create this resource or not?"
default = 1
}
variable "identifier" {
description = "The name of the RDS instance, if omitted, Terraform will assign a random, unique identifier"
}
......
......@@ -2,7 +2,7 @@
# DB parameter group
#####################
resource "aws_db_parameter_group" "this" {
count = "${var.count}"
count = "${var.count ? 1 : 0}"
name_prefix = "${var.name_prefix}"
description = "Database parameter group for ${var.identifier}"
......
......@@ -2,7 +2,7 @@
# DB subnet group
##################
resource "aws_db_subnet_group" "this" {
count = "${var.count}"
count = "${var.count ? 1 : 0}"
name_prefix = "${var.name_prefix}"
description = "Database subnet group for ${var.identifier}"
......
......@@ -185,3 +185,19 @@ variable "parameters" {
description = "A list of DB parameters (map) to apply"
default = []
}
variable "create_db_subnet_group" {
description = "Whether to create a database subnet group"
default = true
}
variable "create_db_parameter_group" {
description = "Whether to create a database parameter group"
default = true
}
variable "create_db_instance" {
description = "Whether to create a database instance"
default = true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment