From 125c426fc00cd1f2744189a30f9225778169cefb Mon Sep 17 00:00:00 2001
From: Melissa Greenbaum <69476188+magreenbaum@users.noreply.github.com>
Date: Wed, 6 Jul 2022 17:12:00 -0400
Subject: [PATCH] feat: Add support for `identifier_prefix` (#416)
---
README.md | 1 +
examples/complete-postgres/main.tf | 3 ++-
main.tf | 5 +++--
modules/db_instance/README.md | 1 +
modules/db_instance/main.tf | 6 +++++-
modules/db_instance/variables.tf | 6 ++++++
variables.tf | 6 ++++++
7 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 1ab484b8..84a29319 100644
--- a/README.md
+++ b/README.md
@@ -275,6 +275,7 @@ Users have the ability to:
| [iam\_database\_authentication\_enabled](#input\_iam\_database\_authentication\_enabled) | Specifies whether or not the mappings of AWS Identity and Access Management (IAM) accounts to database accounts are enabled | `bool` | `false` | no |
| [identifier](#input\_identifier) | The name of the RDS instance | `string` | n/a | yes |
| [instance\_class](#input\_instance\_class) | The instance type of the RDS instance | `string` | `null` | no |
+| [instance\_use\_identifier\_prefix](#input\_instance\_use\_identifier\_prefix) | Determines whether to use `identifier` as is or create a unique identifier beginning with `identifier` as the specified prefix | `bool` | `false` | no |
| [iops](#input\_iops) | The amount of provisioned IOPS. Setting this implies a storage\_type of 'io1' | `number` | `0` | no |
| [kms\_key\_id](#input\_kms\_key\_id) | The ARN for the KMS encryption key. If creating an encrypted replica, set this to the destination KMS ARN. If storage\_encrypted is set to true and kms\_key\_id is not specified the default KMS key created in your account will be used | `string` | `null` | no |
| [license\_model](#input\_license\_model) | License model information for this DB instance. Optional, but required for some DB engines, i.e. Oracle SE1 | `string` | `null` | no |
diff --git a/examples/complete-postgres/main.tf b/examples/complete-postgres/main.tf
index a757dc36..2b279147 100644
--- a/examples/complete-postgres/main.tf
+++ b/examples/complete-postgres/main.tf
@@ -124,7 +124,8 @@ module "db" {
module "db_default" {
source = "../../"
- identifier = "${local.name}-default"
+ identifier = "${local.name}-default"
+ instance_use_identifier_prefix = true
create_db_option_group = false
create_db_parameter_group = false
diff --git a/main.tf b/main.tf
index a6dcb34b..f7a265b5 100644
--- a/main.tf
+++ b/main.tf
@@ -69,8 +69,9 @@ module "db_option_group" {
module "db_instance" {
source = "./modules/db_instance"
- create = local.create_db_instance
- identifier = var.identifier
+ create = local.create_db_instance
+ identifier = var.identifier
+ use_identifier_prefix = var.instance_use_identifier_prefix
engine = var.engine
engine_version = var.engine_version
diff --git a/modules/db_instance/README.md b/modules/db_instance/README.md
index c51258c2..2d4edfc3 100644
--- a/modules/db_instance/README.md
+++ b/modules/db_instance/README.md
@@ -93,6 +93,7 @@ No modules.
| [tags](#input\_tags) | A mapping of tags to assign to all resources | `map(string)` | `{}` | no |
| [timeouts](#input\_timeouts) | Updated Terraform resource management timeouts. Applies to `aws_db_instance` in particular to permit resource management times | `map(string)` | `{}` | no |
| [timezone](#input\_timezone) | Time zone of the DB instance. timezone is currently only supported by Microsoft SQL Server. The timezone can only be set on creation. See MSSQL User Guide for more information. | `string` | `null` | no |
+| [use\_identifier\_prefix](#input\_use\_identifier\_prefix) | Determines whether to use `identifier` as is or create a unique identifier beginning with `identifier` as the specified prefix | `bool` | `false` | no |
| [username](#input\_username) | Username for the master DB user | `string` | `null` | no |
| [vpc\_security\_group\_ids](#input\_vpc\_security\_group\_ids) | List of VPC security groups to associate | `list(string)` | `[]` | no |
diff --git a/modules/db_instance/main.tf b/modules/db_instance/main.tf
index cf28adfc..99e086ba 100644
--- a/modules/db_instance/main.tf
+++ b/modules/db_instance/main.tf
@@ -3,6 +3,9 @@ locals {
final_snapshot_identifier = var.skip_final_snapshot ? null : "${var.final_snapshot_identifier_prefix}-${var.identifier}-${try(random_id.snapshot_identifier[0].hex, "")}"
+ identifier = var.use_identifier_prefix ? null : var.identifier
+ identifier_prefix = var.use_identifier_prefix ? "${var.identifier}-" : null
+
# Replicas will use source metadata
username = var.replicate_source_db != null ? null : var.username
password = var.replicate_source_db != null ? null : var.password
@@ -26,7 +29,8 @@ resource "random_id" "snapshot_identifier" {
resource "aws_db_instance" "this" {
count = var.create ? 1 : 0
- identifier = var.identifier
+ identifier = local.identifier
+ identifier_prefix = local.identifier_prefix
engine = local.engine
engine_version = local.engine_version
diff --git a/modules/db_instance/variables.tf b/modules/db_instance/variables.tf
index 71e650e6..7c664206 100644
--- a/modules/db_instance/variables.tf
+++ b/modules/db_instance/variables.tf
@@ -9,6 +9,12 @@ variable "identifier" {
type = string
}
+variable "use_identifier_prefix" {
+ description = "Determines whether to use `identifier` as is or create a unique identifier beginning with `identifier` as the specified prefix"
+ type = bool
+ default = false
+}
+
variable "allocated_storage" {
description = "The allocated storage in gigabytes"
type = string
diff --git a/variables.tf b/variables.tf
index 001a4b7d..56913d1e 100644
--- a/variables.tf
+++ b/variables.tf
@@ -3,6 +3,12 @@ variable "identifier" {
type = string
}
+variable "instance_use_identifier_prefix" {
+ description = "Determines whether to use `identifier` as is or create a unique identifier beginning with `identifier` as the specified prefix"
+ type = bool
+ default = false
+}
+
variable "allocated_storage" {
description = "The allocated storage in gigabytes"
type = string