Web backed RDS on AWS with CDK

RamblingDeveloper
4 min readJul 25, 2021

Just to show how to get a going with CDK deploying a web application backed by a RDS instance on AWS. This source code is only for demonstration only and would need to be prepared for production. Use at your own responsibility.

What it we are aiming for

Prerequisites
1 The code located at:
https://github.com/mkdirJava/cdkPlayground
git clone https://github.com/mkdirJava/cdkPlayground
GO TO RDS sub folder

2 An AWS account with a keypair, created and downloaded, details to create a key pair, PELASE REMEMBER THE NAME: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html#having-ec2-create-your-key-pair

3 Have nodejs installed, at least v14.12.0
https://nodejs.org/en/download/

4. Mysql workbench installed

5. Have set up you local for aws cli:
for install
https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

for config see
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html

6. Have CDK installed
npm install -g aws-cdk

The Actual deploy

1: Install the command line tool CDK
npm install -g cdk

2 Setup a AWS config
Run the following command and fill in the details from aws configure with details of the key pair created in the prerequisite.

3. Setup CDK and source code
In the source code

<root of project>/rds
run
npm install to install dependencies
cdk bootstrap, to setup bootstrapping to the aws region

<root of project>/rds/bin/playground.ts

new PlaygroundStack(app, 'PlaygroundStack', {rootPassword: 'password',rootUser: 'user',keyName: 'work',schemaName: 'test',...});

Edit the keyName to the name of your key pair.

4. From the root of the project/rds run
cdk deploy
This is reading the cdk configuration and using your local aws configuration to deploy the artifacts both AWS and web app code to a AWS region: specified in the aws configure command. This might take 10–20 mins. At the end you should see

output

5. Now you should be able to execute some commands to the EC2 instance, remember to use YOUR EC2 instance from the output above,
Create a

curl --location --request POST 'ec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/signup' \--header 'Content-Type: application/json' \--data-raw '{"name": "Pretender","email": "noWhere@home.com","post": [{"title": "article one","content": "This is why I am in this business"}]}'

list that user

curl --location --request GET 'ec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/users'

submit a post in draft

curl --location --request POST 'ec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/post' \
--header 'Content-Type: application/json' \
--data-raw '{
"authorEmail": "noWhere@home.com",
"title": "another show",
"content":"more content"
}'

list current feeds

ec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/feed

To publish a post and list it in the feed

curl --location --request PUT 'ec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/publish/1'//thenec2-35-176-186-187.eu-west-2.compute.amazonaws.com:8080/feed

Find out more options at: <root of project>/rds/lib/app/playground-app/src/index

6. You can use the Bastion instance to hook into the database,
Open up mysql, create a new connection and select “Standard TCP/IP over SSH”

From the output of step 4:
SSH Host name is the bastion server
SSH username is ec-2-user
SSH key file is the path of the key pair downloaded
mySQL Hostname is the RDS instance
user name and password is the user found in step 3
<root of project>/rds/bin/playground.ts

When you connect you should see a schema and hopfully tables and if you executed some commands, data!!!

To take down the AWS resource execute from project root /rds
cdk destroy

Conclusion

CDK is quite nice, it allows pragmatic infrastructure as code. Is it an different from likes of terraform ? This feels more programatic. I am sure there are intelisense for terraform, but on the whole I like it. Just a balancing act between vendor lock in, amount to learn and actually doing the task at hand. I do think terraform being cloud agnostic might be helpful, but then again instead of writing CDK I would be writing terraform with AWS libraries.

I hope this provides some insight to AWS CDK. I am hopeful to do more CDK projects. If there are any questions please feel free to drop me a line, mkdirjava@gmail.com. I am also working with people interested in taking on more AWS projects. Have a nice day

--

--