Skip to content

Commit 7218e7a

Browse files
authored
Replace make_primary_cluster with better named cluster_type (cloudposse#109)
1 parent 5851f2d commit 7218e7a

File tree

6 files changed

+34
-22
lines changed

6 files changed

+34
-22
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,13 +378,13 @@ Available targets:
378378
| [aws_db_parameter_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/db_parameter_group) |
379379
| [aws_db_subnet_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/db_subnet_group) |
380380
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/data-sources/iam_policy_document) |
381-
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role) |
382381
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role_policy_attachment) |
383-
| [aws_rds_cluster](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster) |
382+
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role) |
384383
| [aws_rds_cluster_instance](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster_instance) |
385384
| [aws_rds_cluster_parameter_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster_parameter_group) |
386-
| [aws_security_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group) |
385+
| [aws_rds_cluster](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster) |
387386
| [aws_security_group_rule](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group_rule) |
387+
| [aws_security_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group) |
388388

389389
## Inputs
390390

@@ -412,6 +412,7 @@ Available targets:
412412
| cluster\_identifier | The RDS Cluster Identifier. Will use generated label ID if not supplied | `string` | `""` | no |
413413
| cluster\_parameters | List of DB cluster parameters to apply | <pre>list(object({<br> apply_method = string<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
414414
| cluster\_size | Number of DB instances to create in the cluster | `number` | `2` | no |
415+
| cluster\_type | Either `regional` or `global`.<br>If `regional` will be created as a normal, standalone DB.<br>If `global`, will be made part of a Global cluster (requires `global_cluster_identifier`). | `string` | `"regional"` | no |
415416
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
416417
| copy\_tags\_to\_snapshot | Copy tags to backup snapshots | `bool` | `false` | no |
417418
| db\_name | Database name (default is not to create a database) | `string` | `""` | no |
@@ -438,7 +439,6 @@ Available targets:
438439
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
439440
| label\_value\_case | The letter case of output label values (also used in `tags` and `id`).<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Default value: `lower`. | `string` | `null` | no |
440441
| maintenance\_window | Weekly time range during which system maintenance can occur, in UTC | `string` | `"wed:03:00-wed:04:00"` | no |
441-
| make\_primary\_cluster | Set true or false to force the cluster to be created as primary or secondary. Leave null to set automatically based on global\_cluster\_identifier. | `bool` | `null` | no |
442442
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
443443
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
444444
| performance\_insights\_enabled | Whether to enable Performance Insights | `bool` | `false` | no |

docs/terraform.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
| [aws_db_parameter_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/db_parameter_group) |
3232
| [aws_db_subnet_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/db_subnet_group) |
3333
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/data-sources/iam_policy_document) |
34-
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role) |
3534
| [aws_iam_role_policy_attachment](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role_policy_attachment) |
36-
| [aws_rds_cluster](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster) |
35+
| [aws_iam_role](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/iam_role) |
3736
| [aws_rds_cluster_instance](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster_instance) |
3837
| [aws_rds_cluster_parameter_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster_parameter_group) |
39-
| [aws_security_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group) |
38+
| [aws_rds_cluster](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/rds_cluster) |
4039
| [aws_security_group_rule](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group_rule) |
40+
| [aws_security_group](https://registry.terraform.io/providers/hashicorp/aws/3.1.15/docs/resources/security_group) |
4141

4242
## Inputs
4343

@@ -65,6 +65,7 @@
6565
| cluster\_identifier | The RDS Cluster Identifier. Will use generated label ID if not supplied | `string` | `""` | no |
6666
| cluster\_parameters | List of DB cluster parameters to apply | <pre>list(object({<br> apply_method = string<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
6767
| cluster\_size | Number of DB instances to create in the cluster | `number` | `2` | no |
68+
| cluster\_type | Either `regional` or `global`.<br>If `regional` will be created as a normal, standalone DB.<br>If `global`, will be made part of a Global cluster (requires `global_cluster_identifier`). | `string` | `"regional"` | no |
6869
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
6970
| copy\_tags\_to\_snapshot | Copy tags to backup snapshots | `bool` | `false` | no |
7071
| db\_name | Database name (default is not to create a database) | `string` | `""` | no |
@@ -91,7 +92,6 @@
9192
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
9293
| label\_value\_case | The letter case of output label values (also used in `tags` and `id`).<br>Possible values: `lower`, `title`, `upper` and `none` (no transformation).<br>Default value: `lower`. | `string` | `null` | no |
9394
| maintenance\_window | Weekly time range during which system maintenance can occur, in UTC | `string` | `"wed:03:00-wed:04:00"` | no |
94-
| make\_primary\_cluster | Set true or false to force the cluster to be created as primary or secondary. Leave null to set automatically based on global\_cluster\_identifier. | `bool` | `null` | no |
9595
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
9696
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
9797
| performance\_insights\_enabled | Whether to enable Performance Insights | `bool` | `false` | no |

enhanced-monitoring.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module "enhanced_monitoring_label" {
66
version = "0.24.1"
77

88
enabled = module.this.enabled && var.enhanced_monitoring_role_enabled
9-
attributes = concat(module.this.attributes, ["enhanced-monitoring"])
9+
attributes = ["enhanced-monitoring"]
1010

1111
context = module.this.context
1212
}
@@ -16,6 +16,7 @@ resource "aws_iam_role" "enhanced_monitoring" {
1616
count = module.this.enabled && var.enhanced_monitoring_role_enabled ? 1 : 0
1717
name = module.enhanced_monitoring_label.id
1818
assume_role_policy = join("", data.aws_iam_policy_document.enhanced_monitoring.*.json)
19+
tags = module.enhanced_monitoring_label.tags
1920
}
2021

2122
# Attach Amazon's managed policy for RDS enhanced monitoring

main.tf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
22
cluster_instance_count = module.this.enabled ? var.cluster_size : 0
3-
is_primary_cluster = var.make_primary_cluster != null ? var.make_primary_cluster : var.global_cluster_identifier == null || var.global_cluster_identifier == "" ? true : false
3+
is_regional_cluster = var.cluster_type == "regional"
44
}
55

66
resource "aws_security_group" "default" {
@@ -44,8 +44,10 @@ resource "aws_security_group_rule" "egress" {
4444
security_group_id = join("", aws_security_group.default.*.id)
4545
}
4646

47+
# The name "primary" is poorly chosen. We actually mean standalone or regional.
48+
# The primary cluster of a global database is actually created with the "secondary" cluster resource below.
4749
resource "aws_rds_cluster" "primary" {
48-
count = module.this.enabled && local.is_primary_cluster ? 1 : 0
50+
count = module.this.enabled && local.is_regional_cluster ? 1 : 0
4951
cluster_identifier = var.cluster_identifier == "" ? module.this.id : var.cluster_identifier
5052
database_name = var.db_name
5153
master_username = var.admin_user
@@ -120,7 +122,7 @@ resource "aws_rds_cluster" "primary" {
120122

121123
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster#replication_source_identifier
122124
resource "aws_rds_cluster" "secondary" {
123-
count = module.this.enabled && ! local.is_primary_cluster ? 1 : 0
125+
count = module.this.enabled && ! local.is_regional_cluster ? 1 : 0
124126
cluster_identifier = var.cluster_identifier == "" ? module.this.id : var.cluster_identifier
125127
database_name = var.db_name
126128
master_username = var.admin_user

outputs.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@ output "database_name" {
44
}
55

66
output "master_username" {
7-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.master_username) : join("", aws_rds_cluster.secondary.*.master_username)
7+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.master_username) : join("", aws_rds_cluster.secondary.*.master_username)
88
description = "Username for the master DB user"
99
}
1010

1111
output "cluster_identifier" {
12-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.cluster_identifier) : join("", aws_rds_cluster.secondary.*.cluster_identifier)
12+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.cluster_identifier) : join("", aws_rds_cluster.secondary.*.cluster_identifier)
1313
description = "Cluster Identifier"
1414
}
1515

1616
output "arn" {
17-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.arn) : join("", aws_rds_cluster.secondary.*.arn)
17+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.arn) : join("", aws_rds_cluster.secondary.*.arn)
1818
description = "Amazon Resource Name (ARN) of the cluster"
1919
}
2020

2121
output "endpoint" {
22-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.endpoint) : join("", aws_rds_cluster.secondary.*.endpoint)
22+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.endpoint) : join("", aws_rds_cluster.secondary.*.endpoint)
2323
description = "The DNS address of the RDS instance"
2424
}
2525

2626
output "reader_endpoint" {
27-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.reader_endpoint) : join("", aws_rds_cluster.secondary.*.reader_endpoint)
27+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.reader_endpoint) : join("", aws_rds_cluster.secondary.*.reader_endpoint)
2828
description = "A read-only endpoint for the Aurora cluster, automatically load-balanced across replicas"
2929
}
3030

@@ -44,7 +44,7 @@ output "dbi_resource_ids" {
4444
}
4545

4646
output "cluster_resource_id" {
47-
value = local.is_primary_cluster ? join("", aws_rds_cluster.primary.*.cluster_resource_id) : join("", aws_rds_cluster.secondary.*.cluster_resource_id)
47+
value = local.is_regional_cluster ? join("", aws_rds_cluster.primary.*.cluster_resource_id) : join("", aws_rds_cluster.secondary.*.cluster_resource_id)
4848
description = "The region-unique, immutable identifie of the cluster"
4949
}
5050

variables.tf

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,19 @@ variable "reader_dns_name" {
342342
default = ""
343343
}
344344

345-
variable "make_primary_cluster" {
346-
type = bool
347-
description = "Set true or false to force the cluster to be created as primary or secondary. Leave null to set automatically based on global_cluster_identifier."
348-
default = null
345+
variable "cluster_type" {
346+
type = string
347+
description = <<-EOT
348+
Either `regional` or `global`.
349+
If `regional` will be created as a normal, standalone DB.
350+
If `global`, will be made part of a Global cluster (requires `global_cluster_identifier`).
351+
EOT
352+
default = "regional"
353+
354+
validation {
355+
condition = contains(["regional", "global"], var.cluster_type)
356+
error_message = "Allowed values: `regional` (standalone), `global` (part of global cluster)."
357+
}
349358
}
350359

351360
variable "global_cluster_identifier" {

0 commit comments

Comments
 (0)