在我的模块中,我想根据是否设置了变量来启用或禁用gcp存储桶lifecycle_rule,因此基本上我希望某些存储桶启用规则,而其他存储桶则忽略规则设置。但是terraform没有对GCP lifecycle_rule的支持enabled
,而该支持很容易做到(请查看下面的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:无效或未知密钥:已启用
任何实现上述要求的想法都值得赞赏。