diff --git a/README.md b/README.md index 89ed8e3..d21e77b 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ You can use this module like as below example. ``` -odule "your_db" { - source = "terraform-aws-mysql" - version = "v1.0.0" +module "mysql" { + source = "tf-mod/mysql/aws" + version = "1.0.0" - app_name = "mysql" + name = "mysql" stack = "${var.stack}" vpc = "${module.vpc.id}" subnets = "${module.vpc.private_subnets}" diff --git a/label.tf b/label.tf index 8486b28..f25d032 100644 --- a/label.tf +++ b/label.tf @@ -1,6 +1,6 @@ # label.tf -resource "random_string" "this" { +resource "random_string" "suffix" { length = 4 upper = false lower = true @@ -10,8 +10,7 @@ resource "random_string" "this" { ### frigga naming rule locals { - name = "${join("-", compact(list(var.app_name, var.stack, var.app_detail, local.slug)))}" - slug = "${var.slug == "" ? random_string.this.result : var.slug}" - cluster_name = "${local.name}" - cluster_id = "${local.name}" + name = "${join("-", compact(list(var.name, var.stack, var.detail, random_string.suffix.result)))}" + cluster-name = "${local.name}" + cluster-id = "${local.name}" } diff --git a/main.tf b/main.tf index 7b29fe0..0577035 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,113 @@ -terraform { - required_version = ">= 0.11.0" +# rds.tf +# relational database service + +### security/firewall +resource "random_string" "password" { + length = 16 + special = true + override_special = "^" +} + +resource "aws_security_group" "db" { + name = "${local.name}-db" + description = "security group for ${local.name}-db" + vpc_id = "${var.vpc}" + + tags = "${merge( + map("Name", "${local.name}-db"), + var.tags) + }" +} + +resource "aws_security_group_rule" "db-ingress-rules" { + type = "ingress" + from_port = "${var.mysql_port}" + to_port = "${var.mysql_port}" + protocol = "tcp" + source_security_group_id = "${var.source_sg}" + security_group_id = "${aws_security_group.db.id}" +} + +### subnet group +resource "aws_db_subnet_group" "db" { + name = "${local.name}-db" + subnet_ids = ["${var.subnets}"] + tags = "${merge(map("Name", "${local.name}-db"), var.tags)}" +} + +### parameter groups +resource "aws_rds_cluster_parameter_group" "db" { + name = "${local.name}-db-cluster-params" + + family = "${format("aurora-mysql%s.%s", + element(split(".", var.mysql_version), 0), + element(split(".", var.mysql_version), 1) + )}" + + parameter = "${list( + map("name", "character_set_server", "value", "utf8"), + map("name", "character_set_client", "value", "utf8"), + )}" + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_db_parameter_group" "db" { + name = "${local.name}-db-params" + + family = "${format("aurora-mysql%s.%s", + element(split(".", var.mysql_version), 0), + element(split(".", var.mysql_version), 1) + )}" + + lifecycle { + create_before_destroy = true + } +} + +### rds (aurora) +resource "aws_rds_cluster" "db" { + cluster_identifier_prefix = "${local.cluster-id}-" + engine = "aurora-mysql" + engine_version = "${var.mysql_version}" + engine_mode = "provisioned" + port = "${var.mysql_port}" + skip_final_snapshot = "true" + database_name = "${var.mysql_db}" + master_username = "${var.mysql_master_user}" + master_password = "${random_string.password.result}" + snapshot_identifier = "${var.mysql_snapshot}" + backup_retention_period = "5" + db_subnet_group_name = "${aws_db_subnet_group.db.name}" + db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.db.name}" + vpc_security_group_ids = ["${aws_security_group.db.id}"] + tags = "${merge(map("Name", "${local.name}-db"), var.tags)}" + + lifecycle { + ignore_changes = ["snapshot_identifier", "master_password"] + create_before_destroy = true + } +} + +### instances +resource "aws_rds_cluster_instance" "db" { + count = "${var.mysql_node_count}" + identifier = "${local.cluster-id}-${count.index}" + cluster_identifier = "${aws_rds_cluster.db.id}" + instance_class = "${var.mysql_node_type}" + engine = "aurora-mysql" + engine_version = "${var.mysql_version}" + db_parameter_group_name = "${aws_db_parameter_group.db.name}" + db_subnet_group_name = "${aws_db_subnet_group.db.name}" +} + +### dns records +resource "aws_route53_record" "db" { + zone_id = "${var.dns_zone_id}" + name = "${local.cluster-id}-db.${var.dns_zone}" + type = "CNAME" + ttl = 300 + records = ["${coalescelist(aws_rds_cluster.db.*.endpoint, list(""))}"] } diff --git a/rds.tf b/rds.tf deleted file mode 100644 index fac6e7a..0000000 --- a/rds.tf +++ /dev/null @@ -1,113 +0,0 @@ -# rds.tf -# relational database service - -### security/firewall -resource "random_string" "password" { - length = 16 - special = true - override_special = "^" -} - -resource "aws_security_group" "db" { - name = "${local.name}-db" - description = "security group for ${local.name}-db" - vpc_id = "${var.vpc}" - - tags = "${merge( - map("Name", "${local.name}-db"), - var.tags) - }" -} - -resource "aws_security_group_rule" "db_ingress_rules" { - type = "ingress" - from_port = "${var.mysql_port}" - to_port = "${var.mysql_port}" - protocol = "tcp" - source_security_group_id = "${var.source_sg}" - security_group_id = "${aws_security_group.db.id}" -} - -### subnet group -resource "aws_db_subnet_group" "db" { - name = "${local.name}-db" - subnet_ids = ["${var.subnets}"] - tags = "${merge(map("Name", "${local.name}-db"), var.tags)}" -} - -### parameter groups -resource "aws_rds_cluster_parameter_group" "db" { - name = "${local.name}-db-cluster-params" - - family = "${format("aurora-mysql%s.%s", - element(split(".", var.mysql_version), 0), - element(split(".", var.mysql_version), 1) - )}" - - parameter = "${list( - map("name", "character_set_server", "value", "utf8"), - map("name", "character_set_client", "value", "utf8"), - )}" - - lifecycle { - create_before_destroy = true - } -} - -resource "aws_db_parameter_group" "db" { - name = "${local.name}-db-params" - - family = "${format("aurora-mysql%s.%s", - element(split(".", var.mysql_version), 0), - element(split(".", var.mysql_version), 1) - )}" - - lifecycle { - create_before_destroy = true - } -} - -### rds (aurora) -resource "aws_rds_cluster" "db" { - cluster_identifier_prefix = "${local.cluster_id}-" - engine = "aurora-mysql" - engine_version = "${var.mysql_version}" - engine_mode = "provisioned" - port = "${var.mysql_port}" - skip_final_snapshot = "true" - database_name = "${var.mysql_db}" - master_username = "${var.mysql_master_user}" - master_password = "${random_string.password.result}" - snapshot_identifier = "${var.mysql_snapshot}" - backup_retention_period = "5" - db_subnet_group_name = "${aws_db_subnet_group.db.name}" - db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.db.name}" - vpc_security_group_ids = ["${aws_security_group.db.id}"] - tags = "${merge(map("Name", "${local.name}-db"), var.tags)}" - - lifecycle { - ignore_changes = ["snapshot_identifier", "master_password"] - create_before_destroy = true - } -} - -### instances -resource "aws_rds_cluster_instance" "db" { - count = "${var.mysql_node_count}" - identifier = "${local.cluster_id}-${count.index}" - cluster_identifier = "${aws_rds_cluster.db.id}" - instance_class = "${var.mysql_node_type}" - engine = "aurora-mysql" - engine_version = "${var.mysql_version}" - db_parameter_group_name = "${aws_db_parameter_group.db.name}" - db_subnet_group_name = "${aws_db_subnet_group.db.name}" -} - -### dns records -resource "aws_route53_record" "db" { - zone_id = "${var.dns_zone_id}" - name = "${local.cluster_id}-db.${var.dns_zone}" - type = "CNAME" - ttl = 300 - records = ["${coalescelist(aws_rds_cluster.db.*.endpoint, list(""))}"] -} diff --git a/variables.tf b/variables.tf index 6ebcbbd..7c4b226 100644 --- a/variables.tf +++ b/variables.tf @@ -59,13 +59,13 @@ variable "tags" { } ### description -variable "app_name" { - description = "The logical name of the module instance" +variable "name" { + description = "The logical name" default = "mysql" } -variable "app_detail" { - description = "The extra description of module instance" +variable "detail" { + description = "The extra description" default = "" } @@ -74,14 +74,9 @@ variable "stack" { default = "default" } -variable "slug" { - description = "A random string to be end of tail of module name" - default = "" -} - ### dns variable "dns_zone" { - description = "The hosted zone name for internal dns, e.g., ${var.dns_zone}.internal" + description = "The hosted zone name for internal dns, e.g., app.internal" } variable "dns_zone_id" { diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..7b29fe0 --- /dev/null +++ b/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = ">= 0.11.0" +}