Skip to content

Commit

Permalink
Merge pull request #2 from Young-ook/rename-var
Browse files Browse the repository at this point in the history
Rename variables
  • Loading branch information
Young-ook authored Aug 2, 2019
2 parents 51306a8 + 3dbb6ea commit 664eb52
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 134 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
9 changes: 4 additions & 5 deletions label.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# label.tf

resource "random_string" "this" {
resource "random_string" "suffix" {
length = 4
upper = false
lower = true
Expand All @@ -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}"
}
114 changes: 112 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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(""))}"]
}
113 changes: 0 additions & 113 deletions rds.tf

This file was deleted.

15 changes: 5 additions & 10 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}

Expand All @@ -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" {
Expand Down
3 changes: 3 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
required_version = ">= 0.11.0"
}

0 comments on commit 664eb52

Please sign in to comment.