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