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
相关产品推荐
相关产品推荐

