Skip to content

rds-postgres

A single, production-basic RDS PostgreSQL instance: encrypted at rest with the AWS managed key, private (publicly_accessible = false), master password generated and stored in Secrets Manager by RDS (manage_master_user_password), automated backups, deletion protection on by default and a security group that only admits the security groups you list.

Aurora, a customer-managed KMS key, Enhanced Monitoring, CloudWatch alarms and custom parameter groups are part of the Pro aurora-postgres module.

Usage

module "database" {
  source = "github.com/rafaelcarvajal05/terraform-stacks//modules/rds-postgres?ref=v0.1.0"

  name       = "my-app"
  vpc_id     = "vpc-0123456789abcdef0"
  subnet_ids = ["subnet-aaaa", "subnet-bbbb"] # private subnets, two AZs

  allowed_security_group_ids = [module.service.service_security_group_id]

  tags = { Environment = "prod" }
}

Read the credentials at runtime from the secret in master_user_secret_arn; the password never appears in Terraform code or tfvars.

Requirements

Name Version
terraform >= 1.6.0
aws >= 5.0

Providers

Name Version
aws >= 5.0

Modules

No modules.

Resources

Name Type
aws_db_instance.this resource
aws_db_subnet_group.this resource
aws_security_group.this resource
aws_vpc_security_group_ingress_rule.allowed resource

Inputs

Name Description Type Default Required
allowed_security_group_ids Security group IDs allowed to connect to the database on the PostgreSQL port. No other source gets access. list(string) n/a yes
name Name of the database. Used as prefix for the DB instance identifier, subnet group and security group. string n/a yes
subnet_ids Private subnet IDs (at least two AZs) for the DB subnet group. list(string) n/a yes
vpc_id ID of the VPC where the database security group is created. string n/a yes
allocated_storage Allocated storage in GiB (gp3). number 20 no
backup_retention_days Days to keep automated backups (0 disables them). number 7 no
database_name Name of the initial database created in the instance. string "app" no
deletion_protection Whether to block deletion of the instance. Disable it explicitly before destroying. bool true no
engine_version PostgreSQL engine version, for example 16 (latest minor of that major) or 16.4. string "16" no
instance_class RDS instance class, for example db.t4g.micro or db.m6g.large. string "db.t4g.micro" no
master_username Master username. The password is generated and stored by RDS in Secrets Manager (see output master_user_secret_arn). string "postgres_admin" no
multi_az Whether to deploy a standby in a second availability zone. bool false no
skip_final_snapshot Whether to skip the final snapshot on deletion. Keep false outside of throwaway environments. bool false no
tags Tags applied to all resources. A Name tag is added to each one. map(string) {} no

Outputs

Name Description
db_instance_identifier Identifier of the DB instance.
endpoint Connection endpoint of the instance in address:port form.
master_user_secret_arn ARN of the Secrets Manager secret holding the master user credentials, managed by RDS.
port Port the database listens on.
security_group_id ID of the database security group.