A Step-by-Step Guide.
Deploy a PostgreSQL RDS Instance with AWS CDK
Part 1 of the series of CDK Infrastructure As Code (IAC)
--
Embarking on a project with AWS CDK to query a PostgreSQL RDS database using Athena? You might find, like I did, that the journey is not as straightforward as it seems, especially with the existing gaps in AWS documentation.
But worry not! I’ve simplified the process for you in this hands-on guide. Here, I will walk you through creating a small and isolated PostgreSQL RDS instance using AWS CDK. It’s a perfect setup for experiments and temporary use cases, with easy clean-up steps to avoid any post-experiment hassle.
So, if you’re looking to dip your toes into using AWS CDK for PostgreSQL RDS, this guide is your starting point. Let’s dive in!
Step 1:
Ensure AWS CLI and CDK command lines are installed and your environment (node, npm), and AWS credentials are set up. We will not cover those here!
Step 2:
Initiate a CDK project:
cd ~/Desktop # creating the project in your desktop!
mkdir psql-cdk
cd psql-cdk
cdk init app --language=typescript
Step 3:
Here, we will finally create all the resources that include a VPC ( so we create an isolated system and can easily clean everything up after the fact, otherwise you could use the default VPC as well ); we also create any needed roles and policies and security groups for the VPC and subnets, we also create a record in the Secrets Manager which will let us to access and connect the database later on safely and securely without having to hardcode or expose the secrets.
I provided comments for all the sections explaining in the code what every part of the code does:
import * as cdk from 'aws-cdk-lib';
import {Construct} from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import {SubnetType} from 'aws-cdk-lib/aws-ec2';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
export class CdkRdsPgdslStack extends cdk.Stack {
constructor(scope: Construct…