Skip to content

Defining DynamoDB Tables

Dennis Vriend edited this page Dec 25, 2017 · 2 revisions

Defining DynamoDB Tables

DynamoDB Tables are resources. sbt-sam uses Typesafe/Lightbend Config to configure AWS resources like tables.

Table resource configuration

In the base directory, of sbt-sam projects, a folder conf must be present, containing a file sam.conf that defines AWS resources. sbt-sam plugin interprets the sam.conf and indexes the resources, in effect it 'knows' about AWS resources.

An example sam.conf that defines AWS::DynamoDB::Table resources:

dynamodb {
   // a simple table that has only a hash key 'id'
   People {
    name = people
    hash-key = {
      name = id
      type = S
    }
    rcu = 1
    wcu = 1
  }
  
   // a simple table that has only a hash key 'id', and 
   // has DynamoDB streams enabled with KEYS_ONLY
   People {
    name = people
    hash-key = {
      name = id
      type = S
    }
    stream = KEYS_ONLY // only the key attributes of the modified item.
//    stream = NEW_IMAGE // the entire item, as it appears after it was modified.
//    stream = OLD_IMAGE // the entire item, as it appeared before it was modified.
//    stream = NEW_AND_OLD_IMAGES // both the new and the old images of the item.

    rcu = 1
    wcu = 1
  }
  
    // a table that has has two keys, a hash 'name' and range key 'age' 
     People {
      name = people
      hash-key = {
        name = id
        type = S
      }
      range-key = {
        name = name
        type = N
      }
      rcu = 1
      wcu = 1
    }
    
  // a table that has has two keys, a hash 'name' and range key 'age' and
  // a global secondary index 
  People {
    name = people
    hash-key = {
      name = name
      type = S
    }
    range-key = {
      name = name
      type = N
    }

    global-secondary-indexes {
      people_id {
        hash-key = {
          name = id
          type = S
        }

        projection-type = ALL // All of the table attributes are projected into the index
//        projection-type = INCLUDE // Only the specified table attributes are projected into the index. The list of projected attributes are in NonKeyAttributes.
//        projection-type = KEYS_ONLY // Only the index and primary keys are projected into the index

        rcu = 1
        wcu = 1
      }
    }
    rcu = 1
    wcu = 1
  }

Defining multiple tables

To define multiple tables, just put them in the same dynamodb block, and give them a new name:

dynamodb {
   People {
    name = people
    hash-key = {
      name = id
      type = S
    }
    rcu = 1
    wcu = 1
  }
  Addresses {
      name = addresses
      hash-key = {
        name = id
        type = S
      }
      rcu = 1
      wcu = 1
  }
  Orders {
    name = orders
     hash-key = {
        name = id
        type = S
     }
     rcu = 1
     wcu = 1
  }

Table names

Because resources in a single AWS account has a flat namespace, a design decision has been made, in order to easily develop and deploy resources without having resource name clash. This means that sbt-sam knows about the name of a resource in the project, like eg. the people table, but will deploy the resource with a managed logical name with the following pattern:

[project-name][stage][resource-name]

For the table people, this means that the resource name will become:

sam-seed-dnvriend-people