Terraform配置Google Cloud Storage时matches_prefix生命周期规则失效
Terraform配置GCS桶生命周期规则时matches_prefix被忽略问题
我正在用Terraform给特定Google Cloud Storage(GCS)桶前缀配置生命周期规则,按照GCS文档,应该用matches_prefix = "path"来指定前缀,但执行terraform plan和terraform apply时,这个配置被忽略了,规则直接应用到了整个桶。
我的当前代码:
module "my-buckets" { source = "terraform-google-modules/cloud-storage/google" version = "3.2" project_id = var.gcp_project_id location = "US" names = ["operational-bucket"] prefix = var.env_stage # 变量已定义,当前值为dev versioning = { operational-bucket = true } randomize_suffix = false lifecycle_rules = [ { action = { type = "Delete" } condition = { matches_prefix = "var/logs" age = 400 } } ] }
执行terraform plan的结果显示matches_prefix是空数组:
resource "google_storage_bucket" "buckets" { id = "dev-operational-bucket" name = "dev-operational-bucket" # (11 unchanged attributes hidden) + lifecycle_rule { + action { + type = "Delete" } + condition { + age = 400 + matches_prefix = [] + matches_storage_class = [] + matches_suffix = [] + with_state = (known after apply) } } # (1 unchanged block hidden) }
问题出在matches_prefix的参数类型上。你使用的terraform-google-modules/cloud-storage模块(3.2版本)会直接将配置映射到Terraform官方的google_storage_bucket资源,而该资源的lifecycle_rule.condition.matches_prefix是列表类型(list(string)),支持指定多个前缀,而非单个字符串。
你当前写的是单个字符串"var/logs",模块无法正确解析这个值,导致最终生成的配置里matches_prefix为空数组,规则也就应用到了整个桶。
只需要把matches_prefix改成数组形式即可:
condition = { matches_prefix = ["var/logs"] age = 400 }
修改后重新执行terraform plan,就能看到matches_prefix被正确设置为["var/logs"],规则只会匹配该前缀下的对象。
内容的提问来源于stack exchange,提问作者Julia Bel
相关产品推荐
相关产品推荐

