CDK Serverless Aurora

In this article, we will share with you how to create AWS(Amazon Web Services) Serverless Aurora instance using CDK (Cloud Development Kit).

CDK Serverless Aurora

What is CDK?

The AWS Cloud Development Kit is a framework that can be used by developers to provision and manipulate AWS resources programmatically.

There are many tools like Terraform and CloudFormation that exist to give you programmatic access to your application’s resources, many are based in a series of formatted configuration files. But for CDK you can write code using

  1. TypeScript
  2. JavaScript
  3. Python
  4. Java
  5. Go & Others programming languages

Why CDK?

# Create Manual infrastructure using AWS UI console

As a learner, you can create manual infrastructure using the AWS console as shown in the following fig.

Create Manual infrastructure using AWS UI console

If you want to create 3 different environments Dev, Stage, and Prod using manually it’s a time-consuming process

You have to go to different screens on AWS console for Dev then Stage, and Prod again and again

# Create infrastructure using AWS CDK

Write code once and create multiple environments with single code

Create infrastructure using AWS CDK

Install AWS Client

Make sure you have installed “AWS Client” on your system if not you can use the following command to install

brew install awscli

once you have installed the AWS client to verify please type the following command on the terminal

aws configure

Then you will see the following prompt then input your AWS Access Key ID

for example: 
AWS Access Key ID [None]: AKIAI44QH8DHBEXAMPLE

Once you hit enter/return then you will see the following prompt then you need to input AWS Secret Access Key

for example: 
Secret Access Key [None]: je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY

Once you hit enter/return then you will see the following prompt then you need to input Default region name

for example:
Default region name [None]: ap-southeast-1

Once you hit enter/return then you will see the following prompt then you need to input Default output format

for example:
Default output format [None]: json

After that, you can .aws directory under your username as shown in the following screenshot

Note: .aws is a hidden directory

If you open config file you will see the following lines

If you open credentials file you will see the following lines

We are done with the AWS Client Setup 👏👏👏

IAM User Role Permissions

To create Aurora Serverless Instance you need to add this following permission to your AWS user account.

Install the AWS CDK (Cloud Development Kit)

Before installation of AWS CDK make sure you have installed Node.JS on your system. We already published the story to install Node.js. You can follow the following link for Node.JS Installation

Install the AWS CDK Toolkit globally using the following Node Package Manager command.

npm install -g aws-cdk

Run the following command to verify the correct installation and print the version number of the AWS CDK.

cdk --version

Create a CDK App

Create a new directory aurora-cdk using the following command

mkdir aurora-cdk && cd aurora-cdk

from the above command, we have created the aurora-cdk directory and we will create the CDK App using TypeScript

You need to initialize the app using the cdk init command, specifying the desired template (“app”) and programming language.

Example:

cdk init app --language typescript

Once the app successfully created it will show you the All Done! as shown below

Now you will see there are a file aurora-cdk.ts under bin directory

Above file contain an instance of the AuroraCdkStack the implementation of that class is under the lib folder as shown below

Before creating a new Instance & VPC we will do some refactoring and Renaming.

bin -> aurora-cdk.ts

We renamed from (AuroraCdkStack)

import { AuroraCdkStack } from '../lib/aurora-cdk-stack';

To (AuroraSlsStack)

import { AuroraSlsStack } from '../lib/aurora-cdk-stack';

We refactored to the following code.

You need to update test file as well aurora-cdk.test.ts

Creating a VPC and Aurora Serverless Instance using CDK

First, before creating the Aurora instance we will create a VPC.

We need to add aws-ec2 package dependency. try to run the following command

npm install @aws-cdk/aws-ec2

and for Aurora, Serverless Instance run the following command

npm install @aws-cdk/aws-rds

Here is the final version of the following file aurora-cdk-stack.ts

lib -> aurora-cdk-stack.ts

Build the App

Try to run the following command.

npm run build

This will create compiled Javascript files as shown in the screenshots.

Then try to run following command

cdk synth

You will see it will return you the CloudFormation Template as shown below

Now, it’s time to Deploy the CDK Stack 🚀🚀🚀🚀

Try to run following command

cdk deploy

After successful deployment, it will show you the Output as shown below

You can check the Stack under Cloudformation as shown below

You can also verify the Aurora Serverless Instance under RDS databases as shown below.

Now, it’s time to Destroy the CDK Stack

Try to run following command

cdk destroy

It will prompt you message as shown below

Are you sure you want to delete: auroraslsdevAuroraSlsStackB62126AC (y/n)?

then input y then hit enter/return

Once it’s destroyed you will see the following message

Github

Happy Coding 👏👏👏👏👏👏

Written by

We teach & develop for startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store