【问题标题】:Create secondary global index for existing dynamoDB table using CloudFormation使用 CloudFormation 为现有 dynamoDB 表创建二级全局索引
【发布时间】:2020-02-27 09:00:24
【问题描述】:

DynamoDB 表已创建并在生产中运行。根据当前用例,计划添加新的二级全局索引。这可以通过 AWS SDK 实现,是否可以使用 CloudFormation 脚本更新 DynamoDB 表。

任何帮助将不胜感激。

【问题讨论】:

  • 您不应该将 cloudformation 用于此类事情。如果表是首先使用 CF 创建的,那么您可以修改资源并再次部署它。但 CF 并不意味着修改与您的堆栈无关的现有资源。
  • 我可以使用 CF 而不是二级索引为同一个现有发电机表创建自动缩放策略。这特定于 dynamoDB GSI,而不是上述问题的重复。

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


【解决方案1】:

创建一个新的或使用覆盖您当前的 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

【讨论】:

  • 此脚本将尝试使用existing_table_name 创建表,但由于表已存在而失败。
  • 希望表是首先使用 CloudFormation 创建的。否则,无法获取现有资源并将它们包含在新的 CloudFormation 堆栈中。
猜你喜欢
  • 2016-10-28
  • 2020-11-12
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
相关资源
最近更新 更多