DynamoDB resource in serverless.yml

I looked through the forum and didn’t see a topic on this. It appears that if you have a dynamoDB resource where you are creating tables and indexes, you can deploy once. If you need to make an update, you need to first remove and then deploy. This is fine in dev (I guess) but wouldn’t work in prod. So,

  1. Is there a way to deploy changes (adding new functions and api endpoints) without first removing everything, in this scenario?

  2. If not, what is the recommended way to deploy updates (adding new functions and api endpoints) when the dynamoDB tables are already created and filled with data?

  3. What is the recommended way to modify dynamoDB tables if they have already been deployed and filled with data?

You can definitely deploy new functions and API endpoints without needing to remove everything first. You just need to run sls deploy.

Exactly how are you modifying the DynamoDB tables?

Here is the resources section in my serverless.yml file (slightly modified field names, but essentially the same):

usersTable:
  Type: AWS::DynamoDB::Table
  Properties:
    TableName: Users
    AttributeDefinitions:
      - AttributeName: user_id
        AttributeType: S
      - AttributeName: user_username
        AttributeType: S
      - AttributeName: user_password
        AttributeType: S
      - AttributeName: user_organization_id
        AttributeType: S
    KeySchema:
      - AttributeName: user_id
        KeyType: HASH
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1
    GlobalSecondaryIndexes:
      - IndexName: UserAuthIndex
        KeySchema:
          - AttributeName: user_username
            KeyType: HASH
          - AttributeName: user_password
            KeyType: RANGE
        Projection:
          NonKeyAttributes:
            - user_id
            - user_organization_id
          ProjectionType: INCLUDE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
      - IndexName: UserGroupIndex
        KeySchema:
          - AttributeName: user_organization_id
            KeyType: HASH
        Projection:
          ProjectionType: ALL
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

Before I put in the GSI stuff, sls deploy would fail with “table already exists”. Now with the GSI stuff, it fails saying “GSI already exists”. I am not a CF expert (or fan), so I am sure there is something I am doing wrong.

1 Like

well, I am not sure what to say. I just tried it again, and it worked. I changed nothing in serverless.yml for the resources, but I added a new function with api endpoint and it worked without first removing. I must have tried 4 or 5 times yesterday and it never worked.

Trying to create two dynamoDB tables under resources in serverless.yml. I get an error stating Cannot read property ‘LogicalResourceId’ of null. Any idea what might be the problem ?