Skip to content

Commit 6f38e8b

Browse files
authored
Add postgres config (#7)
### TL;DR Adds RDS Aurora PostgreSQL database configuration and connects it to xmtpd services. ### What changed? - Added RDS Aurora PostgreSQL 16 cluster configuration with 2 instances - Updated module sources to reference GitHub repository paths - Connected database URL to xmtpd-api and xmtpd-worker services - Configured security groups for database access within VPC - Set up database parameter groups and subnet groups - Implemented password generation for database access
1 parent 676519f commit 6f38e8b

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

.tflint.hcl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ rule "terraform_required_providers" {
1212

1313
rule "terraform_required_version" {
1414
enabled = false
15-
}
15+
}

terraform/examples/aws-complete/main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
module "mls_validation_service" {
3-
source = "./aws/xmtp-validation-service" # TODO: Replace with git URL once merged to main
3+
# tflint-ignore: terraform_module_pinned_source
4+
source = "github.com/xmtp/xmtpd-infrastructure//terraform/aws/xmtp-validation-service"
45
depends_on = [module.vpc, aws_service_discovery_private_dns_namespace.xmtp]
56

67
env = terraform.workspace
@@ -18,7 +19,8 @@ module "mls_validation_service" {
1819
}
1920

2021
module "xmtpd_api" {
21-
source = "./aws/xmtpd-api" # TODO: Replace with git URL once merged to main
22+
# tflint-ignore: terraform_module_pinned_source
23+
source = "github.com/xmtp/xmtpd-infrastructure//terraform/aws/xmtpd-api"
2224

2325
vpc_id = module.vpc.vpc_id
2426
public_subnets = module.vpc.public_subnets
@@ -36,7 +38,7 @@ module "xmtpd_api" {
3638
service_secrets = {
3739
signer_private_key = var.signer_private_key
3840
chain_rpc_url = var.chain_rpc_url
39-
database_url = "CHANGE_ME" # TODO:nm add database
41+
database_url = "postgres://${aws_rds_cluster.cluster.master_username}:${aws_rds_cluster.cluster.master_password}@${aws_rds_cluster.cluster.endpoint}:5432/${aws_rds_cluster.cluster.database_name}?sslmode=disable"
4042
}
4143
enable_debug_logs = false
4244

@@ -46,7 +48,8 @@ module "xmtpd_api" {
4648
}
4749

4850
module "xmtpd_worker" {
49-
source = "./aws/xmtpd-worker" # TODO: Replace with git URL once merged to main
51+
# tflint-ignore: terraform_module_pinned_source
52+
source = "github.com/xmtp/xmtpd-infrastructure//terraform/aws/xmtpd-worker"
5053

5154
vpc_id = module.vpc.vpc_id
5255
public_subnets = module.vpc.public_subnets
@@ -62,7 +65,7 @@ module "xmtpd_worker" {
6265
service_secrets = {
6366
signer_private_key = var.signer_private_key
6467
chain_rpc_url = var.chain_rpc_url
65-
database_url = "CHANGE_ME" # TODO:nm add database
68+
database_url = "postgres://${aws_rds_cluster.cluster.master_username}:${aws_rds_cluster.cluster.master_password}@${aws_rds_cluster.cluster.endpoint}:5432/${aws_rds_cluster.cluster.database_name}?sslmode=disable"
6669
}
6770
enable_debug_logs = false
6871

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
locals {
2+
db_engine_version = "16"
3+
db_name = "xmtp"
4+
db_root_user = "xmtp"
5+
is_production_environment = false
6+
db_num_instances = 2
7+
db_instance_class = "db.t4g.medium"
8+
db_ca_certificate_identifier = "rds-ca-rsa2048-g1"
9+
db_parameter_group_family = "aurora-postgresql16"
10+
}
11+
12+
resource "random_password" "password" {
13+
length = 64
14+
special = false
15+
}
16+
17+
resource "aws_rds_cluster" "cluster" {
18+
engine = "aurora-postgresql"
19+
engine_version = local.db_engine_version
20+
availability_zones = module.vpc.azs
21+
database_name = local.db_name
22+
master_username = local.db_root_user
23+
master_password = random_password.password.result
24+
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.default.name
25+
db_subnet_group_name = aws_db_subnet_group.cluster.name
26+
vpc_security_group_ids = [aws_security_group.rds.id]
27+
deletion_protection = local.is_production_environment
28+
apply_immediately = true
29+
# These will need to be turned off for production usage
30+
backup_retention_period = local.is_production_environment ? 30 : 1
31+
skip_final_snapshot = local.is_production_environment ? false : true
32+
33+
lifecycle {
34+
ignore_changes = [
35+
availability_zones
36+
]
37+
}
38+
}
39+
40+
resource "aws_rds_cluster_instance" "instances" {
41+
count = local.db_num_instances
42+
43+
cluster_identifier = aws_rds_cluster.cluster.id
44+
instance_class = local.db_instance_class
45+
engine = aws_rds_cluster.cluster.engine
46+
engine_version = aws_rds_cluster.cluster.engine_version
47+
auto_minor_version_upgrade = false
48+
ca_cert_identifier = local.db_ca_certificate_identifier
49+
publicly_accessible = false
50+
performance_insights_enabled = true
51+
db_subnet_group_name = aws_db_subnet_group.cluster.name
52+
apply_immediately = true
53+
}
54+
55+
resource "aws_db_subnet_group" "cluster" {
56+
subnet_ids = module.vpc.private_subnets
57+
}
58+
59+
# Create a parameter group so that we can adjust parameters later without recreating the cluster
60+
resource "aws_rds_cluster_parameter_group" "default" {
61+
family = local.db_parameter_group_family
62+
description = "RDS cluster parameter group"
63+
64+
parameter {
65+
name = "log_temp_files"
66+
# Log any temp files greater than 1MB
67+
value = "1000"
68+
apply_method = "pending-reboot"
69+
}
70+
}
71+
72+
73+
resource "aws_security_group" "rds" {
74+
description = "RDS security group"
75+
vpc_id = module.vpc.vpc_id
76+
}
77+
78+
resource "aws_security_group_rule" "ingress" {
79+
description = "Allow Postgres traffic from our VPC"
80+
cidr_blocks = [module.vpc.vpc_cidr_block]
81+
from_port = 5432
82+
protocol = "tcp"
83+
security_group_id = aws_security_group.rds.id
84+
to_port = 5432
85+
type = "ingress"
86+
}

0 commit comments

Comments
 (0)