【问题标题】:Terraform plan does not include all of my .tf changesTerraform 计划不包括我的所有 .tf 更改
【发布时间】:2021-10-14 03:39:01
【问题描述】:

我正在使用 AWS 提供商。我在 lifecycle_rule 块中添加了具有适当 daysstorage_class 属性的事务块。除了这一变化,我还将expiry_days 从 30 增加到 180。

变量如下所示:

variable "bucket_details" {
  type = map(object({
    bucket_name = string
    purpose =     string
    infrequent_transition_days = number
    infrequent_transition_storage = string
    archive_transition_days = number
    archive_transition_storage = string
    expiry_days = number
    versioning =  bool
  }))
}

资源看起来像这样:(我已经删除了不相关的配置)

resource "aws_s3_bucket" "bucket-s3" {
  for_each = var.bucket_details

  bucket = "${each.key}-${var.region}-${var.environment}"

  lifecycle_rule {
    id =      "clear"
    enabled = true

    transition {
      days = each.value.infrequent_transition_days
      storage_class = each.value.infrequent_transition_storage
    }

    transition {
      days = each.value.archive_transition_days
      storage_class = each.value.archive_transition_storage
    }
  
    expiration {
      days = each.value.expiry_days
    }
  }
}

我已关注this transition example 以供参考。

当我运行transaction plan 时,我得到以下输出:

~ lifecycle_rule {
    abort_incomplete_multipart_upload_days = 0
    enabled                                = true
    id                                     = "clear"
    tags                                   = {}

  + expiration {
      + days = 180
    }
  - expiration {
      - days                         = 30 -> null
      - expired_object_delete_marker = false -> null
    }
}

未列出任何过渡更改。可能是因为转换是 AWS 特有的,因此 Terraform 没有捕捉到它吗?

【问题讨论】:

    标签: amazon-web-services amazon-s3 terraform


    【解决方案1】:

    我按原样尝试了您的代码,这是响应:

    provider "aws" {
      region = "us-west-2"
    }
    
    variable "region" {
      default = "us-west-2"
    }
    
    variable "environment" {
      default = "dev"
    }
    
    variable "bucket_details" {
      type = map(object({
        bucket_name = string
        infrequent_transition_days = number
        infrequent_transition_storage = string
        archive_transition_days = number
        archive_transition_storage = string
        expiry_days = number
      }))
      default = {
        hello_world = {
        bucket_name: "demo-001",
        infrequent_transition_days: 10,
        infrequent_transition_storage: "STANDARD_IA",
        archive_transition_days: 10,
        archive_transition_storage: "GLACIER",
        expiry_days = 30
      }}
    }
    
    resource "aws_s3_bucket" "bucket-s3" {
      for_each = var.bucket_details
    
      bucket = "${each.key}-${var.region}-${var.environment}"
    
      lifecycle_rule {
        id =      "clear"
        enabled = true
    
        transition {
          days = each.value.infrequent_transition_days
          storage_class = each.value.infrequent_transition_storage
        }
    
        transition {
          days = each.value.archive_transition_days
          storage_class = each.value.archive_transition_storage
        }
    
        expiration {
          days = each.value.expiry_days
        }
      }
    }
    

    Terraform 计划的响应:

    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # aws_s3_bucket.bucket-s3["hello_world"] will be created
      + resource "aws_s3_bucket" "bucket-s3" {
          + acceleration_status         = (known after apply)
          + acl                         = "private"
          + arn                         = (known after apply)
          + bucket                      = "hello_world-us-west-2-dev"
          + bucket_domain_name          = (known after apply)
          + bucket_regional_domain_name = (known after apply)
          + force_destroy               = false
          + hosted_zone_id              = (known after apply)
          + id                          = (known after apply)
          + region                      = (known after apply)
          + request_payer               = (known after apply)
          + tags_all                    = (known after apply)
          + website_domain              = (known after apply)
          + website_endpoint            = (known after apply)
    
          + lifecycle_rule {
              + enabled = true
              + id      = "clear"
    
              + expiration {
                  + days = 30
                }
    
              + transition {
                  + days          = 10
                  + storage_class = "GLACIER"
                }
              + transition {
                  + days          = 10
                  + storage_class = "STANDARD_IA"
                }
            }
    
          + versioning {
              + enabled    = (known after apply)
              + mfa_delete = (known after apply)
            }
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
    
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply"
    now.
    

    如您所见,有transition changes。您可以尝试设置默认变量并检查响应吗?

    【讨论】:

    • 感谢 tomarv2,已排除。我会继续深入研究它。问题是这是一个我还不熟悉的大项目,而且我也是 Terraform 的初学者。
    • 这个答案对于 StackOverflow 来说是正确的。我的具体问题不在于 Terraform 本身,而在于我们的基础架构是如何配置的。
    猜你喜欢
    • 2021-01-30
    • 2019-10-13
    • 1970-01-01
    • 2021-01-16
    • 2021-08-30
    • 1970-01-01
    • 2020-10-15
    • 2019-04-17
    • 2021-12-27
    相关资源
    最近更新 更多