【问题标题】:Terraform compare keys two different maps and copy valueTerraform比较键两个不同的地图并复制值
【发布时间】:2020-10-12 23:55:16
【问题描述】:

我有一个输出地图的模块,module.platformusers.paths,像这样:

{
    "user1_test" = "user1_value_path"
    "user2_test" = "user2_value_path"
    "user3_test" = "user3_value_path"
}

我必须遍历local.musyers 映射并获取键并与module.platformusers.paths 键进行比较,如果键包含在第二个映射中,那么我必须复制值

{
    "user1" = "Allen"
    "user2" = "john"
    "user3" = "Rose"
}

当我假设他们是个人时,我想要这样的东西

resource "aws_ssm_parameter" "userspath" {
  name  = "Allen"
  type  = "String"
  value = "user1_value_path"
}

resource "aws_ssm_parameter" "userspath" {
  name  = "john"
  type  = "String"
  value = "user2_value_path"
}

resource "aws_ssm_parameter" "userspath" {
  name  = "Rose"
  type  = "String"
  value = "user3_value_path"
}

我正在尝试如下所示,但它不起作用,因为我的键不完全匹配:

resource "aws_ssm_parameter" "userspath" {
  for_each =  module.platformusers.paths
  name  = ${each.value}
  type  = "String"
  value = lookup( module.platformusers.paths,  ${each.key}, "")
}

这里如何申请包含?

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    可能有更好的方法来做到这一点,但这是我能想到的一种快速方法.. 如果键的差异只是如上所示的“_test”,那么实际上可能很简单。您可以使用格式功能添加“_test”并进行查找。

    在您的示例代码中,您没有针对 local.musyers 进行循环。 注意:它区分大小写。您可以使用 UPPER 函数使其不敏感。

    resource "aws_ssm_parameter" "userspath" {
      for_each =  local.musyers
      name  = ${each.value}
      type  = "String"
      value = lookup(module.platformusers.paths, format("%s_test", each.key) , "missing")
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2017-06-19
      • 1970-01-01
      相关资源
      最近更新 更多