From 54be61f4b60484e13253ea7d971b91ac1f1f3476 Mon Sep 17 00:00:00 2001 From: finchr Date: Thu, 6 Jun 2024 19:34:35 -0700 Subject: [PATCH] reopen #192 (#213) * implement create_before_destroy on instances * add random provider to versions.tf * run terraform fmt * run `make readme` to pass ci action * fix random_pet in case of enabled == true * don't create resource in enabled == false --------- Co-authored-by: Benjamin Smith --- README.md | 3 +++ docs/terraform.md | 3 +++ main.tf | 16 +++++++++++++--- versions.tf | 4 ++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2d43f390..680d95ae 100644 --- a/README.md +++ b/README.md @@ -320,12 +320,14 @@ Available targets: | [terraform](#requirement\_terraform) | >= 1.0.0 | | [aws](#requirement\_aws) | >= 4.23.0 | | [null](#requirement\_null) | >= 2.0 | +| [random](#requirement\_random) | >= 2.0 | ## Providers | Name | Version | |------|---------| | [aws](#provider\_aws) | >= 4.23.0 | +| [random](#provider\_random) | >= 2.0 | ## Modules @@ -356,6 +358,7 @@ Available targets: | [aws_security_group_rule.ingress_cidr_blocks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.ingress_security_groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.traffic_inside_security_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | +| [random_pet.instance](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource | | [aws_iam_policy_document.enhanced_monitoring](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | diff --git a/docs/terraform.md b/docs/terraform.md index 117e4f04..a518b7ff 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -6,12 +6,14 @@ | [terraform](#requirement\_terraform) | >= 1.0.0 | | [aws](#requirement\_aws) | >= 4.23.0 | | [null](#requirement\_null) | >= 2.0 | +| [random](#requirement\_random) | >= 2.0 | ## Providers | Name | Version | |------|---------| | [aws](#provider\_aws) | >= 4.23.0 | +| [random](#provider\_random) | >= 2.0 | ## Modules @@ -42,6 +44,7 @@ | [aws_security_group_rule.ingress_cidr_blocks](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.ingress_security_groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | | [aws_security_group_rule.traffic_inside_security_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource | +| [random_pet.instance](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource | | [aws_iam_policy_document.enhanced_monitoring](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | | [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source | diff --git a/main.tf b/main.tf index d7262009..af0c6b1f 100644 --- a/main.tf +++ b/main.tf @@ -249,11 +249,20 @@ resource "aws_rds_cluster" "secondary" { } } +resource "random_pet" "instance" { + count = local.enabled ? 1 : 0 + prefix = var.cluster_identifier == "" ? module.this.id : var.cluster_identifier + keepers = { + cluster_family = var.cluster_family + instance_class = var.serverlessv2_scaling_configuration != null ? "db.serverless" : var.instance_type + } +} + resource "aws_rds_cluster_instance" "default" { count = local.cluster_instance_count - identifier = var.cluster_identifier == "" ? "${module.this.id}-${count.index + 1}" : "${var.cluster_identifier}-${count.index + 1}" + identifier = "${random_pet.instance[0].id}-${count.index + 1}" cluster_identifier = coalesce(join("", aws_rds_cluster.primary[*].id), join("", aws_rds_cluster.secondary[*].id)) - instance_class = var.serverlessv2_scaling_configuration != null ? "db.serverless" : var.instance_type + instance_class = random_pet.instance[0].keepers.instance_class db_subnet_group_name = join("", aws_db_subnet_group.default[*].name) db_parameter_group_name = join("", aws_db_parameter_group.default[*].name) publicly_accessible = var.publicly_accessible @@ -291,7 +300,8 @@ resource "aws_rds_cluster_instance" "default" { ] lifecycle { - ignore_changes = [engine_version] + ignore_changes = [engine_version] + create_before_destroy = true } } diff --git a/versions.tf b/versions.tf index 375a8719..467a9d83 100644 --- a/versions.tf +++ b/versions.tf @@ -10,5 +10,9 @@ terraform { source = "hashicorp/null" version = ">= 2.0" } + random = { + source = "hashicorp/random" + version = ">= 2.0" + } } }