generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
411 lines (376 loc) · 17.7 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
##############################################################################
# ICD PostgreSQL module
# Input variable validation
# (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
#
# TODO: Replace with terraform cross variable validation: https://github.ibm.com/GoldenEye/issues/issues/10836
########################################################################################################################
locals {
# Validation (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
# tflint-ignore: terraform_unused_declarations
validate_kms_values = var.use_ibm_owned_encryption_key && (var.kms_key_crn != null || var.backup_encryption_key_crn != null) ? tobool("When passing values for 'kms_key_crn' or 'backup_encryption_key_crn', you must set 'use_ibm_owned_encryption_key' to false. Otherwise unset them to use default encryption.") : true
# tflint-ignore: terraform_unused_declarations
validate_kms_vars = !var.use_ibm_owned_encryption_key && var.kms_key_crn == null ? tobool("When setting 'use_ibm_owned_encryption_key' to false, a value must be passed for 'kms_key_crn'.") : true
# tflint-ignore: terraform_unused_declarations
validate_backup_key = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn != null && (var.use_default_backup_encryption_key || var.use_same_kms_key_for_backups) ? tobool("When passing a value for 'backup_encryption_key_crn' you cannot set 'use_default_backup_encryption_key' to true or 'use_ibm_owned_encryption_key' to false.") : true
# tflint-ignore: terraform_unused_declarations
validate_backup_key_2 = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn == null && !var.use_same_kms_key_for_backups ? tobool("When 'use_same_kms_key_for_backups' is set to false, a value needs to be passed for 'backup_encryption_key_crn'.") : true
# tflint-ignore: terraform_unused_declarations
validate_pitr_vars = (var.pitr_id != null && var.pitr_time == null) || (var.pitr_time != null && var.pitr_id == null) ? tobool("To use Point-In-Time Recovery (PITR), values for both var.pitr_id and var.pitr_time need to be set. Otherwise, unset both of these.") : true
# If 'use_ibm_owned_encryption_key' is true or 'use_default_backup_encryption_key' is true, default to null.
# If no value is passed for 'backup_encryption_key_crn', then default to use 'kms_key_crn'.
backup_encryption_key_crn = var.use_ibm_owned_encryption_key || var.use_default_backup_encryption_key ? null : (var.backup_encryption_key_crn != null ? var.backup_encryption_key_crn : var.kms_key_crn)
# Determine if auto scaling is enabled
auto_scaling_enabled = var.auto_scaling == null ? [] : [1]
# Determine if host_flavor is used
host_flavor_set = var.member_host_flavor != null ? true : false
# Determine if restore, from backup or point in time recovery
recovery_mode = var.backup_crn != null || var.pitr_id != null
}
########################################################################################################################
# Parse info from KMS key CRNs
########################################################################################################################
module "kms_key_crn_parser" {
count = var.use_ibm_owned_encryption_key ? 0 : 1
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
version = "1.1.0"
crn = var.kms_key_crn
}
module "backup_key_crn_parser" {
count = var.use_ibm_owned_encryption_key ? 0 : 1
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
version = "1.1.0"
crn = local.backup_encryption_key_crn
}
# Put parsed values into locals
locals {
kms_service = !var.use_ibm_owned_encryption_key ? module.kms_key_crn_parser[0].service_name : null
kms_account_id = !var.use_ibm_owned_encryption_key ? module.kms_key_crn_parser[0].account_id : null
kms_key_id = !var.use_ibm_owned_encryption_key ? module.kms_key_crn_parser[0].resource : null
kms_key_instance_guid = !var.use_ibm_owned_encryption_key ? module.kms_key_crn_parser[0].service_instance : null
backup_kms_service = !var.use_ibm_owned_encryption_key ? module.backup_key_crn_parser[0].service_name : null
backup_kms_account_id = !var.use_ibm_owned_encryption_key ? module.backup_key_crn_parser[0].account_id : null
backup_kms_key_id = !var.use_ibm_owned_encryption_key ? module.backup_key_crn_parser[0].resource : null
backup_kms_key_instance_guid = !var.use_ibm_owned_encryption_key ? module.backup_key_crn_parser[0].service_instance : null
}
########################################################################################################################
# KMS IAM Authorization Policies
########################################################################################################################
locals {
# only create auth policy if 'use_ibm_owned_encryption_key' is false, and 'skip_iam_authorization_policy' is false
create_kms_auth_policy = !var.use_ibm_owned_encryption_key && !var.skip_iam_authorization_policy ? 1 : 0
# only create backup auth policy if 'use_ibm_owned_encryption_key' is false, 'skip_iam_authorization_policy' is false and 'use_same_kms_key_for_backups' is false
create_backup_kms_auth_policy = !var.use_ibm_owned_encryption_key && !var.skip_iam_authorization_policy && !var.use_same_kms_key_for_backups ? 1 : 0
}
# Create IAM Authorization Policies to allow PostgreSQL to access KMS for the encryption key
resource "ibm_iam_authorization_policy" "kms_policy" {
count = local.create_kms_auth_policy
source_service_name = "databases-for-postgresql"
source_resource_group_id = var.resource_group_id
roles = ["Reader"]
description = "Allow all PostgreSQL instances in the resource group ${var.resource_group_id} to read the ${local.kms_service} key ${local.kms_key_id} from the instance GUID ${local.kms_key_instance_guid}"
resource_attributes {
name = "serviceName"
operator = "stringEquals"
value = local.kms_service
}
resource_attributes {
name = "accountId"
operator = "stringEquals"
value = local.kms_account_id
}
resource_attributes {
name = "serviceInstance"
operator = "stringEquals"
value = local.kms_key_instance_guid
}
resource_attributes {
name = "resourceType"
operator = "stringEquals"
value = "key"
}
resource_attributes {
name = "resource"
operator = "stringEquals"
value = local.kms_key_id
}
# Scope of policy now includes the key, so ensure to create new policy before
# destroying old one to prevent any disruption to every day services.
lifecycle {
create_before_destroy = true
}
}
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
resource "time_sleep" "wait_for_authorization_policy" {
count = local.create_kms_auth_policy
depends_on = [ibm_iam_authorization_policy.kms_policy]
create_duration = "30s"
}
resource "ibm_iam_authorization_policy" "backup_kms_policy" {
count = local.create_backup_kms_auth_policy
source_service_name = "databases-for-postgresql"
source_resource_group_id = var.resource_group_id
roles = ["Reader"]
description = "Allow all PostgreSQL instances in the Resource Group ${var.resource_group_id} to read the ${local.backup_kms_service} key ${local.backup_kms_key_id} from the instance GUID ${local.backup_kms_key_instance_guid}"
resource_attributes {
name = "serviceName"
operator = "stringEquals"
value = local.backup_kms_service
}
resource_attributes {
name = "accountId"
operator = "stringEquals"
value = local.backup_kms_account_id
}
resource_attributes {
name = "serviceInstance"
operator = "stringEquals"
value = local.backup_kms_key_instance_guid
}
resource_attributes {
name = "resourceType"
operator = "stringEquals"
value = "key"
}
resource_attributes {
name = "resource"
operator = "stringEquals"
value = local.backup_kms_key_id
}
# Scope of policy now includes the key, so ensure to create new policy before
# destroying old one to prevent any disruption to every day services.
lifecycle {
create_before_destroy = true
}
}
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
resource "time_sleep" "wait_for_backup_kms_authorization_policy" {
count = local.create_backup_kms_auth_policy
depends_on = [ibm_iam_authorization_policy.backup_kms_policy]
create_duration = "30s"
}
########################################################################################################################
# Postgresql instance
########################################################################################################################
# Create postgresql database
resource "ibm_database" "postgresql_db" {
depends_on = [time_sleep.wait_for_authorization_policy]
resource_group_id = var.resource_group_id
name = var.name
service = "databases-for-postgresql"
location = var.region
plan = "standard" # Only standard plan is available for postgres
backup_id = var.backup_crn
remote_leader_id = var.remote_leader_crn
version = var.pg_version
tags = var.resource_tags
adminpassword = var.admin_pass
service_endpoints = var.service_endpoints
# remove elements with null values: see https://github.com/terraform-ibm-modules/terraform-ibm-icd-postgresql/issues/273
configuration = var.configuration != null ? jsonencode({ for k, v in var.configuration : k => v if v != null }) : null
key_protect_key = var.kms_key_crn
backup_encryption_key_crn = local.backup_encryption_key_crn
point_in_time_recovery_deployment_id = var.pitr_id
point_in_time_recovery_time = var.pitr_time
dynamic "users" {
for_each = nonsensitive(var.users != null ? var.users : [])
content {
name = users.value.name
password = users.value.password
type = users.value.type
role = (users.value.role != "" ? users.value.role : null)
}
}
# Workaround for https://github.ibm.com/GoldenEye/issues/issues/11359
# means that no `group` block is added when restoring from backup
# or point in time recovery
## This for_each block is NOT a loop to attach to multiple group blocks.
## This is used to conditionally add one, OR, the other group block depending on var.local.host_flavor_set
## This block is for if host_flavor IS set to specific pre-defined host sizes and not set to "multitenant"
dynamic "group" {
for_each = local.host_flavor_set && var.member_host_flavor != "multitenant" && !local.recovery_mode ? [1] : []
content {
group_id = "member" # Only member type is allowed for IBM Cloud Databases
host_flavor {
id = var.member_host_flavor
}
disk {
allocation_mb = var.member_disk_mb
}
dynamic "members" {
for_each = var.remote_leader_crn == null ? [1] : []
content {
allocation_count = var.members
}
}
}
}
## This block is for if host_flavor IS set to "multitenant"
dynamic "group" {
for_each = local.host_flavor_set && var.member_host_flavor == "multitenant" && !local.recovery_mode ? [1] : []
content {
group_id = "member" # Only member type is allowed for IBM Cloud Databases
host_flavor {
id = var.member_host_flavor
}
disk {
allocation_mb = var.member_disk_mb
}
memory {
allocation_mb = var.member_memory_mb
}
cpu {
allocation_count = var.member_cpu_count
}
dynamic "members" {
for_each = var.remote_leader_crn == null ? [1] : []
content {
allocation_count = var.members
}
}
}
}
## This block is for if host_flavor IS NOT set
dynamic "group" {
for_each = local.host_flavor_set == false && !local.recovery_mode ? [1] : []
content {
group_id = "member" # Only member type is allowed for IBM Cloud Databases
memory {
allocation_mb = var.member_memory_mb
}
disk {
allocation_mb = var.member_disk_mb
}
cpu {
allocation_count = var.member_cpu_count
}
dynamic "members" {
for_each = var.remote_leader_crn == null ? [1] : []
content {
allocation_count = var.members
}
}
}
}
## This for_each block is NOT a loop to attach to multiple auto_scaling blocks.
## This block is only used to conditionally add auto_scaling block depending on var.auto_scaling
dynamic "auto_scaling" {
for_each = local.auto_scaling_enabled
content {
disk {
capacity_enabled = var.auto_scaling.disk.capacity_enabled
free_space_less_than_percent = var.auto_scaling.disk.free_space_less_than_percent
io_above_percent = var.auto_scaling.disk.io_above_percent
io_enabled = var.auto_scaling.disk.io_enabled
io_over_period = var.auto_scaling.disk.io_over_period
rate_increase_percent = var.auto_scaling.disk.rate_increase_percent
rate_limit_mb_per_member = var.auto_scaling.disk.rate_limit_mb_per_member
rate_period_seconds = var.auto_scaling.disk.rate_period_seconds
rate_units = var.auto_scaling.disk.rate_units
}
memory {
io_above_percent = var.auto_scaling.memory.io_above_percent
io_enabled = var.auto_scaling.memory.io_enabled
io_over_period = var.auto_scaling.memory.io_over_period
rate_increase_percent = var.auto_scaling.memory.rate_increase_percent
rate_limit_mb_per_member = var.auto_scaling.memory.rate_limit_mb_per_member
rate_period_seconds = var.auto_scaling.memory.rate_period_seconds
rate_units = var.auto_scaling.memory.rate_units
}
}
}
lifecycle {
ignore_changes = [
# Ignore changes to these because a change will destroy and recreate the instance
version,
key_protect_key,
backup_encryption_key_crn,
]
}
timeouts {
create = "120m" # Extending provisioning time to 120 minutes
update = "120m"
delete = "15m"
}
}
resource "ibm_resource_tag" "postgresql_tag" {
count = length(var.access_tags) == 0 ? 0 : 1
resource_id = ibm_database.postgresql_db.resource_crn
tags = var.access_tags
tag_type = "access"
}
##############################################################################
# Context Based Restrictions
##############################################################################
module "cbr_rule" {
count = length(var.cbr_rules) > 0 ? length(var.cbr_rules) : 0
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module"
version = "1.29.0"
rule_description = var.cbr_rules[count.index].description
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
rule_contexts = var.cbr_rules[count.index].rule_contexts
resources = [{
attributes = [
{
name = "accountId"
value = var.cbr_rules[count.index].account_id
operator = "stringEquals"
},
{
name = "serviceInstance"
value = ibm_database.postgresql_db.guid
operator = "stringEquals"
},
{
name = "serviceName"
value = "databases-for-postgresql"
operator = "stringEquals"
}
]
}]
operations = [{
api_types = [
{
api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:data-plane"
}
]
}]
}
##############################################################################
# Service Credentials
##############################################################################
resource "ibm_resource_key" "service_credentials" {
for_each = var.service_credential_names
name = each.key
role = each.value
resource_instance_id = ibm_database.postgresql_db.id
}
locals {
# used for output only
service_credentials_json = length(var.service_credential_names) > 0 ? {
for service_credential in ibm_resource_key.service_credentials :
service_credential["name"] => service_credential["credentials_json"]
} : null
service_credentials_object = length(var.service_credential_names) > 0 ? {
hostname = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.postgres.hosts.0.hostname"]
certificate = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.postgres.certificate.certificate_base64"]
port = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.postgres.hosts.0.port"]
credentials = {
for service_credential in ibm_resource_key.service_credentials :
service_credential["name"] => {
username = service_credential.credentials["connection.postgres.authentication.username"]
password = service_credential.credentials["connection.postgres.authentication.password"]
}
}
} : null
}
data "ibm_database_connection" "database_connection" {
endpoint_type = var.service_endpoints == "public-and-private" ? "public" : var.service_endpoints
deployment_id = ibm_database.postgresql_db.id
user_id = ibm_database.postgresql_db.adminuser
user_type = "database"
}
data "ibm_database_point_in_time_recovery" "source_db_earliest_pitr_time" {
count = var.pitr_time != "" ? 0 : 1
deployment_id = var.pitr_id
}