|
| 1 | +# Don't use `admin` |
| 2 | +# Read more: <https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html> |
| 3 | +# ("MasterUsername admin cannot be used as it is a reserved word used by the engine") |
| 4 | +variable "postgres_admin_user" { |
| 5 | + type = "string" |
| 6 | + description = "Postgres admin user name" |
| 7 | + default = "" |
| 8 | +} |
| 9 | + |
| 10 | +# Must be longer than 8 chars |
| 11 | +# Read more: <https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html> |
| 12 | +# ("The parameter MasterUserPassword is not a valid password because it is shorter than 8 characters") |
| 13 | +variable "postgres_admin_password" { |
| 14 | + type = "string" |
| 15 | + description = "Postgres password for the admin user" |
| 16 | + default = "" |
| 17 | +} |
| 18 | + |
| 19 | +variable "postgres_db_name" { |
| 20 | + type = "string" |
| 21 | + description = "Postgres database name" |
| 22 | + default = "" |
| 23 | +} |
| 24 | + |
| 25 | +# db.r4.large is the smallest instance type supported by Aurora Postgres |
| 26 | +# https://aws.amazon.com/rds/aurora/# |
| 27 | +variable "postgres_instance_type" { |
| 28 | + type = "string" |
| 29 | + default = "db.r4.large" |
| 30 | + description = "EC2 instance type for Postgres cluster" |
| 31 | +} |
| 32 | + |
| 33 | +variable "postgres_cluster_size" { |
| 34 | + type = "string" |
| 35 | + default = "2" |
| 36 | + description = "Postgres cluster size" |
| 37 | +} |
| 38 | + |
| 39 | +variable "postgres_cluster_enabled" { |
| 40 | + type = "string" |
| 41 | + default = "" |
| 42 | + description = "Set to false to prevent the module from creating any resources" |
| 43 | +} |
| 44 | + |
| 45 | +variable "postgres_cluster_family" { |
| 46 | + type = "string" |
| 47 | + default = "aurora-postgresql9.6" |
| 48 | + description = "Postgres cluster DB family. Currently supported values are `aurora-postgresql9.6` and `aurora-postgresql10`" |
| 49 | +} |
| 50 | + |
| 51 | +variable "postgres_maintenance_window" { |
| 52 | + type = "string" |
| 53 | + default = "sun:03:00-sun:04:00" |
| 54 | + description = "Weekly time range during which system maintenance can occur, in UTC" |
| 55 | +} |
| 56 | + |
| 57 | +locals { |
| 58 | + postgres_cluster_enabled = "${var.postgres_cluster_enabled != "" ? var.postgres_cluster_enabled : var.enabled}" |
| 59 | + postgres_admin_user = "${length(var.postgres_admin_user) > 0 ? var.postgres_admin_user : join("", random_string.postgres_admin_user.*.result)}" |
| 60 | + postgres_admin_password = "${length(var.postgres_admin_password) > 0 ? var.postgres_admin_password : join("", random_string.postgres_admin_password.*.result)}" |
| 61 | + postgres_db_name = "${var.postgres_db_name != "" ? var.postgres_db_name : join("", random_pet.postgres_db_name.*.id)}" |
| 62 | +} |
| 63 | + |
| 64 | +module "aurora_postgres" { |
| 65 | + source = "git::https://github.com/cloudposse/terraform-aws-rds-cluster.git?ref=tags/0.10.0" |
| 66 | + namespace = "${var.namespace}" |
| 67 | + stage = "${var.stage}" |
| 68 | + name = "${var.name}" |
| 69 | + attributes = ["postgresql"] |
| 70 | + engine = "aurora-postgresql" |
| 71 | + cluster_family = "${var.postgres_cluster_family}" |
| 72 | + instance_type = "${var.postgres_instance_type}" |
| 73 | + cluster_size = "${var.postgres_cluster_size}" |
| 74 | + admin_user = "${local.postgres_admin_user}" |
| 75 | + admin_password = "${local.postgres_admin_password}" |
| 76 | + db_name = "${local.postgres_db_name}" |
| 77 | + db_port = "5432" |
| 78 | + maintenance_window = "${var.postgres_maintenance_window}" |
| 79 | + vpc_id = "${var.vpc_id}" |
| 80 | + subnets = ["${var.subnet_ids}"] |
| 81 | + zone_id = "${local.zone_id}" |
| 82 | + security_groups = ["${var.security_groups}"] |
| 83 | + enabled = "${local.postgres_cluster_enabled}" |
| 84 | +} |
| 85 | + |
| 86 | +resource "random_pet" "postgres_db_name" { |
| 87 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 88 | + separator = "_" |
| 89 | +} |
| 90 | + |
| 91 | +resource "random_string" "postgres_admin_user" { |
| 92 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 93 | + length = 8 |
| 94 | + special = false |
| 95 | + number = false |
| 96 | +} |
| 97 | + |
| 98 | +resource "random_string" "postgres_admin_password" { |
| 99 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 100 | + length = 16 |
| 101 | + special = true |
| 102 | +} |
| 103 | + |
| 104 | +resource "aws_ssm_parameter" "aurora_postgres_database_name" { |
| 105 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 106 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_database_name")}" |
| 107 | + value = "${module.aurora_postgres.name}" |
| 108 | + description = "Aurora Postgres Database Name" |
| 109 | + type = "String" |
| 110 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 111 | +} |
| 112 | + |
| 113 | +resource "aws_ssm_parameter" "aurora_postgres_master_username" { |
| 114 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 115 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_master_username")}" |
| 116 | + value = "${module.aurora_postgres.user}" |
| 117 | + description = "Aurora Postgres Username for the master DB user" |
| 118 | + type = "String" |
| 119 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 120 | +} |
| 121 | + |
| 122 | +resource "aws_ssm_parameter" "aurora_postgres_master_password" { |
| 123 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 124 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_master_password")}" |
| 125 | + value = "${module.aurora_postgres.password}" |
| 126 | + description = "Aurora Postgres Password for the master DB user" |
| 127 | + type = "SecureString" |
| 128 | + key_id = "${data.aws_kms_key.chamber_kms_key.id}" |
| 129 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 130 | +} |
| 131 | + |
| 132 | +resource "aws_ssm_parameter" "aurora_postgres_master_hostname" { |
| 133 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 134 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_master_hostname")}" |
| 135 | + value = "${module.aurora_postgres.master_host}" |
| 136 | + description = "Aurora Postgres DB Master hostname" |
| 137 | + type = "String" |
| 138 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 139 | +} |
| 140 | + |
| 141 | +resource "aws_ssm_parameter" "aurora_postgres_replicas_hostname" { |
| 142 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 143 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_replicas_hostname")}" |
| 144 | + value = "${module.aurora_postgres.replicas_host}" |
| 145 | + description = "Aurora Postgres DB Replicas hostname" |
| 146 | + type = "String" |
| 147 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 148 | +} |
| 149 | + |
| 150 | +resource "aws_ssm_parameter" "aurora_postgres_cluster_name" { |
| 151 | + count = "${local.postgres_cluster_enabled == "true" ? 1 : 0}" |
| 152 | + name = "${format(var.chamber_format, local.chamber_service, "aurora_postgres_cluster_name")}" |
| 153 | + value = "${module.aurora_postgres.cluster_name}" |
| 154 | + description = "Aurora Postgres DB Cluster Identifier" |
| 155 | + type = "String" |
| 156 | + overwrite = "${var.overwrite_ssm_parameter}" |
| 157 | +} |
| 158 | + |
| 159 | +output "aurora_postgres_database_name" { |
| 160 | + value = "${local.postgres_cluster_enabled == "true" ? module.aurora_postgres.name : ""}" |
| 161 | + description = "Aurora Postgres Database name" |
| 162 | +} |
| 163 | + |
| 164 | +output "aurora_postgres_master_username" { |
| 165 | + value = "${local.postgres_cluster_enabled == "true" ? module.aurora_postgres.user : ""}" |
| 166 | + description = "Aurora Postgres Username for the master DB user" |
| 167 | +} |
| 168 | + |
| 169 | +output "aurora_postgres_master_hostname" { |
| 170 | + value = "${local.postgres_cluster_enabled == "true" ? module.aurora_postgres.master_host : ""}" |
| 171 | + description = "Aurora Postgres DB Master hostname" |
| 172 | +} |
| 173 | + |
| 174 | +output "aurora_postgres_replicas_hostname" { |
| 175 | + value = "${local.postgres_cluster_enabled == "true" ? module.aurora_postgres.replicas_host : ""}" |
| 176 | + description = "Aurora Postgres Replicas hostname" |
| 177 | +} |
| 178 | + |
| 179 | +output "aurora_postgres_cluster_name" { |
| 180 | + value = "${local.postgres_cluster_enabled == "true" ? module.aurora_postgres.cluster_name : ""}" |
| 181 | + description = "Aurora Postgres Cluster Identifier" |
| 182 | +} |
0 commit comments