Skip to content

Commit 301599e

Browse files
authored
feat: added remote leader support (#66)
1 parent c14c43a commit 301599e

File tree

12 files changed

+231
-28
lines changed

12 files changed

+231
-28
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ You need the following permissions to run this module.
3333
- **Databases for PostgreSQL** service
3434
- `Editor` role access
3535
<!-- END MODULE HOOK -->
36+
37+
## Read-only Replica considerations
38+
39+
- **There are additional considerations when setting the variables in the read-only-replica example.** Refer [configuring read-only replicas](https://cloud.ibm.com/docs/databases-for-postgresql?topic=databases-for-postgresql-read-only-replicas)
40+
41+
| Variable | Description |
42+
|------|---------|
43+
| <a name="input_region"></a> [region](#input\_region)| The read-only replica can exist in the same region as the source formation or in different one, enabling your data to be replicated across regions. |
44+
| <a name="input_member_memory_mb"></a> [member\_memory\_mb](#input\_member\_memory\_mb) | The minimum RAM size of a read-only replica is 3 GB |
45+
| <a name="input_member_disk_mb"></a> [member\_disk\_mb](#input\_member\_disk\_mb) | The minimum disk size of a read-only replica is 15 GB |
46+
| <a name="input_member_cpu_count"></a> [member\_cpu\_count](#input\_member\_cpu\_count) | The minimum CPU allocation required for read-only replica is 9 |
47+
| <a name="input_members"></a> [members](#input\_members)| A read-only replica is a deployment with single data member and does not have any internal high availability.|
48+
3649
<!-- BEGIN EXAMPLES HOOK -->
3750
## Examples
3851

@@ -41,6 +54,7 @@ You need the following permissions to run this module.
4154
- [ Complete example with byok encryption, CBR rules and storing credentials in secrets manager](examples/complete)
4255
- [ Default example](examples/default)
4356
- [ Financial Services Cloud profile example](examples/fscloud)
57+
- [ Replica example](examples/replica)
4458
<!-- END EXAMPLES HOOK -->
4559
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4660
## Requirements
@@ -82,6 +96,7 @@ You need the following permissions to run this module.
8296
| <a name="input_pg_version"></a> [pg\_version](#input\_pg\_version) | Version of the postgresql instance | `string` | `null` | no |
8397
| <a name="input_plan_validation"></a> [plan\_validation](#input\_plan\_validation) | Enable or disable validating the database parameters for postgres during the plan phase | `bool` | `true` | no |
8498
| <a name="input_region"></a> [region](#input\_region) | The region postgresql is to be created on. The region must support BYOK if key\_protect\_key\_crn is used | `string` | `"us-south"` | no |
99+
| <a name="input_remote_leader_crn"></a> [remote\_leader\_crn](#input\_remote\_leader\_crn) | The CRN of the leader database to make the replica(read-only) deployment. | `string` | `null` | no |
85100
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where the postgresql will be created | `string` | n/a | yes |
86101
| <a name="input_resource_tags"></a> [resource\_tags](#input\_resource\_tags) | Optional list of tags to be added to created resources | `list(string)` | `[]` | no |
87102
| <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 |

examples/replica/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Replica example
2+
3+
An end-to-end example that uses the module's default variable values. 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 new ICD Postgresql database instance.
7+
- Create a read-only replica of the leader Postgresql database instance.

examples/replica/main.tf

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
7+
# if an existing resource group is not set (null) create a new one using prefix
8+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
9+
existing_resource_group_name = var.resource_group
10+
}
11+
12+
##############################################################################
13+
# ICD postgresql primary/leader database
14+
##############################################################################
15+
16+
module "postgresql_db" {
17+
source = "../.."
18+
resource_group_id = module.resource_group.resource_group_id
19+
name = "${var.prefix}-primary"
20+
region = var.region
21+
resource_tags = var.resource_tags
22+
}
23+
24+
##############################################################################
25+
# ICD postgresql read-only-replica
26+
##############################################################################
27+
28+
module "read_only_replica_postgresql_db" {
29+
source = "../.."
30+
resource_group_id = module.resource_group.resource_group_id
31+
name = "${var.prefix}-read-only-replica"
32+
region = var.region
33+
resource_tags = var.resource_tags
34+
remote_leader_crn = module.postgresql_db.crn
35+
member_memory_mb = var.member_memory_mb
36+
member_disk_mb = var.member_disk_mb
37+
member_cpu_count = var.member_cpu_count
38+
}

examples/replica/outputs.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "id" {
6+
description = "Postgresql instance id"
7+
value = module.postgresql_db.id
8+
}
9+
10+
output "replica_id" {
11+
description = "Postgresql read-only replica instance id"
12+
value = module.read_only_replica_postgresql_db.id
13+
}
14+
15+
output "version" {
16+
description = "Postgresql instance version"
17+
value = module.postgresql_db.version
18+
}
19+
20+
output "replica_version" {
21+
description = "Postgresql read-only replica instance version"
22+
value = module.read_only_replica_postgresql_db.version
23+
}

examples/replica/provider.tf

Lines changed: 4 additions & 0 deletions
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/replica/variables.tf

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 = "postgres"
17+
}
18+
19+
variable "resource_group" {
20+
type = string
21+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
22+
default = null
23+
}
24+
25+
variable "resource_tags" {
26+
type = list(string)
27+
description = "Optional list of tags to be added to created resources"
28+
default = []
29+
}
30+
31+
variable "member_memory_mb" {
32+
type = string
33+
description = "Memory allocation required for postgresql read-only replica database"
34+
default = "3072"
35+
validation {
36+
condition = alltrue([
37+
var.member_memory_mb >= 3072,
38+
var.member_memory_mb <= 114688
39+
])
40+
error_message = "member group memory must be >= 3072 and <= 114688 in increments of 384"
41+
}
42+
}
43+
44+
variable "member_disk_mb" {
45+
type = string
46+
description = "Disk allocation required for postgresql read-only replica database"
47+
default = "15360"
48+
validation {
49+
condition = alltrue([
50+
var.member_disk_mb >= 15360,
51+
var.member_disk_mb <= 4194304
52+
])
53+
error_message = "member group disk must be >= 15360 and <= 4194304 in increments of 1536"
54+
}
55+
}
56+
57+
variable "member_cpu_count" {
58+
type = string
59+
description = "CPU allocation required for the postgresql read-only replica database"
60+
default = "9"
61+
validation {
62+
condition = alltrue([
63+
var.member_cpu_count >= 9,
64+
var.member_cpu_count <= 28
65+
])
66+
error_message = "member group cpu must be >= 9 and <= 28 in increments of 1"
67+
}
68+
}

examples/replica/version.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
required_providers {
4+
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
5+
ibm = {
6+
source = "IBM-Cloud/ibm"
7+
version = "1.49.0"
8+
}
9+
}
10+
}

main.tf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ resource "ibm_database" "postgresql_db" {
1818
plan = "standard" # Only standard plan is available for postgres
1919
backup_id = var.backup_crn
2020
plan_validation = var.plan_validation
21+
remote_leader_id = var.remote_leader_crn
2122
version = var.pg_version
2223
tags = var.resource_tags
2324
service_endpoints = var.service_endpoints
@@ -44,9 +45,11 @@ resource "ibm_database" "postgresql_db" {
4445
cpu {
4546
allocation_count = var.member_cpu_count
4647
}
47-
48-
members {
49-
allocation_count = var.members
48+
dynamic "members" {
49+
for_each = var.remote_leader_crn == null ? [1] : []
50+
content {
51+
allocation_count = var.members
52+
}
5053
}
5154
}
5255

0 commit comments

Comments
 (0)