使用CloudFormation为现有dynamoDB表创建二级全局索引

时间:2019-11-01 00:51:41

标签: amazon-dynamodb amazon-cloudformation secondary-indexes

DynamoDB表已经创建并在生产中运行。根据当前用例,计划添加新的二级全局索引。如果可以使用CloudFormation脚本更新DynamoDB表,则可以通过AWS开发工具包实现。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用以下内容创建新的或覆盖当前的CloudFormation脚本

Resources
 DDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: YOUR_EXISTING_TABLE_NAME
      AttributeDefinitions: #List out all existing cols here
        -
          AttributeName: "ColX" # hash key
          AttributeType: "S"
        -
          AttributeName: "ColY" # range key
          AttributeType: "S"
        -
          AttributeName: "ColZ" # used for your Global Secondary Index 
          AttributeType: "S"

      KeySchema: # List out your main Hash & Range Key
        -
          AttributeName: "ColX"
          KeyType: "HASH"
        -
          AttributeName: "ColY"
          KeyType: "RANGE"

      GlobalSecondaryIndexes: #  new Global Secondary Index
      - IndexName: INDEX_NAME
        KeySchema:
        - AttributeName: ColZ #different than your main table Hash Key
          KeyType: HASH
        Projection:
          ProjectionType: ALL