Skip to content

Commit

Permalink
Adding creation of db Subnet Groups
Browse files Browse the repository at this point in the history
Added flags to enable the creation of additional db_subnet_groups for both private and public subnets. By default, secure remains trhe default, but now the configuration allows for the creation of up to two additional subnet groups (private and public).
  • Loading branch information
alandavid committed Jul 29, 2024
1 parent 9fbda14 commit 477a833
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 14 deletions.
12 changes: 10 additions & 2 deletions _outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,16 @@ output "nat_gateway_ids" {
description = "List of NAT Gateway IDs"
}

output "db_subnet_group_id" {
value = aws_db_subnet_group.secure.id
output "db_subnet_group_secure_id" {
value = aws_db_subnet_group.secure[0].id
}

output "db_subnet_group_private_id" {
value = aws_db_subnet_group.private[0].id
}

output "db_subnet_group_public_id" {
value = aws_db_subnet_group.public[0].id
}

output "public_route_table_id" {
Expand Down
18 changes: 18 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,24 @@ variable "enable_firewall_default_rule" {
description = "Enable or disable the default stateful rule."
}

variable "create_dbsubgroup_secure" {
type = bool
default = true
description = "Create Secure Subgroup"
}

variable "create_dbsubgroup_public" {
type = bool
default = false
description = "Create Public Subgroup"
}

variable "create_dbsubgroup_private" {
type = bool
default = false
description = "Create Private Subgroup"
}

locals {
kubernetes_clusters = zipmap(
formatlist("kubernetes.io/cluster/%s", var.kubernetes_clusters),
Expand Down
20 changes: 11 additions & 9 deletions cf-exports.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ resource "aws_cloudformation_stack" "tf_exports" {
"VpcId" = aws_vpc.default.id,
"CidrBlock" = aws_vpc.default.cidr_block,
"InternetGatewayId" = aws_internet_gateway.default.id,
"PublicSubnetIds" = join(",", aws_subnet.public[*].id),
"PublicSubnetCidrs" = join(",", aws_subnet.public[*].cidr_block),
"PrivateSubnetIds" = join(",", aws_subnet.private[*].id),
"PrivateSubnetCidrs" = join(",", aws_subnet.private[*].cidr_block),
"SecureSubnetIds" = join(",", aws_subnet.secure[*].id),
"SecureSubnetCidrs" = join(",", aws_subnet.secure[*].cidr_block),
"NatGatewayIds" = var.nat ? join(",", aws_nat_gateway.nat_gw[*].id) : "undefined",
"DbSubnetGroupId" = aws_db_subnet_group.secure.id
"PublicSubnetIds" = join(",", aws_subnet.public.*.id),

Check warning on line 9 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"PublicSubnetCidrs" = join(",", aws_subnet.public.*.cidr_block),

Check warning on line 10 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"PrivateSubnetIds" = join(",", aws_subnet.private.*.id),

Check warning on line 11 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"PrivateSubnetCidrs" = join(",", aws_subnet.private.*.cidr_block),

Check warning on line 12 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"SecureSubnetIds" = join(",", aws_subnet.secure.*.id),

Check warning on line 13 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"SecureSubnetCidrs" = join(",", aws_subnet.secure.*.cidr_block),

Check warning on line 14 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"NatGatewayIds" = var.nat ? join(",", aws_nat_gateway.nat_gw.*.id) : "undefined",

Check warning on line 15 in cf-exports.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets
"DbSubnetGroupId" = aws_db_subnet_group.secure[0].id,
"DbSubnetPrivateGroupId" = try(aws_db_subnet_group.private[0].id,"")
"DbSubnetPublicGroupId" = try(aws_db_subnet_group.public[0].id,"")
}
})
}
}
38 changes: 35 additions & 3 deletions db-subnet.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
resource "aws_db_subnet_group" "secure" {
name = lower(format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix))
subnet_ids = aws_subnet.secure[*].id
count = var.create_dbsubgroup_secure ? 1 : 0
name = lower("${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-secure")
subnet_ids = aws_subnet.secure.*.id

Check warning on line 4 in db-subnet.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets

tags = merge(
var.tags,
{
"Name" = format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)
"Name" = "${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-secure"
"Scheme" = "secure"
"EnvName" = var.name
},
)
}

resource "aws_db_subnet_group" "private" {
count = var.create_dbsubgroup_private ? 1 : 0
name = lower("${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-private")
subnet_ids = aws_subnet.private.*.id

Check warning on line 19 in db-subnet.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets

tags = merge(
var.tags,
{
"Name" = "${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-private"
"Scheme" = "private"
"EnvName" = var.name
},
)
}

resource "aws_db_subnet_group" "public" {
count = var.create_dbsubgroup_public ? 1 : 0
name = lower("${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-public")
subnet_ids = aws_subnet.public.*.id

Check warning on line 34 in db-subnet.tf

View workflow job for this annotation

GitHub Actions / Lint

List items should be accessed using square brackets

tags = merge(
var.tags,
{
"Name" = "${format(local.names[var.name_pattern].db_subnet, var.name, local.name_suffix)}-public"
"Scheme" = "public"
"EnvName" = var.name
},
)
}

0 comments on commit 477a833

Please sign in to comment.