【发布时间】:2021-01-08 01:57:08
【问题描述】:
TF 版本: 0.12.28 和 0.13.3
我的目标:
- 有一个用于 PROD env 的 AWS S3 存储桶来存储 tf 状态
- 拥有一个用于 NONPROD 环境的 AWS S3 存储桶来存储 tf 状态
按照this tutorial,我成功完成了以下任务:
- 一个 AWS S3 存储桶和一个名为 TEST 的文件夹中的 dynamodb:
provider "aws" {
region = var.aws_region_id
}
resource "aws_s3_bucket" "terraform_state" {
bucket = var.aws_bucket_name
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
}
resource "aws_dynamodb_table" "terraform_locks" {
name = var.aws_bucket_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
}
terraform {
backend "s3" {
bucket = "test-myproject-poc"
key = "global/s3/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "test-myproject-poc"
encrypt = true
}
}
到目前为止,一切都已成功部署
但是,当我想为 PROD 环境创建另一个 S3 存储桶/Dynamodb 时,发生了以下情况:
- 我去了另一个名为 PRODUCTION 的文件夹,我做了 terraform init(初始化没问题)
- 将我在 PROD 上的相同模块复制到此文件夹。我用 TEST 重命名了 PROD 以匹配环境
Terrarom 计划现在说它想替换我的实际部署来创建新的部署:
➜ S3 tf plan
Acquiring state lock. This may take a few moments...
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_dynamodb_table.terraform_locks: Refreshing state... [id=test-myproject-poc]
aws_s3_bucket.terraform_state: Refreshing state... [id=test-myproject-poc]
------------------------------------------------------------------------
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement
Terraform will perform the following actions:
# aws_dynamodb_table.terraform_locks must be replaced
-/+ resource "aws_dynamodb_table" "terraform_locks" {
~ arn = "arn:aws:dynamodb:us-east-1:1234567890:table/test-myproject-poc" -> (known after apply)
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
~ id = "test-myproject-poc" -> (known after apply)
~ name = "test-myproject-poc" -> "prod-myproject-poc" # forces replacement
- 状态实际上是在
global/s3/terraform.tfstate - 我没有使用工作区
不删除第一个 S3_PROD 的正确方法是什么?
【问题讨论】: