带有 DynamoDB 后端的 terraform:创建新工作区时出现 400 Bad Request

terraform with DynamoDB backend: 400 Bad Request when creating new workspace

我有在 DynamoDB 中保持锁定的 terraform 堆栈:

terraform {
  backend "s3" {
    bucket  = "bucketname"
    key     = "my_key"
    encrypt = "true"
    role_arn = "arn:aws:iam::11111111:role/my_role"
    dynamodb_table = "tf-remote-state-lock"
  }
}

当我 运行 terraform workspace new test 它失败并出现(非常误导的)错误:

failed to lock s3 state: 2 errors occurred:
* ResourceNotFoundException: Requested resource not found
* ResourceNotFoundException: Requested resource not found

如果我打开 TF_LOG=DEBUG 然后我会看到 400 Bad Request(更多详细信息 on pastebin

我做错了什么以及如何解决?

可能您没有创建 AWS 资源。 documentation 表示以下关于 s3 存储桶的内容:

This assumes we have a bucket created called mybucket.

我认为第二条消息来自 DynamoDb table 未创建或未正确配置。该文档说明了以下有关使用 DynamoDb 进行锁定和一致性的内容:

dynamodb_table - (Optional) Name of DynamoDB Table to use for state locking and consistency. The table must have a primary key named LockID with type of string. If not configured, state locking will be disabled.

解决方案:terraform workspace whatsoever 应该只在 terraform init 之后朗姆酒。如果您设置了 TF_WORKSPACE,您可能会在 tf init 期间出错,提示工作区尚不存在,因此您可能会想在 tf init 之前朗姆酒 tf ws new。不做,就只在tf init后设置TF_WORKSPACE