VPC + PostgreSQL with AWS CDK
Infrastructure as code: a small, secure AWS network with a PostgreSQL database, defined in TypeScript with the AWS Cloud Development Kit (CDK).
VPC architecture
What it builds
ProjectStack: the network
A Virtual Private Cloud (VPC) across 2 Availability Zones Public subnets with a route to the Internet Gateway Private isolated subnets with no internet access
RDSStack: the database
PostgreSQL on Amazon RDS (Relational Database Service), placed in the private subnets Not publicly accessible, with encrypted storage Multi-AZ for high availability Automatic backups kept for 7 days Custom parameter group (logs slow queries over 1 second) Admin password generated by AWS and stored in Secrets Manager, never written in the code
The VPC is created in one stack and passed to the database stack, so network and database can be managed separately.
Why I built it
I come from architecture and BIM, where you design a building before you build it. Infrastructure as code works the same way: the whole environment is described in a file, reviewed, versioned in Git and rebuilt the same way every time.
What I learned Splitting infrastructure into stacks and passing resources between them Designing subnets: what should be public and what should stay private Security by default: private database, encryption, generated secrets Debugging subnet selection between stacks (see the commit history) How to run it bash cd project npm install npx cdk synth # generate the CloudFormation template npx cdk deploy --all npx cdk destroy --all # clean up afterwards to avoid costs
Requires an AWS account and configured AWS credentials. Note: Multi-AZ RDS is not free tier, so destroy the stacks after testing.
Tech
AWS CDK · TypeScript · Amazon VPC · Amazon RDS (PostgreSQL) · AWS Secrets Manager