Skip to content

Commit f8ababe

Browse files
authored
feat: Made the database optional. (#8)
1 parent 9b17644 commit f8ababe

4 files changed

Lines changed: 24 additions & 57 deletions

File tree

tofu/config/development/infra/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module "app" {
5353
for_each = local.apps
5454

5555
project = each.value.name
56+
project_short = try(each.value.name_short, each.value.name)
5657
environment = "development"
5758
program = each.value.program
5859
services = each.value.services

tofu/modules/app/database.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module "database" {
22
source = "../database"
3+
for_each = var.database_engine != null ? toset(["this"]) : toset([])
34

45
project = var.project
56
environment = var.environment
@@ -14,11 +15,22 @@ module "database" {
1415
}
1516

1617
resource "aws_vpc_security_group_ingress_rule" "database" {
17-
for_each = module.service
18-
security_group_id = module.database.security_group_id
18+
for_each = length(module.database) > 0 ? module.service : {}
19+
security_group_id = module.database["this"].security_group_id
1920

2021
ip_protocol = "tcp"
21-
from_port = module.database.port
22-
to_port = module.database.port
22+
from_port = module.database["this"].port
23+
to_port = module.database["this"].port
2324
referenced_security_group_id = each.value.security_group_id
2425
}
26+
27+
locals {
28+
database_environment_variables = {
29+
DATABASE_HOST = try(module.database["this"].host, null)
30+
DATABASE_PORT = try(module.database["this"].port, null)
31+
}
32+
database_environment_secrets = {
33+
DATABASE_USERNAME = length(module.database) > 0 ? "${module.database["this"].secret_arn}:username" : null
34+
DATABASE_PASSWORD = length(module.database) > 0 ? "${module.database["this"].secret_arn}:password" : null
35+
}
36+
}

tofu/modules/app/main.tf

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ module "service" {
2828
container_port = try(each.value.expose, 3000)
2929
create_version_parameter = true
3030

31-
environment_variables = {
32-
DATABASE_HOST = module.database.host
33-
DATABASE_PORT = module.database.port
34-
}
35-
36-
environment_secrets = {
37-
DATABASE_USERNAME = "${module.database.secret_arn}:username"
38-
DATABASE_PASSWORD = "${module.database.secret_arn}:password"
39-
}
31+
environment_variables = tomap({
32+
for k, v in local.database_environment_variables : k => v if v != "" && v != null
33+
})
34+
35+
environment_secrets = tomap({
36+
for k, v in local.database_environment_secrets : k => v if v != "" && v != null
37+
})
4038

4139
tags = local.tags
4240
}

tofu/modules/app/moved.tf

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)