AWS Dynamodb with same structure

In my current serverless.yml I have the following resource declaration:

resources:
  Resources:
    stocksTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.stocksTableName}
        AttributeDefinitions:
          -
            AttributeName: symbol
            AttributeType: S
          -
            AttributeName: dateiso
            AttributeType: S
          -
            AttributeName: monthIdx
            AttributeType: S
        KeySchema:
          -
            AttributeName: symbol
            KeyType: HASH
          -
            AttributeName: dateiso
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        GlobalSecondaryIndexes:
          - 
            IndexName: monthIdx
            KeySchema:
              - 
                AttributeName: monthIdx
                KeyType: HASH
              - 
                AttributeName: dateiso
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1

Now, I need to add a second table with exactly the same structure and indexes.

Do I need to duplicate the entire entry for the new resource?

Option1

resources:
  Resources:
    stocksTable:
      {declarations}
    stocks2Table:
      {declarations copied}

Or can I do something like this:

Option2:

resources:
  Resources:
    stocksTable:
    stocks2Table:
      {declarations}

But the TableName will be a problem with option 2.

resources:
  Resources:
    stocksTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.stocksTableName}

The simplest solution I can think of will look something like:

custom:
  commonTableProps: &commonTableProps
    AttributeDefinitions:
      -
        AttributeName: symbol
        AttributeType: S
      -
        AttributeName: dateiso
        AttributeType: S
      -
        AttributeName: monthIdx
        AttributeType: S
    KeySchema:
      -
        AttributeName: symbol
        KeyType: HASH
      -
        AttributeName: dateiso
        KeyType: RANGE
    ProvisionedThroughput:
      ReadCapacityUnits: 1
      WriteCapacityUnits: 1
    GlobalSecondaryIndexes:
      - 
        IndexName: monthIdx
        KeySchema:
          - 
            AttributeName: monthIdx
            KeyType: HASH
          - 
            AttributeName: dateiso
            KeyType: RANGE
        Projection:
          ProjectionType: ALL
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

resources:
  Resources:
    stocksTable:
      Type: AWS::DynamoDB::Table
      Properties:
        <<: *commonTableProps
        TableName: ${self:custom.stocksTableName}
    stocks2Table:
      Type: AWS::DynamoDB::Table
      Properties:
        <<: *commonTableProps
        TableName: ${self:custom.stocks2TableName}

Nice. I think it works.
Thanks!
Learnt about the new syntax :slight_smile: