|
| 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