Create Managed Cassandra Database(AWS Keyspaces) using AWS CDK
Tags: aws, database
Published: 2021-07-09 10:02:25

### What is Cassandra?
Apache Cassandra is an open source NoSQL distributed database where it is popular because of its linear scalability, proven fault tolerance and high performance.

###Query Language - CQL
Cassandra provides the Cassandra Query Language (CQL), an SQL-like language, to create and update database schema and access data. CQL syntax is much similar to SQL but there are some limitations such as no joins, no aggregations etc.
Learn more about CQL: https://www.guru99.com/cassandra-query-language-cql-insert-update-delete-read-data.html

### How Cassandra stores its data?
* **Keyspace**: defines how a dataset is replicated, for example in which datacenters and how many copies. Keyspaces contain tables.
* **Table**: defines the typed schema for a collection of partitions. Cassandra tables have flexible addition of new columns to tables with zero downtime. Tables contain partitions, which contain partitions, which contain columns.
* **Partition**: defines the mandatory part of the primary key all rows in Cassandra must have. All performant queries supply the partition key in the query.
* **Row**: contains a collection of columns identified by a unique primary key made up of the partition key and optionally additional clustering keys.
* **Column**: A single datum with a type which belong to a row.

### Why Amazon Keyspaces introduced?
Amazon Keyspaces is a managed Apache Cassandra–compatible database service. With Amazon Keyspaces, you can run your Cassandra workloads on AWS using the same Cassandra application code and developer tools easily. Its a serverless service. Therefore it eliminates Server provisioning, patching, maintaining burdens and you just pay as you go. Amazon Keyspaces take care of automatically scaling tables up and down in response to application traffic.

#### More benefits of Amazon Keyspaces...
* Virtually unlimited throughput and storage
* Data is encrypted by default 
* Enables you to back up your table data continuously using 
  point-in-time recovery

### Why AWS CDK?
AWS CDK helps to provision Cloud infrastructure resources in AWS faster in your favorite language. So, I decided to use Python to provision infrastructure for this tutorial and its super easy.

### Open up your favorite IDE and Lets write some code to provision Amazon Keyspace resources


#### 1)create the CDK project in Python language
```
cdk init app --language python
```
You will get similar folder structure as shown below.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnup84d7jq6jndp55uef.png)

> Note:- cdk init uses the name of the project folder to name various elements of the project, including classes, subfolders, and files.

I ran cdk init command in a empty folder called `cassandra_cdk`

#### 2) Activate virtual environment

```
#For Linux enviroments
source .venv/bin/activate 
```
.venv folder is your python virtual environment directory
#### 3) Add dependencies to Setup.py and run following command to download all dependencies
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfxt8k0t8vor1sojmdau.png)
 
``` 
python -m pip install -r requirements.txt 
```
#### 4) Define Keyspace and Table resources in the code
https://github.com/chathra222/aws-managed-cassandra-cdk/blob/master/cassandra_cdk/cassandra_cdk_stack.py

#### 5) Make sure AWS Credentials are set.
##### Options 1: 
Set credentials in the AWS credentials profile file on your local system, located at:

`~/.aws/credentials` on Linux, macOS, or Unix

`C:\Users\USERNAME\.aws\credentials` on Windows
```
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key
```
##### Options 2: 
Set the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` environment variables.

To set these variables on Linux, macOS, or Unix, use :
```
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
```
To set these variables on Windows, use :
```
set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_key
```
##### Options 3:
For an EC2 instance, specify an IAM role and then give your EC2 instance access to that role.

#### 6) Bootstrapping - provisioning these initial resources such as S3 bucket for storing files and IAM roles that grant permissions needed to perform deployments.
`cdk bootstrap`

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmeb49c6bi84s5gdyzv1.png)
I have already bootstrapped.But if its your first time you will notice that it will create some initial resoruces.
#### 7) Generate CloudFormation Templates
`cdk synth`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmopzejarwwnfytzqvv6.png)
 
#### 8) To list the defined stacks in the code
`cdk list`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2unvhkybnm5qtgso2pvg.png)
 
#### 9) Provision AWS resources
` cdk deploy`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t54uybb56m89s5157h1t.png)

You will notice in AWS management console that Keyspace is successfully provisioned and table is created which we previously defined in CDK code.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of5mj8urg5mczi1mesbw.png)
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ksnobxdd5forgqenl00e.png)
 
#### 10) Setup Cassandra client/Run CQL queries to test it.
#####Option 1: Use CQL editor in AWS Management console
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/prx0ddev4bghl1ceoj7b.png)
But this may incur charges.
#####Option 2: Use cqlsh
cqlsh is usually bundled with Cassandra. To make things easier, i will use cassandra:3.11.7 docker image to run cql queries through cqlsh utility.

Pull the docker image
`docker pull cassandra:3.11.7`
Create a container from the image and start bash,so you will be able to run commands in it.
`docker run -it <Image id> /bin/bash`

Amazon Keyspaces only accepts secure connections using Transport Layer Security (TLS).Therefore to connect using SSL/TLS,
Download the Starfield digital certificate:
`curl https://certs.secureserver.net/repository/sf-class2-root.crt -O`
Note down the cerfile path.

export SSL_CERTFILE environment variable.
`export SSL_CERTFILE=<cert file path>`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xekyvfxglb1flg8ot6m.png)
 
Run following command to connect to Cassandra database.

Generate AWS Keyspace credentials for your IAM User:
Follow this guide to generate it.
[How to Generate AWS Keyspace credentials for your IAM User](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_keyspaces.html?icmpid=docs_iam_console)

From this step, you will obtain username and password for Keyspace.

List of service endpoints for Keyspace Available here.Choose the correct Service Endpoint based on the region.
[List of service endpoints for Keyspace](https://docs.aws.amazon.com/keyspaces/latest/devguide/programmatic.endpoints.html)


```cqlsh <keyspace service endpoint> 9142 -u "<generated-keyspace-useranme>" -p "<generated-keyspace-password>" --ssl```

After you execute this command, you will prompt to cqlsh command line shell to execute query on Keyspace(Cassandra) Database.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mn4eplcfhxo8koxgn5bv.png)
 
####To destroy the resources, You can run..
`cdk destroy`

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/225sic6ijfbpd5f16wcm.png)

Github repository link: https://github.com/chathra222/aws-managed-cassandra-cdk