PostgreSQL RDS
This guide describes how to provision an Amazon RDS PostgreSQL database.
A PostgreSQL RDS instance provides a managed relational database service where AWS handles backups, patching, monitoring, and high availability. Applications can connect to the database using a connection string stored in AWS Secrets Manager.
Note
PostgreSQL RDS is optional. Only create an RDS instance if your application requires persistent relational data storage.
Provision a PostgreSQL RDS Instance
The PostgreSQL database runs inside the AWS account and can be accessed by applications running on ECS Fargate, AWS Lambda, or other AWS services through private networking.
Create a PostgreSQL RDS Instance
Note
The following data sources and local values are only required if they are not already defined elsewhere in the Terraform configuration.
Shared Configuration
data "aws_route53_zone" "route53_zone" {
provider = aws.route53
name = "test.bymoslo.net" # Change based on the target environment
}
data "aws_ssm_parameter" "vpc_information" {
name = "/bym/vpc"
}
locals {
vpc = jsondecode(data.aws_ssm_parameter.vpc_information.insecure_value)
route53_domain = "test.bymoslo.net" # Change based on the target environment
db_instance_type = "db.t4g.micro"
disk_type = "gp3"
disk_io = 3000
db_disk_size = 20
}
Create the RDS Instance
module "rds" {
source = "git@github.com:BYM-IKT/terraform-aws-rds-postgresql.git?ref=v4"
application_name = "kattehotell"
environment = var.environment
instance_class = local.db_instance_type
major_engine_version = <latest PostgreSQL major version>
disk_size = local.db_disk_size
disk_type = local.disk_type
disk_io = local.disk_io
# Optional: override default autoscaling threshold (especially for production cases)
disk_autoscaling_size = <optional maximum-storage-threshold>
vpc = local.vpc
transit_gateway_cidr = "172.20.0.0/20"
backup_settings = {
hourly = true
daily = true
biweekly = false
biannuall = false
monthly = true
daily_retention_days = 35
}
providers = {
aws = aws.byks
}
}
output "rds" {
value = module.rds
sensitive = true
}
Important considerations
Warning
When using gp3, ensure that disk_io is configured appropriately for the workload.
Note
Backup configuration is typically enabled for prod environments, depending on recovery and retention requirements.
Warning
Before selecting or changing the instance type, disk size or backup configuration, double-check with Team Cloud to ensure the configuration aligns with platform standards, cost, and performance expectations.
Route53 Record (Optional)
A Route53 CNAME record provides a stable DNS name for the database instead of relying on the AWS-generated RDS endpoint.
By default, RDS endpoints can change if the instance is recreated or replaced. Using a Route53 alias ensures applications always connect using a consistent hostname.
This is optional, but recommended when:
- Applications should not depend directly on the AWS RDS endpoint.
- You want a stable, environment-friendly database
hostname. - You expect possible database replacement or migration in the future.
Example:
resource "aws_route53_record" "CNAME" {
provider = aws.route53
zone_id = data.aws_route53_zone.route53_zone.zone_id
name = "${var.application_name}-rds.${data.aws_route53_zone.route53_zone.name}"
type = "CNAME"
ttl = "60"
records = [module.rds.rds_endpoint]
}
Configuration
Application Name
Used for naming AWS resources and identifying the database instance.
Environment
Determines which environment (for example test or prod) the database belongs to.Database Version
Use the latest PostgreSQL major version supported by both AWS RDS and the BYM PostgreSQL Terraform module.
This defines the PostgreSQL major version used for the RDS instance. Always ensure compatibility with application drivers and any Terraform module constraints before upgrading.Instance Size
Defines the compute capacity of the RDS instance (CPU and memory).Typical values include:
- db.t4g.micro
- db.t4g.small
- db.t4g.medium
Choose the instance size based on workload requirements such as:
- Expected database traffic
- How complex the query is
- Memory requirements
- Environment type (test compared to prod)
Storage Configuration
Note
In production environments, storage auto-scaling is enabled by default, and the auto-scaling threshold is set to local.db_disk_size * 1.5.
Override the default by setting disk_autoscaling_size if a different threshold is required.
For gp2, IOPS are managed automatically by AWS based on the allocated storage size.
For gp3, specify disk_io according to workload requirements. The minimum disk size is 20 GB.
Example:
Backup Configuration
Backup configuration is optional and should be enabled according to the product’s recovery and retention requirements, typically for production environments.
Note
A standard backup plan is typically enabled in prod environments when the RDS instance is created.