Connecting a Node.js client to the new serverless Cassandra offering by AWS

A quick tutorial to get you started with connecting the cassandra-driver node module to the new Amazon Managed Cassandra Service

Tom Kowalski
Dec 21, 2019 · 3 min read
Photo by israel palacio on Unsplash

There were a lot of serverless offerings that were released during the AWS re:Invent conference. If you were like me you might have seen the new Cassandra offering and assumed that it was just “managed servers” like the ElasticSearch offering. However, the new Managed Cassandra Service is a truly serverless offering. It scales from zero and you only pay for the reads/writes and storage that you use.

I have not worked with Cassandra much before, but now that it is serverless it was time to give it another try. The only issue was that there was no good documentation for connecting from a Node.js(javascript/typescript) app. After a couple pain staking hours I was able to get a test connection up and running. So, I decided to make this little article to help others navigate through some of the gotchas of connecting.

Prerequisites

A few things you will need first:

  1. AWS Account — obvi
  2. IAM user — This user needs to be able to access the Cassandra Service
  3. Node.js — Preferably the latest, but not necessary
  4. Initial keyspace(optional) — This tutorial assumes that you have walked through the getting started guide and created the keyspace with data

Setup

1. Create credentials

This can be done via the cli using the following command and replacing the user-name with your IAM user:

aws iam create-service-specific-credential \ — user-name alice \ — service-name cassandra.amazonaws.com

Or, via the console. By first selecting your IAM user. Then, clicking on the “Security credentials” tab. Then, clicking “Generate credentials” under the “Credentials for Amazon Managed Apache Cassandra Service (MCS)” section.

Take note of the ServiceUserName and ServicePassword.

2. Download the Amazon digital certificate

You need to connect to the Amazon Managed Cassandra Service with TLS/SSL. In order to do this you need the Amazon digital certificate. It can be downloaded to your working directory using the following curl command:

curl https://www.amazontrust.com/repository/AmazonRootCA1.pem -O

3. Create a Node.js app file

Create a file in your working directory and call it app.js. Then, copy the following code into it and save the file.

var fs = require('fs');
var cassandra = require('cassandra-driver');
var authProvider = new cassandra.auth.PlainTextAuthProvider('USERNAME', 'PASSWORD');
var contactPoints = ['cassandra.us-east-1.amazonaws.com:9142'];
var sslOptions = {
cert: fs.readFileSync('AmazonRootCA1.pem'),
host: 'cassandra.us-east-1.amazonaws.com',
rejectUnauthorized: true
};
var client = new cassandra.Client(
{
contactPoints: contactPoints,
authProvider: authProvider,
localDataCenter: 'dc1',
keyspace:'tutorialkeyspace',
sslOptions: sslOptions

}
);
var query = 'SELECT * FROM tutorialtable';client.execute(query, [], (err, result) => {
if(err) {
console.log('err: $' + JSON.stringify(err))
} else {
console.log('result: $' + JSON.stringify(result))
}
process.exit();
});

4. Update with your info

Replace the USERNAME and PASSWORD with the generated ones from step 1. Also, if you are using the service in a different region other than us-east-1, then replace it in the code with the region that you are using.

Then, save the file again.

5. Run it

Install the cassandra-driver using the following command:

npm install cassandra-driver

Run the code with the following command:

node app.js

The output should look like this:

result: ${"info":{"queriedHost":"3.83.170.142:9142","triedHosts":{"3.83.170.142:9142":null},"speculativeExecutions":0,"achievedConsistency":10,"isSchemaInAgreement":true},"rows":[{"email":"john@example.com","age":21,"name":"John"}],"rowLength":1,"columns":[{"name":"email","type":{"code":13,"type":null}},{"name":"age","type":{"code":9,"type":null}},{"name":"name","type":{"code":13,"type":null}}],"pageState":null}

Conclusion

Obviously this is not production ready code, but it should enable you to get up and running and testing out the new Cassandra service with Node.js. If you have any questions or would like to talk all things serverless you can find me on Twitter.

Tom Kowalski

Written by

Technologist | Serverless Enthusiast | Father | Chief Architect @DaySmart

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade