paint-brush
Manage table resources in serverlessby@chris.feist
635 reads
635 reads

Manage table resources in serverless

by Chris FeistMarch 4th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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: <code class="markup--code markup--p-code"><a href="https://github.com/chris-feist/serverless-plugin-tables" target="_blank">serverless-plugin-tables</a></code>.

Company Mentioned

Mention Thumbnail
featured image - Manage table resources in serverless
Chris Feist HackerNoon profile picture

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 👏👏👏