Skip to content

Commit

Permalink
adding custom naming override if applicable (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsaucier-sf authored Sep 11, 2023
1 parent 280cbab commit d4ca82f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ module "aurora" {
| <a name="input_aurora_cluster_enabled"></a> [aurora\_cluster\_enabled](#input\_aurora\_cluster\_enabled) | Enable creation of an Aurora Cluster | `bool` | `false` | no |
| <a name="input_aurora_cluster_family"></a> [aurora\_cluster\_family](#input\_aurora\_cluster\_family) | The family of the DB cluster parameter group | `string` | `"aurora-postgresql14"` | no |
| <a name="input_aurora_cluster_name"></a> [aurora\_cluster\_name](#input\_aurora\_cluster\_name) | Database name (default is not to create a database) | `string` | `""` | no |
| <a name="input_aurora_cluster_name_override"></a> [aurora\_cluster\_name\_override](#input\_aurora\_cluster\_name\_override) | If `true`, this will set a the Aurora Cluster name to what is defined in var.aurora\_cluster\_name.<br>If `false`, this will prepend ${var.namespace}-${var.environment} to ${var.aurora\_cluster\_name}" | `bool` | `false` | no |
| <a name="input_aurora_cluster_size"></a> [aurora\_cluster\_size](#input\_aurora\_cluster\_size) | Number of DB instances to create in the cluster | `number` | `0` | no |
| <a name="input_aurora_db_admin_password"></a> [aurora\_db\_admin\_password](#input\_aurora\_db\_admin\_password) | Password of the DB admin | `string` | `""` | no |
| <a name="input_aurora_db_admin_username"></a> [aurora\_db\_admin\_username](#input\_aurora\_db\_admin\_username) | Name of the default DB admin user role | `string` | `""` | no |
Expand Down Expand Up @@ -172,6 +173,7 @@ module "aurora" {
| <a name="input_rds_instance_major_engine_version"></a> [rds\_instance\_major\_engine\_version](#input\_rds\_instance\_major\_engine\_version) | major\_engine\_version Database MAJOR engine version, depends on engine type | `string` | `"14"` | no |
| <a name="input_rds_instance_multi_az"></a> [rds\_instance\_multi\_az](#input\_rds\_instance\_multi\_az) | Set to true if multi AZ deployment must be supported | `bool` | `false` | no |
| <a name="input_rds_instance_name"></a> [rds\_instance\_name](#input\_rds\_instance\_name) | RDS Instance name | `string` | `""` | no |
| <a name="input_rds_instance_name_override"></a> [rds\_instance\_name\_override](#input\_rds\_instance\_name\_override) | If `true`, this will set a the RDS Instance name to what is defined in var.rds\_instance\_name.<br>If `false`, this will prepend ${var.namespace}-${var.environment} to ${var.rds\_instance\_name}" | `bool` | `false` | no |
| <a name="input_rds_instance_option_group_name"></a> [rds\_instance\_option\_group\_name](#input\_rds\_instance\_option\_group\_name) | Name of the DB option group to associate | `string` | `""` | no |
| <a name="input_rds_instance_publicly_accessible"></a> [rds\_instance\_publicly\_accessible](#input\_rds\_instance\_publicly\_accessible) | Determines if database can be publicly available (NOT recommended) | `bool` | `false` | no |
| <a name="input_rds_instance_security_group_ids"></a> [rds\_instance\_security\_group\_ids](#input\_rds\_instance\_security\_group\_ids) | The IDs of the security groups from which to allow ingress traffic to the DB instance | `list(string)` | `[]` | no |
Expand Down
2 changes: 2 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ locals {
rds_instance_option_group_name = length(aws_db_option_group.this) > 0 ? aws_db_option_group.this[0].name : var.rds_instance_option_group_name

## aurora
aurora_cluster_name = var.aurora_cluster_name_override == true ? var.aurora_cluster_name : "${var.namespace}-${var.environment}-${var.aurora_cluster_name}"
aurora_ssm_params = var.aurora_cluster_enabled == true ? [
{
name = "/${var.namespace}/${var.environment}/${var.aurora_cluster_name}/cluster_admin_db_password"
Expand All @@ -28,6 +29,7 @@ locals {
} : {}
## rds
rds_instance_name = var.rds_instance_name_override == true ? var.rds_instance_name : "${var.namespace}-${var.environment}-${var.rds_instance_name}"
rds_instance_ssm_params = var.rds_instance_enabled == true ? [
{
name = "/${var.namespace}/${var.environment}/${var.rds_instance_name}/admin_db_password"
Expand Down
40 changes: 23 additions & 17 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "aws_kms_key" "rds_db_kms_key" {
resource "aws_kms_alias" "rds_db_kms_key" {
count = var.rds_instance_enabled == true ? 1 : 0

name = "alias/${var.namespace}-${var.environment}-${var.rds_instance_name}"
name = "alias/${local.rds_instance_name}"
target_key_id = aws_kms_key.rds_db_kms_key[0].id
}

Expand Down Expand Up @@ -142,9 +142,7 @@ module "aurora_cluster" {
source = "git::https://github.com/cloudposse/terraform-aws-rds-cluster.git?ref=1.3.2"
count = var.aurora_cluster_enabled == true ? 1 : 0

name = var.aurora_cluster_name
namespace = var.namespace
stage = var.environment
name = local.aurora_cluster_name

engine = var.aurora_engine
engine_mode = var.aurora_engine_mode
Expand Down Expand Up @@ -188,7 +186,10 @@ module "aurora_cluster" {
serverlessv2_scaling_configuration = var.aurora_serverlessv2_scaling_configuration

tags = merge(var.tags, tomap({
Name = var.aurora_cluster_name
Name = var.aurora_cluster_name
Namespace = var.namespace
Environment = var.environment
Stage = var.environment
}))
}

Expand All @@ -199,9 +200,7 @@ module "db_management" {
source = "git::https://github.com/cloudposse/terraform-aws-s3-bucket?ref=3.0.0"
count = var.rds_enable_custom_option_group == true ? 1 : 0

name = "${var.rds_instance_name}-db-management"
stage = var.environment
namespace = var.namespace
name = "${local.rds_instance_name}-db-management"

acl = "private"
enabled = true
Expand All @@ -211,7 +210,11 @@ module "db_management" {
kms_master_key_arn = "arn:${data.aws_partition.this.partition}:kms:${var.region}:${var.account_id}:alias/aws/s3"
sse_algorithm = "aws:kms"

tags = var.tags
tags = merge(var.tags, tomap({
Namespace = var.namespace
Environment = var.environment
Stage = var.environment
}))
}

################################################################################
Expand Down Expand Up @@ -241,7 +244,7 @@ resource "aws_iam_role" "option_group" {
resource "aws_iam_policy" "option_group" {
count = var.rds_enable_custom_option_group == true ? 1 : 0

name_prefix = "${var.namespace}-${var.environment}-${var.rds_instance_name}-"
name_prefix = "${local.rds_instance_name}-"

policy = jsonencode(
{
Expand Down Expand Up @@ -302,8 +305,8 @@ resource "aws_iam_role_policy_attachment" "option_group" {
resource "aws_db_option_group" "this" {
count = var.rds_enable_custom_option_group == true ? 1 : 0

name = "${var.namespace}-${var.environment}-${var.rds_instance_name}-option-group"
option_group_description = "${var.namespace}-${var.environment}-${var.rds_instance_name} Custom Option Group"
name = "${local.rds_instance_name}-option-group"
option_group_description = "${local.rds_instance_name} Custom Option Group"
engine_name = var.rds_instance_engine
major_engine_version = var.rds_instance_major_engine_version

Expand Down Expand Up @@ -333,7 +336,7 @@ resource "aws_db_option_group" "this" {
}

tags = merge(var.tags, tomap({
Name = "${var.namespace}-${var.environment}-${var.rds_instance_name}-option-group"
Name = "${local.rds_instance_name}-option-group"
}))
}

Expand All @@ -352,9 +355,8 @@ module "rds_instance" {
count = var.rds_instance_enabled == true ? 1 : 0
source = "git::https://github.com/cloudposse/terraform-aws-rds?ref=0.40.0"

stage = var.environment
name = var.rds_instance_name
namespace = var.namespace
name = local.rds_instance_name

dns_zone_id = var.rds_instance_dns_zone_id
host_name = var.rds_instance_host_name
vpc_id = var.vpc_id
Expand Down Expand Up @@ -398,7 +400,11 @@ module "rds_instance" {
iam_database_authentication_enabled = var.iam_database_authentication_enabled
timeouts = var.timeouts

tags = var.tags
tags = merge(var.tags, tomap({
Namespace = var.namespace
Environment = var.environment
Stage = var.environment
}))
}

################################################################################
Expand Down
18 changes: 18 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ variable "aurora_cluster_enabled" {
default = false
}

variable "aurora_cluster_name_override" {
type = bool
description = <<-EOT
If `true`, this will set a the Aurora Cluster name to what is defined in var.aurora_cluster_name.
If `false`, this will prepend $${var.namespace}-$${var.environment} to $${var.aurora_cluster_name}"
EOT
default = false
}

variable "aurora_cluster_name" {
type = string
description = "Database name (default is not to create a database)"
Expand Down Expand Up @@ -213,6 +222,15 @@ variable "rds_instance_iops" {
default = 0
}

variable "rds_instance_name_override" {
type = bool
description = <<-EOT
If `true`, this will set a the RDS Instance name to what is defined in var.rds_instance_name.
If `false`, this will prepend $${var.namespace}-$${var.environment} to $${var.rds_instance_name}"
EOT
default = false
}

variable "rds_instance_name" {
type = string
description = "RDS Instance name"
Expand Down

0 comments on commit d4ca82f

Please sign in to comment.