在不破坏 table 的情况下将范围键添加到 DynamoDB table

Add range key to DynamoDB table without destroying the table

我有一个只有散列键的 table,我想使用 terraform 添加新的范围键和 GSI,而不影响 table 中的数据或破坏它。

我知道它有 prevent_destroy = true。 我想在不删除或破坏旧数据的情况下更新 table。

老一辈:

resource "aws_dynamodb_table" "table" {
  name         = "table_example"
  hash_key     = "hash"

  attribute {
    name = "hash"
    type = "S"
  }

  lifecycle {
    prevent_destroy = true
  }

}

更新后:

resource "aws_dynamodb_table" "table" {
  name         = "table_example"
  hash_key     = "hash"
  range_key    = "range"

  attribute {
    name = "hash"
    type = "S"
  }

  attribute {
    name = "range"
    type = "S"
  }

  global_secondary_index {
    name            = "gsi-example"
    hash_key        = "range"
    projection_type = "ALL"
  }

  lifecycle {
    prevent_destroy = true
  }

}

不能这样做。对 KeySchema 的任何更改 都需要替换

您必须备份数据,更新时创建新的table,然后重新上传。或者,使用您的排序键创建 GSI。这样您就可以保持主 table 不变,并在需要时在 GSI 上运行以使用排序键。