database
View SourceRDS 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:
# View database instance detailsaws rds describe-db-instances --db-instance-identifier your-db-identifier
# Create a manual snapshotaws rds create-db-snapshot --db-instance-identifier your-db-identifier --db-snapshot-identifier my-snapshot-$(date +%Y%m%d)
# View database logsaws rds describe-db-log-files --db-instance-identifier your-db-identifier
Direct Connection:
# Connect using psql (PostgreSQL)psql "postgresql://root:password@your-db-endpoint:5432/main?sslmode=require"
# Or using environment variablesexport PGHOST=your-db-endpointexport PGPORT=5432export PGDATABASE=mainexport PGUSER=rootexport PGPASSWORD=your-passwordpsql
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();
Related Components
Section titled “Related Components”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$24.82/month) ordb.t4g.small
(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.
database
Section titled “database”Creates an RDS database instance with security group, subnet group, and parameter group.
function database(ctx: Context, args: DatabaseArgs): DatabaseOutput
Parameters
Section titled “Parameters”ctx
(Context
) - The context for resource naming and taggingargs
(DatabaseArgs
) - Configuration arguments for the database
Returns
Section titled “Returns”- (
DatabaseOutput
) - Creates an RDS database instance with security group, subnet group, and parameter group.
Functions
Section titled “Functions”databaseToIds
Section titled “databaseToIds”Converts a DatabaseOutput to a structure containing just the instance ID and URL.
function databaseToIds(database: DatabaseOutput): { instance: Output<string>; url: Output<string> }
Parameters
Section titled “Parameters”database
(DatabaseOutput
) - The database output to convert
Returns
Section titled “Returns”- (
{ instance: Output<string>; url: Output<string> }
) - Converts a DatabaseOutput to a structure containing just the instance ID and URL.
Interfaces
Section titled “Interfaces”DatabaseArgs
Section titled “DatabaseArgs”Configuration arguments for creating an RDS database instance.
Properties
Section titled “Properties”availabilityZone?
(Input<string>
) - Specific availability zone for the database instanceengine?
(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 databasenoDeletionProtection?
(boolean
) - Whether to disable deletion protectionnoPrefix?
(boolean
) - Whether to skip adding a prefix to the resource namepassword?
(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 databaseusername?
(Input<string>
) - Master username for the database (defaults to “root”)version?
(Input<string>
) - Database engine version (defaults to “17” for postgres)
DatabaseOutput
Section titled “DatabaseOutput”Output from creating a database, containing the instance and connection URL.
Properties
Section titled “Properties”instance
(Instance
) - The RDS instance resourceurl
(Output<string>
) - Connection URL for the database