Skip to content

Commit c60277c

Browse files
committed
add RDS child module for auto-drive
1 parent 07c696c commit c60277c

File tree

3 files changed

+269
-2
lines changed

3 files changed

+269
-2
lines changed

auto-drive/db.tf

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
################################################################################
4+
# RDS Module
5+
################################################################################
6+
7+
module "db" {
8+
source = "../templates/terraform/aws/rds/"
9+
10+
identifier = local.name
11+
12+
engine = "postgres"
13+
engine_version = "16"
14+
engine_lifecycle_support = "open-source-rds-extended-support-disabled"
15+
family = "postgres16" # DB parameter group
16+
major_engine_version = "16" # DB option group
17+
instance_class = "db.t4g.large"
18+
19+
allocated_storage = 50
20+
max_allocated_storage = 200
21+
22+
23+
db_name = "postgres"
24+
username = "postgres"
25+
port = 5432
26+
27+
28+
manage_master_user_password_rotation = true
29+
master_user_password_rotate_immediately = false
30+
master_user_password_rotation_schedule_expression = "rate(15 days)"
31+
32+
multi_az = true
33+
db_subnet_group_name = module.vpc_rds.database_subnet_group
34+
vpc_security_group_ids = [module.security_group.security_group_id]
35+
36+
maintenance_window = "Mon:00:00-Mon:03:00"
37+
backup_window = "03:00-06:00"
38+
enabled_cloudwatch_logs_exports = ["postgresql", "upgrade"]
39+
create_cloudwatch_log_group = true
40+
41+
backup_retention_period = 1
42+
skip_final_snapshot = true
43+
deletion_protection = false
44+
45+
performance_insights_enabled = true
46+
performance_insights_retention_period = 7
47+
create_monitoring_role = true
48+
monitoring_interval = 60
49+
monitoring_role_name = "example-monitoring-role-name"
50+
monitoring_role_use_name_prefix = true
51+
monitoring_role_description = "Description for monitoring role"
52+
53+
parameters = [
54+
{
55+
name = "autovacuum"
56+
value = 1
57+
},
58+
{
59+
name = "client_encoding"
60+
value = "utf8"
61+
}
62+
]
63+
64+
tags = local.tags
65+
db_option_group_tags = {
66+
"Sensitive" = "low"
67+
}
68+
db_parameter_group_tags = {
69+
"Sensitive" = "low"
70+
}
71+
cloudwatch_log_group_tags = {
72+
"Sensitive" = "high"
73+
}
74+
}
75+
76+
################################################################################
77+
# RDS Automated Backups Replication Module
78+
################################################################################
79+
80+
provider "aws" {
81+
alias = "region2"
82+
region = local.region2
83+
}
84+
85+
module "kms" {
86+
source = "terraform-aws-modules/kms/aws"
87+
version = "~> 1.0"
88+
description = "KMS key for cross region automated backups replication"
89+
90+
# Aliases
91+
aliases = [local.name]
92+
aliases_use_name_prefix = true
93+
94+
key_owners = [data.aws_caller_identity.current.arn]
95+
96+
tags = local.tags
97+
98+
providers = {
99+
aws = aws.region2
100+
}
101+
}
102+
103+
module "db_automated_backups_replication" {
104+
source = "../templates/terraform/aws/rds/modules/db_instance_automated_backups_replication"
105+
106+
source_db_instance_arn = module.db.db_instance_arn
107+
kms_key_arn = module.kms.key_arn
108+
109+
providers = {
110+
aws = aws.region2
111+
}
112+
}
113+
114+
################################################################################
115+
# Supporting Resources
116+
################################################################################
117+
118+
module "vpc_rds" {
119+
source = "terraform-aws-modules/vpc/aws"
120+
version = "~> 5.0"
121+
122+
name = local.name
123+
cidr = local.vpc_cidr
124+
125+
azs = local.azs
126+
public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)]
127+
private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 3)]
128+
database_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 6)]
129+
130+
create_database_subnet_group = true
131+
132+
tags = local.tags
133+
}
134+
135+
module "security_group" {
136+
source = "terraform-aws-modules/security-group/aws"
137+
version = "~> 5.0"
138+
139+
name = local.name
140+
description = "Auto Drive PostgreSQL security group"
141+
vpc_id = module.vpc_rds.vpc_id
142+
143+
# ingress
144+
ingress_with_cidr_blocks = [
145+
{
146+
from_port = 5432
147+
to_port = 5432
148+
protocol = "tcp"
149+
description = "PostgreSQL access from within VPC"
150+
cidr_blocks = module.vpc_rds.vpc_cidr_block
151+
},
152+
]
153+
154+
tags = local.tags
155+
}

auto-drive/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ data "aws_availability_zones" "available" {
77
}
88

99
locals {
10-
name = basename(path.cwd)
11-
region = var.region
10+
name = basename(path.cwd)
11+
region = var.region
12+
region2 = "us-west-1"
1213

1314
vpc_cidr = var.vpc_cidr
1415
azs = slice(data.aws_availability_zones.available.names, 0, var.az_count)

auto-drive/outputs.tf

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,114 @@ output "gateway_eip" {
7575
description = "Elastic IPs for Gateway instances"
7676
value = aws_eip.gateway_eip[*].public_ip
7777
}
78+
79+
80+
################################################################################
81+
# RDS Outputs
82+
################################################################################
83+
84+
output "db_instance_address" {
85+
description = "The address of the RDS instance"
86+
value = module.db.db_instance_address
87+
}
88+
89+
output "db_instance_arn" {
90+
description = "The ARN of the RDS instance"
91+
value = module.db.db_instance_arn
92+
}
93+
94+
output "db_instance_availability_zone" {
95+
description = "The availability zone of the RDS instance"
96+
value = module.db.db_instance_availability_zone
97+
}
98+
99+
output "db_instance_endpoint" {
100+
description = "The connection endpoint"
101+
value = module.db.db_instance_endpoint
102+
}
103+
104+
output "db_instance_engine" {
105+
description = "The database engine"
106+
value = module.db.db_instance_engine
107+
}
108+
109+
output "db_instance_engine_version_actual" {
110+
description = "The running version of the database"
111+
value = module.db.db_instance_engine_version_actual
112+
}
113+
114+
output "db_instance_hosted_zone_id" {
115+
description = "The canonical hosted zone ID of the DB instance (to be used in a Route 53 Alias record)"
116+
value = module.db.db_instance_hosted_zone_id
117+
}
118+
119+
output "db_instance_identifier" {
120+
description = "The RDS instance identifier"
121+
value = module.db.db_instance_identifier
122+
}
123+
124+
output "db_instance_resource_id" {
125+
description = "The RDS Resource ID of this instance"
126+
value = module.db.db_instance_resource_id
127+
}
128+
129+
output "db_instance_status" {
130+
description = "The RDS instance status"
131+
value = module.db.db_instance_status
132+
}
133+
134+
output "db_instance_name" {
135+
description = "The database name"
136+
value = module.db.db_instance_name
137+
}
138+
139+
output "db_instance_username" {
140+
description = "The master username for the database"
141+
value = module.db.db_instance_username
142+
sensitive = true
143+
}
144+
145+
output "db_instance_port" {
146+
description = "The database port"
147+
value = module.db.db_instance_port
148+
}
149+
150+
output "db_subnet_group_id" {
151+
description = "The db subnet group name"
152+
value = module.db.db_subnet_group_id
153+
}
154+
155+
output "db_subnet_group_arn" {
156+
description = "The ARN of the db subnet group"
157+
value = module.db.db_subnet_group_arn
158+
}
159+
160+
output "db_parameter_group_id" {
161+
description = "The db parameter group id"
162+
value = module.db.db_parameter_group_id
163+
}
164+
165+
output "db_parameter_group_arn" {
166+
description = "The ARN of the db parameter group"
167+
value = module.db.db_parameter_group_arn
168+
}
169+
170+
output "db_enhanced_monitoring_iam_role_arn" {
171+
description = "The Amazon Resource Name (ARN) specifying the monitoring role"
172+
value = module.db.enhanced_monitoring_iam_role_arn
173+
}
174+
175+
output "db_instance_cloudwatch_log_groups" {
176+
description = "Map of CloudWatch log groups created and their attributes"
177+
value = module.db.db_instance_cloudwatch_log_groups
178+
}
179+
180+
output "db_instance_master_user_secret_arn" {
181+
description = "The ARN of the master user secret (Only available when manage_master_user_password is set to true)"
182+
value = module.db.db_instance_master_user_secret_arn
183+
}
184+
185+
output "db_instance_secretsmanager_secret_rotation_enabled" {
186+
description = "Specifies whether automatic rotation is enabled for the secret"
187+
value = module.db.db_instance_secretsmanager_secret_rotation_enabled
188+
}

0 commit comments

Comments
 (0)