generated from terraform-ibm-modules/terraform-ibm-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
281 lines (253 loc) · 12.4 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
##############################################################################
# ICD Redis module
##############################################################################
locals {
# Validation (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
# tflint-ignore: terraform_unused_declarations
validate_kms_values = !var.kms_encryption_enabled && (var.kms_key_crn != null || var.backup_encryption_key_crn != null) ? tobool("When passing values for var.backup_encryption_key_crn or var.kms_key_crn, you must set var.kms_encryption_enabled to true. Otherwise unset them to use default encryption") : true
# tflint-ignore: terraform_unused_declarations
validate_kms_vars = var.kms_encryption_enabled && var.kms_key_crn == null && var.backup_encryption_key_crn == null ? tobool("When setting var.kms_encryption_enabled to true, a value must be passed for var.kms_key_crn and/or var.backup_encryption_key_crn") : true
# tflint-ignore: terraform_unused_declarations
validate_auth_policy = var.kms_encryption_enabled && var.skip_iam_authorization_policy == false && var.existing_kms_instance_guid == null ? tobool("When var.skip_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
# tflint-ignore: terraform_unused_declarations
validate_backup_key = var.backup_encryption_key_crn != null && var.use_default_backup_encryption_key == true ? tobool("When passing a value for 'backup_encryption_key_crn' you cannot set 'use_default_backup_encryption_key' to 'true'") : true
# If no value passed for 'backup_encryption_key_crn' use the value of 'kms_key_crn' and perform validation of 'kms_key_crn' to check if region is supported by backup encryption key.
# For more info, see https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect&interface=ui#key-byok and https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-hpcs#use-hpcs-backups"
backup_encryption_key_crn = var.use_default_backup_encryption_key == true ? 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 what KMS service is being used for database encryption
kms_service = var.kms_key_crn != null ? (
can(regex(".*kms.*", var.kms_key_crn)) ? "kms" : (
can(regex(".*hs-crypto.*", var.kms_key_crn)) ? "hs-crypto" : null
)
) : null
# maxmemory configuration should 80% of the deployment's memory.
calculate_config_maxmemory = tonumber(format("%.0f", var.memory_mb * 0.8))
}
# Create IAM Authorization Policies to allow Redis to access KMS for the encryption key
resource "ibm_iam_authorization_policy" "kms_policy" {
count = var.kms_encryption_enabled == false || var.skip_iam_authorization_policy ? 0 : 1
source_service_name = "databases-for-redis"
source_resource_group_id = var.resource_group_id
target_service_name = local.kms_service
target_resource_instance_id = var.existing_kms_instance_guid
roles = ["Reader"]
description = "Allow all redis instances in the resource group ${var.resource_group_id} to read from the ${local.kms_service} instance GUID ${var.existing_kms_instance_guid}"
}
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
resource "time_sleep" "wait_for_authorization_policy" {
depends_on = [ibm_iam_authorization_policy.kms_policy]
create_duration = "30s"
}
resource "ibm_database" "redis_database" {
depends_on = [time_sleep.wait_for_authorization_policy]
name = var.instance_name
plan = "standard" # Only standard plan is available for redis
location = var.region
service = "databases-for-redis"
version = var.redis_version
resource_group_id = var.resource_group_id
service_endpoints = var.endpoints
tags = var.tags
adminpassword = var.admin_pass
key_protect_key = var.kms_key_crn
backup_encryption_key_crn = local.backup_encryption_key_crn
backup_id = var.backup_crn
# For default configuration, see here: https://cloud.ibm.com/docs/databases-for-redis?topic=databases-for-redis-changing-configuration&interface=cli
configuration = var.configuration == null ? null : jsonencode({
maxmemory = var.configuration.maxmemory != null ? var.configuration.maxmemory : local.calculate_config_maxmemory
maxmemory-policy = var.configuration.maxmemory-policy != null ? var.configuration.maxmemory-policy : "noeviction"
appendonly = var.configuration.appendonly != null ? var.configuration.appendonly : "yes"
maxmemory-samples = var.configuration.maxmemory-samples != null ? var.configuration.maxmemory-samples : 5
stop-writes-on-bgsave-error = var.configuration.stop-writes-on-bgsave-error != null ? var.configuration.stop-writes-on-bgsave-error : "yes"
})
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)
}
}
## 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" && var.backup_crn == null ? [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.disk_mb
}
members {
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" && var.backup_crn == null ? [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.disk_mb
}
memory {
allocation_mb = var.memory_mb
}
cpu {
allocation_count = var.cpu_count
}
members {
allocation_count = var.members
}
}
}
## This block is for if host_flavor IS NOT set
dynamic "group" {
for_each = !local.host_flavor_set && var.backup_crn == null ? [1] : []
content {
group_id = "member" # Only member type is allowed for IBM Cloud Databases
memory {
allocation_mb = var.memory_mb
}
disk {
allocation_mb = var.disk_mb
}
cpu {
allocation_count = var.cpu_count
}
members {
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" "access_tag" {
count = length(var.access_tags) == 0 ? 0 : 1
resource_id = ibm_database.redis_database.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.redis_database.id
operator = "stringEquals"
},
{
name = "serviceName"
value = "databases-for-redis"
operator = "stringEquals"
}
]
}]
# There is only 1 operation type for Redis so it is not exposed as a configuration
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.redis_database.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.rediss.hosts.0.hostname"]
certificate = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.rediss.certificate.certificate_base64"]
port = ibm_resource_key.service_credentials[keys(var.service_credential_names)[0]].credentials["connection.rediss.hosts.0.port"]
credentials = {
for service_credential in ibm_resource_key.service_credentials :
service_credential["name"] => {
username = service_credential.credentials["connection.rediss.authentication.username"]
password = service_credential.credentials["connection.rediss.authentication.password"]
}
}
} : null
}
data "ibm_database_connection" "database_connection" {
endpoint_type = var.endpoints == "public-and-private" ? "public" : var.endpoints
deployment_id = ibm_database.redis_database.id
user_id = ibm_database.redis_database.adminuser
user_type = "database"
}