Skip to content

Commit

Permalink
reopen #192 (#213)
Browse files Browse the repository at this point in the history
* 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 <ben.smith.developer@gmail.com>
  • Loading branch information
finchr and Benbentwo authored Jun 7, 2024
1 parent 4a9e83c commit 54be61f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,14 @@ Available targets:
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.23.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.23.0 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |

## Modules

Expand Down Expand Up @@ -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 |

Expand Down
3 changes: 3 additions & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.23.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.23.0 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |

## Modules

Expand Down Expand Up @@ -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 |

Expand Down
16 changes: 13 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -291,7 +300,8 @@ resource "aws_rds_cluster_instance" "default" {
]

lifecycle {
ignore_changes = [engine_version]
ignore_changes = [engine_version]
create_before_destroy = true
}
}

Expand Down
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ terraform {
source = "hashicorp/null"
version = ">= 2.0"
}
random = {
source = "hashicorp/random"
version = ">= 2.0"
}
}
}

0 comments on commit 54be61f

Please sign in to comment.