2

在我的模块中,我想根据是否设置了变量来启用或禁用 gcp 存储桶生命周期规则,所以基本上我希望一些存储桶启用规则,而其他存储桶忽略规则设置。但是 terraform 不支持enabled可以轻松做到这一点的 GCP 生命周期规则(请查看下面的 AWS 链接)。

AWS:https ://www.terraform.io/docs/providers/aws/r/s3_bucket.html#enabled-1

GCP: https ://www.terraform.io/docs/providers/google/r/storage_bucket.html#lifecycle_rule

我尝试有条件地将规则的操作类型和 storage_class 设置为“”

    resource "google_storage_bucket" "bucket_ageoff" {
      count = "${var.enable_ageoff}"

      name     = "${local.bucket_name}"
      project  = "${local.project_id}"
      location = "${var.region}"

      storage_class = "${local.bucket_storage_class}"

      encryption {
        default_kms_key_name = "${google_kms_crypto_key.bucket_key.self_link}"
      }

      logging {
        log_bucket = "${google_storage_bucket.log_bucket.name}"
      }

      labels {
        "environment" = "${var.env}"
      }

      lifecycle_rule {
        action {
          type = "Delete"
        }

        condition {
          age = "${var.ageoff_days}"
        }
      }

      lifecycle_rule {
        action {
          type = "${var.coldline_days != ""  ? "SetStorageClass" : ""}"
          storage_class = "${var.coldline_days != ""  ? "COLDLINE" : ""}"
        }

        condition {
          age = "${var.coldline_days != "" ? var.coldline_days : "0"}"
        }
      }

      }
    }

但是 gcp api 给出了这个错误:

发生 1 个错误:

  • module.log_archive_bucket.google_storage_bucket.bucket_ageoff:发生 1 个错误:

  • google_storage_bucket.bucket_ageoff:googleapi:错误 400:无效参数,无效

以下是代码实际尝试执行的操作:

      ...
      lifecycle_rule {
        //enabled is supported by TF for AWS but not GCP
        //enabled = "${var.coldline_days != "" ? true : false }"

        action {
          type = "SetStorageClass"
          storage_class = "COLDLINE"
        }

        condition {
          age = "${var.coldline_days != "" ? var.coldline_days : "0" }"
        }

      }
    }

错误:module.log_archive_bucket.google_storage_bucket.bucket_ageoff:lifecycle_rule.1:无效或未知密钥:已启用

任何实现上述要求的想法都值得赞赏。

4

0 回答 0