serverless-plugin-dynamodb

1.2.0 • Public • Published

Serverless DynamoDB

npm

A serverless plugin to easily create DynamoDB tables from configurations files. This plugin will also edit the Lambda Role to allow any operations on created tables.

Usage

Installation

$ npm install serverless-plugin-dynamodb --save-dev

or using yarn

$ yarn add serverless-plugin-dynamodb

Configuration

plugins:
  - serverless-plugin-dynamodb
 
custom:
  tables:
    todo:
      name: ${self:service}-${self:provider.stage}-ToDo # Table Name 
      primaryKey: # Primary Key configurations,  default type: S 
        name: id
        type: 'S'
      rangeKey: # Range Key configurations, optional, default type: S 
        name: date
        type: 'S'
      throughput: # ProvisionedThroughput configuration, default: read 1 and write 1 
        read: 1
        write: 1

TTL

To add an TimeToLive specification on table resource set the ttl table configuration:

custom:
  tables:
    todo:
      name: ${self:service}-${self:provider.stage}-ToDo
      primaryKey:
        name: id
        type: 'S'
      ttl: 
        attribute: ttl
        enabled: true # Optional, true by default

Policy

By default the plugin edit the common lambda function policy to allow any function to do any data operations

{
  'Effect': 'Allow',
  'Action': [
    'dynamodb:*'
  ],
  'Resource': <table resource>
}

to disable this behaviour set the config skipTablePolicy to true

custom:
  skipTablePolicy: true

Resource names

Table resource will be create using the table configuratin key name in camel case prepending "Table" before the name, for example:

custom:
  tables:
    todo:
      name: ${self:service}-${self:provider.stage}-ToDo
      primaryKey:
        name: id
        type: 'S'

will create a AWS::DynamoDB::Table resource with key name TableTodo, so you can reference it in this way:

iamRoleStatements:
  - Effect: Allow
    Action:
      - dynamodb:PutItem
    Resource: 
      - "Fn::GetAtt": ["TableTodo", "Arn"]

TODO

  • Create tables
  • Automatic create IAM Role
  • Support TTL attributes
  • Support Secondaries Index
  • Support Global Secondaries Index
  • Support triggers

Install

npm i serverless-plugin-dynamodb

DownloadsWeekly Downloads

4

Version

1.2.0

License

MIT

Unpacked Size

6.72 kB

Total Files

4

Last publish

Collaborators

  • daaru