【问题标题】:Terraform wants to replace existing resourcesTerraform 想要替换现有资源
【发布时间】: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 的正确方法是什么?

【问题讨论】:

    标签: amazon-s3 terraform


    【解决方案1】:

    我解决了这个问题!刚刚发现我需要删除这个块:

    terraform {
      backend "s3" {
        bucket         = "test-myproject-poc"
        key            = "global/s3/terraform.tfstate"
        region         = "us-east-1"
        dynamodb_table = "test-myproject-poc"
        encrypt        = true
      }
    }
    

    删除 .terraform 文件夹并再次运行 init。

    执行这些步骤后,计划按预期运行(它没有尝试删除我的部署)。

    我认为,但不确定是否强硬,是它试图使用之前部署的相同状态文件。所以我只好留下 tf 来创建 bucket 和 dynamo 表,最终运行在 S3 中存储新文件夹(PROD)的新状态的过程。

    HTH

    【讨论】:

      猜你喜欢
      • 2020-04-29
      • 2022-07-03
      • 2019-12-13
      • 2020-08-27
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 1970-01-01
      相关资源
      最近更新 更多