Skip to content

RDS databases in AWS provide managed relational database instances. Stackattack creates PostgreSQL databases with secure networking, automatic backups, encryption at rest, and SSL connections enabled by default.

import * as saws from "@stackattack/aws";
const ctx = saws.context();
const network = saws.vpc(ctx);
const db = saws.database(ctx, {
network: network.network("private")
});
export const dbUrl = db.url;

After deploying a database, you can connect to it using:

AWS CLI:

Terminal window
# View database instance details
aws rds describe-db-instances --db-instance-identifier your-db-identifier
# Create a manual snapshot
aws rds create-db-snapshot --db-instance-identifier your-db-identifier --db-snapshot-identifier my-snapshot-$(date +%Y%m%d)
# View database logs
aws rds describe-db-log-files --db-instance-identifier your-db-identifier

Direct Connection:

Terminal window
# Connect using psql (PostgreSQL)
psql "postgresql://root:password@your-db-endpoint:5432/main?sslmode=require"
# Or using environment variables
export PGHOST=your-db-endpoint
export PGPORT=5432
export PGDATABASE=main
export PGUSER=root
export PGPASSWORD=your-password
psql

Application Code:

import { Client } from "pg";
const client = new Client({
connectionString: "postgresql://root:password@your-db-endpoint:5432/main?sslmode=require"
});
await client.connect();
const result = await client.query("SELECT NOW()");
await client.end();

Databases work together with other Stackattack components:

  • vpc - Provides secure private networking for database access

RDS costs are fixed monthly charges based on instance type plus usage-based storage:

  • Instance costs - The default db.t4g.micro costs $12.41/month if running 24/7. Larger instances like db.t4g.small ($24.82/month) or db.r7g.large (~$158.40/month) provide more CPU and memory.

  • Storage costs - General Purpose SSD storage is ~$0.115/GB/month. The default 30GB allocation costs ~$3.45/month. Storage automatically scales as your database grows.

  • Backup storage - Stackattack enables 7-day backup retention. Backups within your allocated storage are free; additional backup storage is ~$0.095/GB/month.

  • Data transfer - Minimal costs for database connections within the same VPC (typically free). Cross-region replication incurs standard AWS data transfer rates.

  • Snapshots - Manual snapshots cost the same as backup storage (~$0.095/GB/month) and persist until manually deleted.

Cost optimization strategies:

  • Use db.t4g.micro for development/small workloads
  • Monitor storage growth and set CloudWatch alerts for unexpected increases
  • Consider Aurora Serverless for intermittent workloads that can pause/resume
  • Delete old manual snapshots regularly
  • Use Multi-AZ only for production workloads requiring high availability

See RDS Pricing for current rates.

Creates an RDS database instance with security group, subnet group, and parameter group.

function database(ctx: Context, args: DatabaseArgs): DatabaseOutput
  • ctx (Context) - The context for resource naming and tagging
  • args (DatabaseArgs) - Configuration arguments for the database
  • (DatabaseOutput) - Creates an RDS database instance with security group, subnet group, and parameter group.

Converts a DatabaseOutput to a structure containing just the instance ID and URL.

function databaseToIds(database: DatabaseOutput): { instance: Output<string>; url: Output<string> }
  • ({ instance: Output<string>; url: Output<string> }) - Converts a DatabaseOutput to a structure containing just the instance ID and URL.

Configuration arguments for creating an RDS database instance.

  • availabilityZone? (Input<string>) - Specific availability zone for the database instance
  • engine? (Input<"postgres">) - Database engine type (currently only postgres is supported)
  • instanceType? (Input<string>) - RDS instance type (defaults to “db.t4g.micro”)
  • name? (Input<string>) - Name of the database to create (defaults to “main”)
  • network (Network) - The network configuration (VPC and subnets) for the database
  • noDeletionProtection? (boolean) - Whether to disable deletion protection
  • noPrefix? (boolean) - Whether to skip adding a prefix to the resource name
  • password? (Input<string>) - Master password for the database (auto-generated if not provided)
  • port? (Input<number>) - Port number for database connections (defaults to 5432)
  • sourceSecurityGroupId? (Input<string>) - Security group ID that should be allowed to access the database
  • username? (Input<string>) - Master username for the database (defaults to “root”)
  • version? (Input<string>) - Database engine version (defaults to “17” for postgres)

Output from creating a database, containing the instance and connection URL.

  • instance (Instance) - The RDS instance resource
  • url (Output<string>) - Connection URL for the database