Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created September 15, 2017 05:39
Show Gist options
  • Save DavidWells/c7df5df9c3e5039ee8c7c888aece2dd5 to your computer and use it in GitHub Desktop.
Save DavidWells/c7df5df9c3e5039ee8c7c888aece2dd5 to your computer and use it in GitHub Desktop.
DynamoDB custom index serverless.yml example
service: service-name
provider:
name: aws
runtime: nodejs6.10
functions:
myfunc:
handler: handler.myfunc
events:
- http:
path: myfunc
method: post
cors: true
resources:
Resources:
myDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: Album
AttributeType: S
- AttributeName: Artist
AttributeType: S
- AttributeName: Sales
AttributeType: N
KeySchema:
- AttributeName: Album
KeyType: HASH
- AttributeName: Artist
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
TableName: myTableName
GlobalSecondaryIndexes:
- IndexName: myGSI
KeySchema:
- AttributeName: Sales
KeyType: HASH
- AttributeName: Artist
KeyType: RANGE
Projection:
NonKeyAttributes:
- Album
ProjectionType: INCLUDE
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
LocalSecondaryIndexes:
- IndexName: myLSI
KeySchema:
- AttributeName: Album
KeyType: HASH
- AttributeName: Sales
KeyType: RANGE
Projection:
NonKeyAttributes:
- Artist
ProjectionType: INCLUDE
mySecondDDBTable:
Type: AWS::DynamoDB::Table
DependsOn: myDynamoDBTable
Properties:
AttributeDefinitions:
- AttributeName: ArtistId
AttributeType: S
- AttributeName: Concert
AttributeType: S
- AttributeName: TicketSales
AttributeType: S
KeySchema:
- AttributeName: ArtistId
KeyType: HASH
- AttributeName: Concert
KeyType: RANGE
TableName: myTableName2
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
GlobalSecondaryIndexes:
- IndexName: myGSI
KeySchema:
- AttributeName: TicketSales
KeyType: HASH
Projection:
ProjectionType: KEYS_ONLY
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
@Ta-m
Copy link

Ta-m commented Aug 30, 2019

Im trying to create the table only with global secondary indexes.
With this yaml example I get "The number of attributes in key schema must match the number of attributesdefined in attribute definitions."
And I can't create indexes for attributes not present in the table! So this doesn t work...
How should I do it?

@AuctionEdgeCurtis
Copy link

I'm getting a similar error. I want a global index and no secondary key. I can create the index in the console, but serverless throws an error: "Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes."

@tmvnkr
Copy link

tmvnkr commented Sep 17, 2019

You have to define the attributes used in your Primary/Secondary keys, GSIs, and LSIs in the AttributeDefinitions. In addition, you need to remove any attributes in the AttributeDefinitions that you do not use in the Primary/Secondary keys, LSIs, and GSIs.

@AuctionEdgeCurtis
Copy link

My problem was you can not have two - IndexName sections in the GlobalSecondaryIndexes section due to a limitation in CloudFormation. Only one GSI action can be run at a time.

If you want more than one GSI, you have to add them one at a time and wait for them to complete by watching the console. Also make sure to add a new section and not delete an existing section or CF will think you're trying to remove and add an index, which is two operations, and it will fail.

@ticatwolves
Copy link

Hi can this be possible in dynamodb
I am trying to create a table with platform, profileid, and username attributes. it is giving error that

ValidationException: Hash Key not specified in Attribute Definitions. Type unknown.: DynamoDB - Error

I want my schema like that

ProfileTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: ProfileTable
    AttributeDefinitions:
      -
        AttributeName: platform
        AttributeType: S
      -
        AttributeName: profileid
        AttributeType: S
      -
        AttributeName: username
        AttributeType: S
    KeySchema:
      -
        AttributeName: platform
        KeyType: RANGE
      -
        AttributeName: profileid__username
        KeyType: HASH
    ProvisionedThroughput:
      ReadCapacityUnits: ${self:custom.db.rcu}
      WriteCapacityUnits: ${self:custom.db.wcu}

@AuctionEdgeCurtis
Copy link

Hi can this be possible in dynamodb
I am trying to create a table with platform, profileid, and username attributes. it is giving error that

ValidationException: Hash Key not specified in Attribute Definitions. Type unknown.: DynamoDB - Error

I want my schema like that

ProfileTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: ProfileTable
    AttributeDefinitions:
      -
        AttributeName: platform
        AttributeType: S
      -
        AttributeName: profileid
        AttributeType: S
      -
        AttributeName: username
        AttributeType: S
    KeySchema:
      -
        AttributeName: platform
        KeyType: RANGE
      -
        AttributeName: profileid__username
        KeyType: HASH
    ProvisionedThroughput:
      ReadCapacityUnits: ${self:custom.db.rcu}
      WriteCapacityUnits: ${self:custom.db.wcu}

Your HASH key needs to be an Attribute as well, so add it like you added platform.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment