Skip to content

Commit 1f22f28

Browse files
feat: Module updates:<br>- instance_name renamed to name<br>- endpoints renamed to service_endpoints<br><br>DA updates:<br>- Removed cbr_rule_ids, adminuser and certificate_base64 outputs.<br>- Added inputs existing_redis_instance_crn, admin_pass_secret_manager_secret_group, use_existing_admin_pass_secret_manager_secret_group and admin_pass_secret_manager_secret_name inputs.<br>- Added support for passing existing Redis instance (#520)
1 parent 4c2ca03 commit 1f22f28

26 files changed

+472
-226
lines changed

.secrets.baseline

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2025-01-27T10:43:12Z",
6+
"generated_at": "2025-02-11T19:19:45Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -82,7 +82,7 @@
8282
"hashed_secret": "ff9ee043d85595eb255c05dfe32ece02a53efbb2",
8383
"is_secret": false,
8484
"is_verified": false,
85-
"line_number": 33,
85+
"line_number": 34,
8686
"type": "Secret Keyword",
8787
"verified_result": null
8888
}

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This module implements an instance of IBM Cloud Databases for Redis.
2020
* [Basic example](./examples/basic)
2121
* [Complete example](./examples/complete)
2222
* [Financial Services compliant example](./examples/fscloud)
23+
* [Restore from backup example](./examples/backup-restore)
2324
* [Contributing](#contributing)
2425
<!-- END OVERVIEW HOOK -->
2526

@@ -94,16 +95,16 @@ You need the following permissions to run this module.
9495
| <a name="input_configuration"></a> [configuration](#input\_configuration) | Database Configuration. Default values will get picked up if not all the values are passed. | <pre>object({<br/> maxmemory = optional(number)<br/> maxmemory-policy = optional(string)<br/> appendonly = optional(string)<br/> maxmemory-samples = optional(number)<br/> stop-writes-on-bgsave-error = optional(string)<br/> })</pre> | `null` | no |
9596
| <a name="input_cpu_count"></a> [cpu\_count](#input\_cpu\_count) | Allocated dedicated CPU per member. For shared CPU, set to 0. [Learn more](https://cloud.ibm.com/docs/databases-for-redis?topic=databases-for-redis-resources-scaling) | `number` | `0` | no |
9697
| <a name="input_disk_mb"></a> [disk\_mb](#input\_disk\_mb) | Allocated disk per member. [Learn more](https://cloud.ibm.com/docs/databases-for-redis?topic=databases-for-redis-resources-scaling) | `number` | `1024` | no |
97-
| <a name="input_endpoints"></a> [endpoints](#input\_endpoints) | Specify whether you want to enable the public, private, or both service endpoints. Supported values are 'public', 'private', or 'public-and-private'. | `string` | `"private"` | no |
98-
| <a name="input_instance_name"></a> [instance\_name](#input\_instance\_name) | The name to give the Redis instance. | `string` | n/a | yes |
9998
| <a name="input_kms_key_crn"></a> [kms\_key\_crn](#input\_kms\_key\_crn) | The CRN of a Key Protect or Hyper Protect Crypto Services encryption key to encrypt your data. Applies only if `use_ibm_owned_encryption_key` is false. By default this key is used for both deployment data and backups, but this behaviour can be altered using the `use_same_kms_key_for_backups` and `backup_encryption_key_crn` inputs. Bare in mind that backups encryption is only available in certain regions. See [Bring your own key for backups](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect&interface=ui#key-byok) and [Using the HPCS Key for Backup encryption](https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-hpcs#use-hpcs-backups). | `string` | `null` | no |
10099
| <a name="input_member_host_flavor"></a> [member\_host\_flavor](#input\_member\_host\_flavor) | Allocated host flavor per member. [Learn more](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/database#host_flavor). | `string` | `null` | no |
101100
| <a name="input_members"></a> [members](#input\_members) | Allocated number of members. Members can be scaled up but not down. | `number` | `2` | no |
102101
| <a name="input_memory_mb"></a> [memory\_mb](#input\_memory\_mb) | Allocated memory per member. [Learn more](https://cloud.ibm.com/docs/databases-for-redis?topic=databases-for-redis-resources-scaling) | `number` | `4096` | no |
102+
| <a name="input_name"></a> [name](#input\_name) | The name to give the Redis instance. | `string` | n/a | yes |
103103
| <a name="input_redis_version"></a> [redis\_version](#input\_redis\_version) | Version of the Redis instance to provision. If no value is passed, the current preferred version of IBM Cloud Databases is used. | `string` | `null` | no |
104104
| <a name="input_region"></a> [region](#input\_region) | The region where you want to deploy your instance. | `string` | `"us-south"` | no |
105105
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the Redis instance will be created. | `string` | n/a | yes |
106106
| <a name="input_service_credential_names"></a> [service\_credential\_names](#input\_service\_credential\_names) | Map of name, role for service credentials that you want to create for the database | `map(string)` | `{}` | no |
107+
| <a name="input_service_endpoints"></a> [service\_endpoints](#input\_service\_endpoints) | Specify whether you want to enable the public, private, or both service endpoints. Supported values are 'public', 'private', or 'public-and-private'. | `string` | `"private"` | no |
107108
| <a name="input_skip_iam_authorization_policy"></a> [skip\_iam\_authorization\_policy](#input\_skip\_iam\_authorization\_policy) | Set to true to skip the creation of IAM authorization policies that permits all Databases for Redis instances in the given resource group 'Reader' access to the Key Protect or Hyper Protect Crypto Services key that was provided in the `kms_key_crn` and `backup_encryption_key_crn` inputs. This policy is required in order to enable KMS encryption, so only skip creation if there is one already present in your account. No policy is created if `use_ibm_owned_encryption_key` is true. | `bool` | `false` | no |
108109
| <a name="input_tags"></a> [tags](#input\_tags) | Optional list of tags to be added to the Redis instance. | `list(string)` | `[]` | no |
109110
| <a name="input_use_default_backup_encryption_key"></a> [use\_default\_backup\_encryption\_key](#input\_use\_default\_backup\_encryption\_key) | When `use_ibm_owned_encryption_key` is set to false, backups will be encrypted with either the key specified in `kms_key_crn`, or in `backup_encryption_key_crn` if a value is passed. If you do not want to use your own key for backups encryption, you can set this to `true` to use the IBM Cloud Databases default encryption for backups. Alternatively set `use_ibm_owned_encryption_key` to true to use the default encryption for both backups and deployment data. | `bool` | `false` | no |

examples/backup-restore/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Restore from backup example
2+
3+
This example provides an end-to-end executable flow of how a Redis DB instance can be created from a backup instance. This example uses the IBM Cloud terraform provider to:
4+
5+
- Create a new resource group if one is not passed in.
6+
- Create a restored ICD Redis database instance pointing to the lastest backup of the existing Redis database instance crn passed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ibmcloud_api_key": $VALIDATION_APIKEY,
3+
"region": "us-south",
4+
"resource_tags": $TAGS,
5+
"prefix": $PREFIX
6+
}

examples/backup-restore/main.tf

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.1.6"
8+
# if an existing resource group is not set (null) create a new one using prefix
9+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
10+
existing_resource_group_name = var.resource_group
11+
}
12+
13+
data "ibm_database_backups" "backup_database" {
14+
deployment_id = var.existing_database_crn
15+
}
16+
17+
# New redis instance pointing to the backup instance
18+
module "restored_icd_redis" {
19+
source = "../../"
20+
resource_group_id = module.resource_group.resource_group_id
21+
name = "${var.prefix}-redis-restored"
22+
redis_version = var.redis_version
23+
region = var.region
24+
tags = var.resource_tags
25+
access_tags = var.access_tags
26+
member_host_flavor = "multitenant"
27+
backup_crn = data.ibm_database_backups.backup_database.backups[0].backup_id
28+
}

examples/backup-restore/outputs.tf

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
output "restored_icd_redis_id" {
5+
description = "Restored redis instance id"
6+
value = module.restored_icd_redis.id
7+
}
8+
9+
output "restored_icd_redis_version" {
10+
description = "Restored redis instance version"
11+
value = module.restored_icd_redis.version
12+
}

examples/backup-restore/provider.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}

examples/backup-restore/variables.tf

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
6+
7+
variable "region" {
8+
type = string
9+
description = "Region to provision all resources created by this example."
10+
default = "us-south"
11+
}
12+
13+
variable "prefix" {
14+
type = string
15+
description = "Prefix to append to all resources created by this example"
16+
default = "backup"
17+
}
18+
19+
variable "redis_version" {
20+
type = string
21+
description = "Version of the redis instance. If no value passed, the current ICD preferred version is used."
22+
default = null
23+
}
24+
25+
variable "resource_group" {
26+
type = string
27+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
28+
default = null
29+
}
30+
31+
variable "resource_tags" {
32+
type = list(string)
33+
description = "Optional list of tags to be added to created resources"
34+
default = []
35+
}
36+
37+
variable "access_tags" {
38+
type = list(string)
39+
description = "A list of access tags to apply to the redis instance created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details"
40+
default = []
41+
}
42+
43+
variable "existing_database_crn" {
44+
type = string
45+
description = "The existing CRN of a backup resource to restore from."
46+
default = null
47+
}

examples/backup-restore/version.tf

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
required_providers {
4+
# Ensure that there is always 1 example locked into the lowest provider version of the range defined in the main
5+
# module's version.tf (basic example), and 1 example that will always use the latest provider version (complete example).
6+
ibm = {
7+
source = "IBM-Cloud/ibm"
8+
version = ">=1.70.0, <2.0.0"
9+
}
10+
}
11+
}

examples/basic/main.tf

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ module "resource_group" {
1414
# Redis
1515
##############################################################################
1616

17-
module "redis" {
18-
source = "../.."
19-
resource_group_id = module.resource_group.resource_group_id
20-
instance_name = "${var.prefix}-redis"
21-
region = var.region
22-
access_tags = var.access_tags
23-
tags = var.resource_tags
24-
redis_version = var.redis_version
17+
module "database" {
18+
source = "../.."
19+
resource_group_id = module.resource_group.resource_group_id
20+
name = "${var.prefix}-data-store"
21+
region = var.region
22+
access_tags = var.access_tags
23+
service_endpoints = var.service_endpoints
24+
member_host_flavor = var.member_host_flavor
25+
tags = var.resource_tags
26+
redis_version = var.redis_version
27+
service_credential_names = {
28+
"redis_admin" : "Administrator",
29+
"redis_operator" : "Operator",
30+
"redis_viewer" : "Viewer",
31+
"redis_editor" : "Editor",
32+
}
2533
}

examples/basic/outputs.tf

+11-6
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,36 @@
33
##############################################################################
44
output "id" {
55
description = "Redis instance id"
6-
value = module.redis.id
6+
value = module.database.id
7+
}
8+
9+
output "redis_crn" {
10+
description = "Redis CRN"
11+
value = module.database.crn
712
}
813

914
output "version" {
1015
description = "Redis instance version"
11-
value = module.redis.version
16+
value = module.database.version
1217
}
1318

1419
output "adminuser" {
1520
description = "Database admin user name"
16-
value = module.redis.adminuser
21+
value = module.database.adminuser
1722
}
1823

1924
output "hostname" {
2025
description = "Database connection hostname"
21-
value = module.redis.hostname
26+
value = module.database.hostname
2227
}
2328

2429
output "port" {
2530
description = "Database connection port"
26-
value = module.redis.port
31+
value = module.database.port
2732
}
2833

2934
output "certificate_base64" {
3035
description = "Database connection certificate"
31-
value = module.redis.certificate_base64
36+
value = module.database.certificate_base64
3237
sensitive = true
3338
}

examples/basic/variables.tf

+17
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,20 @@ variable "redis_version" {
3939
type = string
4040
default = null
4141
}
42+
43+
variable "service_endpoints" {
44+
type = string
45+
description = "The type of endpoint of the database instance. Possible values: `public`, `private`, `public-and-private`."
46+
default = "public"
47+
48+
validation {
49+
condition = can(regex("public|public-and-private|private", var.service_endpoints))
50+
error_message = "Valid values for service_endpoints are 'public', 'public-and-private', and 'private'"
51+
}
52+
}
53+
variable "member_host_flavor" {
54+
type = string
55+
description = "The host flavor per member. [Learn more](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/database#host_flavor)."
56+
default = "multitenant"
57+
# Validation is done in the Terraform plan phase by the IBM provider, so no need to add extra validation here.
58+
}

examples/complete/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module "icd_redis" {
9292
source = "../../"
9393
resource_group_id = module.resource_group.resource_group_id
9494
redis_version = var.redis_version
95-
instance_name = "${var.prefix}-redis"
95+
name = "${var.prefix}-redis"
9696
region = var.region
9797
admin_pass = var.admin_pass
9898
users = var.users

examples/fscloud/main.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module "cbr_zone" {
5656
module "redis" {
5757
source = "../../modules/fscloud"
5858
resource_group_id = module.resource_group.resource_group_id
59-
instance_name = "${var.prefix}-redis"
59+
name = "${var.prefix}-redis"
6060
region = var.region
6161
redis_version = var.redis_version
6262
access_tags = var.access_tags

ibm_catalog.json

+15-3
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@
231231
{
232232
"key": "admin_pass"
233233
},
234+
{
235+
"key": "admin_pass_secret_manager_secret_group"
236+
},
237+
{
238+
"key": "admin_pass_secret_manager_secret_name"
239+
},
240+
{
241+
"key": "use_existing_admin_pass_secret_manager_secret_group"
242+
},
243+
{
244+
"key": "existing_redis_instance_crn"
245+
},
246+
{
247+
"key": "skip_redis_kms_auth_policy"
248+
},
234249
{
235250
"key": "users"
236251
},
@@ -280,9 +295,6 @@
280295
}
281296
]
282297
},
283-
{
284-
"key": "skip_redis_kms_auth_policy"
285-
},
286298
{
287299
"key": "key_ring_name"
288300
},

main.tf

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
##############################################################################
2-
# ICD Redis module
3-
##############################################################################
1+
########################################################################################################################
2+
# Input variable validation
3+
# (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
4+
#
5+
# TODO: Replace with terraform cross variable validation: https://github.ibm.com/GoldenEye/issues/issues/10836
6+
########################################################################################################################
47

58
locals {
69
# Validation (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
@@ -12,7 +15,13 @@ locals {
1215
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
1316
# tflint-ignore: terraform_unused_declarations
1417
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
18+
}
1519

20+
########################################################################################################################
21+
# Locals
22+
########################################################################################################################
23+
24+
locals {
1625
# 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.
1726

1827
# If 'use_ibm_owned_encryption_key' is true or 'use_default_backup_encryption_key' is true, default to null.
@@ -173,13 +182,13 @@ resource "time_sleep" "wait_for_backup_kms_authorization_policy" {
173182

174183
resource "ibm_database" "redis_database" {
175184
depends_on = [time_sleep.wait_for_authorization_policy]
176-
name = var.instance_name
185+
name = var.name
177186
plan = "standard" # Only standard plan is available for redis
178187
location = var.region
179188
service = "databases-for-redis"
180189
version = var.redis_version
181190
resource_group_id = var.resource_group_id
182-
service_endpoints = var.endpoints
191+
service_endpoints = var.service_endpoints
183192
tags = var.tags
184193
adminpassword = var.admin_pass
185194
key_protect_key = var.kms_key_crn
@@ -391,7 +400,7 @@ locals {
391400
}
392401

393402
data "ibm_database_connection" "database_connection" {
394-
endpoint_type = var.endpoints == "public-and-private" ? "public" : var.endpoints
403+
endpoint_type = var.service_endpoints == "public-and-private" ? "public" : var.service_endpoints
395404
deployment_id = ibm_database.redis_database.id
396405
user_id = ibm_database.redis_database.adminuser
397406
user_type = "database"

0 commit comments

Comments
 (0)