1+ module "secrets" {
2+ source = " github.com/codeforamerica/tofu-modules-aws-secrets?ref=1.0.0"
3+
4+ project = var. project
5+ environment = var. environment
6+ }
7+
18module "service" {
2- source = " github.com/codeforamerica/tofu-modules-aws-fargate-service?ref=1.2.1 "
9+ source = " github.com/codeforamerica/tofu-modules-aws-fargate-service?ref=security-group-outputs "
310 for_each = var. services
411
512 project = var. project
@@ -12,15 +19,78 @@ module "service" {
1219 domain = var. domain
1320 subdomain = try (each. value . subdomain , " www" )
1421
15- vpc_id = var. vpc_id
16- private_subnets = var. private_subnets
17- public_subnets = var. public_subnets
18- logging_key_id = var. logging_key_arn
19- container_port = try (each. value . expose , 3000 )
22+ vpc_id = var. vpc_id
23+ private_subnets = var. private_subnets
24+ public_subnets = var. public_subnets
25+ logging_key_id = var. logging_key_arn
26+ container_port = try (each. value . expose , 3000 )
2027 create_version_parameter = true
2128
22- tags = {
23- application = " ${ var . project } -${ var . environment } "
24- program = var.program
29+ environment_variables = {
30+ DATABASE_HOST = module.mssql.db_instance_endpoint
31+ }
32+
33+ environment_secrets = {
34+ DATABASE_USERNAME = " ${ module . mssql . db_instance_master_user_secret_arn } :usernmae"
35+ DATABASE_PASSWORD = " ${ module . mssql . db_instance_master_user_secret_arn } :password"
2536 }
37+
38+ tags = local. tags
39+ }
40+
41+ module "mssql" {
42+ source = " terraform-aws-modules/rds/aws"
43+ version = " >= 6.12"
44+
45+ identifier = local. prefix
46+ instance_use_identifier_prefix = true
47+ engine = local. database_engine
48+ engine_version = data. aws_rds_engine_version . this . version
49+ auto_minor_version_upgrade = true
50+ apply_immediately = ! local. production
51+ subnet_ids = var. private_subnets
52+ create_db_subnet_group = true
53+ create_db_option_group = false
54+ family = data. aws_rds_engine_version . this . parameter_group_family
55+ instance_class = " db.t3.small"
56+ allocated_storage = 20
57+ max_allocated_storage = 100
58+ username = " root"
59+ storage_type = " gp3"
60+ kms_key_id = aws_kms_key. database . arn
61+ master_user_secret_kms_key_id = module. secrets . kms_key_arn
62+ performance_insights_kms_key_id = var. logging_key_arn
63+ cloudwatch_log_group_kms_key_id = var. logging_key_arn
64+ cloudwatch_log_group_retention_in_days = local. production ? 31 : 7
65+ create_cloudwatch_log_group = true
66+ create_monitoring_role = true
67+ enabled_cloudwatch_logs_exports = data. aws_rds_engine_version . this . exportable_log_types
68+ vpc_security_group_ids = [module . database_security_group . security_group_id ]
69+
70+ allow_major_version_upgrade = ! local. production
71+
72+ tags = local. tags
73+ }
74+
75+ # Create an empty security group for the database. To avoid a circular
76+ # dependency between the database and the services, we create the security group
77+ # here and then add the ingress rules in a separate resource.
78+ module "database_security_group" {
79+ source = " terraform-aws-modules/security-group/aws"
80+ version = " ~> 5.3"
81+
82+ name = " ${ local . prefix } -database"
83+ vpc_id = var. vpc_id
84+
85+ tags = local. tags
86+ }
87+
88+ resource "aws_vpc_security_group_ingress_rule" "database" {
89+ for_each = module. service
90+ security_group_id = module. database_security_group . security_group_id
91+
92+ ip_protocol = " tcp"
93+ from_port = module. mssql . db_instance_port
94+ to_port = module. mssql . db_instance_port
95+ referenced_security_group_id = each. value . security_group_id
2696}
0 commit comments