Manage table resources in serverless

Written by chris.feist | Published 2019/03/04
Tech Story Tags: aws | serverless | dynamodb | database | cloudformation

TLDRvia the TL;DR App

Photo by Blake Parkinson on Unsplash

Serverless is a great platform for managing your serverless infrastructure. It significantly reduces boilerplate code for setting up a service and defining functions. It even allows defining platform specific resources for things that aren’t supported out of the box. The problem is that service providers can require complex templates to support basic infrastructure. For example, AWS CloudFormation requires defining DynamoDB keys under two different fields for a single table. Therefore I created a plugin that would simplify table definitions, reduce boilerplate code and have smart defaults: [serverless-plugin-tables](https://github.com/chris-feist/serverless-plugin-tables).

Let’s see an example

Below are two example servlerless.yml definitions for a table described in the DynamoDB documentation. The first example is using the standard CloudFormation template and the second is using serverless-plugin-tables.

As you can see, the serverless-plugin-tables implementation has significantly reduced boilerplate (1/3 the lines). It also has sane defaults, using On-Demand Billing Mode and strings as the default data types. Despite being so simple to define, it is fully customizable, supporting every CloudFormation option at the time of release. See the complete example in the Readme.

Sounds great! Let’s incorporate it!

First, add the dependency to your existing Serverless project:

yarn add -D serverless-plugin-tables

Now add the plugin to your service file:

# serverless.yml

plugins:

  • serverless-plugin-tables

Now define the `resources` section if you don’t already have one, add a tables property, and start creating tables 🤩

# serverless.yml

resources:tables:# Start defining tables here

Here is another table example for Users, with a little more configuration:

For even more configuration options, check out the GitHub Readme, which describes each option along with links to the AWS documentation.

What about other providers and databases?

I started off the project by implementing it for AWS DynamoDB. However, I designed it so it can easily expand to support other service providers and database platforms. So if there is another platform you want to be supported, comment here or submit an issue on GitHub.

Let me know what you think. Leave a comment below and give me some claps. The longer you hold the claps button, the more I get 👏👏👏


Published by HackerNoon on 2019/03/04