Building a Serverless API with ClaudiaJS

Serverless

Before I start discussing what I did, I wanted to take a second and define some basic terms I’m going to be using:

  • Serverless = any type of system that does not require maintaining a physical server
  • Lambdas = technology that lets you run code in the cloud without hosting infrastructure
  • CRUD = Create, Read, Update, Delete

ClaudiaJS

ClaudiaJS is a CLI that leverages the NPM package claudia as well as the NPM package claudia-api-builder to quickly (and easily) build out a serverless backend.

(source https://www.youtube.com/watch?v=Cuh_gtFX5gI)
  1. Once a user hits the gateway, a lambda is called that starts a node express instance
  2. The AWS event is transformed into a Node Express request
  3. The corresponding Request endpoint is called, ran, and the corresponding return value is fed back to the caller in the form of an HTTP response.

Where do I get started?

In order to start working with ClaudiaJS I recommend you walkthrough the tutorials here. For my project I created some basic endpoints that interacted with a DynamoDB table in AWS. To review my project, checkout my code on GitHub here.

var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder();

module.exports = api;

api.get('/hello', function () {
return 'hello world';
});
{
"lambda": {
"role": "amplify-links-executor",
"name": "amplify-links",
"region": "us-east-1"
},
"api": {
"id": "e4rrn42jmc",
"module": "app",
"url": "https://e4rrn42jmc.execute-api.us-east-1.amazonaws.com/latest"
}
}

Finished Product

When I was finished my basic application looked like the following:

var ApiBuilder = require('claudia-api-builder'),
AWS = require('aws-sdk'),
api = new ApiBuilder(),
documentClient = new AWS.DynamoDB.DocumentClient(),
dynamoDBTableName = 'amplify-links';

AWS.config.update({region: 'us-east-1'});

module.exports = api;

// hello world
api.get('/hello', function () {
return 'hello world';
});

// create
api.put('/api/create', function (request) {
'use strict';
try {
const params = {
TableName: dynamoDBTableName,
Item: {
username: request.body.username,
links: request.body.links
}
};
return documentClient.put(params).promise();
} catch (error) {
return error;
}
}, { success: {code: 200}, error: {code: 500} });

// read
api.get('/api/read/{username}', function (request) {
'use strict';
try {
const params = {
TableName: dynamoDBTableName,
Key: {
username: request.pathParams.username
}
};
return documentClient.get(params).promise();
} catch (error) {
return error;
}
});

// delete
api.delete('/api/delete/{username}', function (request) {
'use strict';
try {
const params = {
TableName: dynamoDBTableName,
Key: {
username: request.pathParams.username
}
};
return documentClient.delete(params).promise();
} catch (error) {
return error;
}
});
{ "Version": "2012-10-17", "Statement": [ { "Sid": "AmplifyLinksLambdaActions", "Effect": "Allow", "Action": [ "dynamodb:Get*", "dynamodb:Query", "dynamodb:Scan", "dynamodb:Delete*", "dynamodb:Update*", "dynamodb:PutItem" ], "Resource": "arn:aws:dynamodb:us-east-1:XXXX:table/amplify-links" } ] }

Closing Thoughts

I hope this post has given you an introduction to claudiajs, and potentially could help you build out serverlesss APIs. There is a lot of good documentation that goes with claudiajs, and I encourage you to read more.

--

--

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