You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform:如何从嵌套Map提取标签配置S3 Bucket资源

解决AWS S3 Bucket从嵌套tag子Map提取标签的问题

你需要从var.s3_bucket_configuration的嵌套tag子Map中提取Customer和Platform标签,为AWS S3 Bucket资源添加。原代码是直接从bucket配置Map中提取标签,现在结构调整后,可按以下方式修改代码:

原有效代码(标签直接在bucket配置下时)

{ Platform = lookup(each.value, "Platform", "") },
{ Customer = lookup(each.value, "Customer", "") }

当前变量结构

> var.s3_bucket_configuration
{
  "bucket_1" = {
    "acl" = "log-delivery-write"
  }
  "bucket_2" = {
    "description" = "Description bucket_2"
    "mfa_delete_enabled" = false
    "object_ownership" = "BucketOwnerEnforced"
    "sse_algorithm" = "AES256"
    "versioning_enabled" = false
    "tag" = {
      "Customer" = "Customer_bucket_2"
      "Platform" = "Platform_bucket_2"
       }
  }
}

修改后的代码

{ 
  Platform = lookup(lookup(each.value, "tag", {}), "Platform", ""),
  Customer = lookup(lookup(each.value, "tag", {}), "Customer", "") 
}

说明

  • 外层lookup先获取当前bucket配置里的tag子Map,默认值设为空{},避免没有tag键的bucket(比如bucket_1)触发报错
  • 内层lookup从tag子Map中提取对应标签值,默认空字符串保持原逻辑的兼容性

修改后,bucket_2能正确提取到Customer_bucket_2和Platform_bucket_2,bucket_1则返回空字符串,和原逻辑一致。

内容的提问来源于stack exchange,提问作者YAZ84

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:07:43