将范围键添加到 DynamoDB 表而不破坏表

pet*_*jet 2 java amazon-web-services amazon-dynamodb terraform

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

我知道它有 Prevent_destroy = true 。我想更新表而不删除或破坏旧数据。

旧的:

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

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

  lifecycle {
    prevent_destroy = true
  }

}
Run Code Online (Sandbox Code Playgroud)

更新后:

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
  }

}
Run Code Online (Sandbox Code Playgroud)

Mar*_*cin 6

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

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