获取terraform输出的数据并将其用作输入。

huangapple go评论63阅读模式
英文:

Get data from terraform Output and use it as input

问题

我正在使用Terraform来构建和自动化基础设施,但我在寻找解决方案时遇到了困难,即获取Azure WebApp的输出,具体来说是WebApp使用的公共IP地址,并将它们用作更新Cloudflare列表的输入。

output "webapp_ip" {
value = azurerm_linux_web_app.lwa.outbound_ip_addresses
}

将输出类似于这样:

`webapp_ip = "ip-address-1,ip-address-2,ip-address-3,ip-address-4"` 

而Cloudflare列表的Terraform资源以以下格式接收数据:

item {
value {
ip = "ip-address-1"
}
comment = "IP 1"
}

item {
value {
ip = "ip-address-2"
}
comment = "IP 2"
}


我考虑使用动态列表,但我不知道是否是正确的方法...
英文:

I'm using Terraform to build and automate infrastructure and I'm having trouble in finding the solution to grab the output of an Azure WebApp, specifically the Public IP addresses used by that WebApp and use them as inputs to update a Cloudflare list.

output "webapp_ip" {
  value = azurerm_linux_web_app.lwa.outbound_ip_addresses
}

will output something like this:

webapp_ip = "ip-address-1,ip-address-2,ip-address-3,ip-address-4"

and Cloudflare_list terraform resource receives data on the following format:

item {
    value {
      ip = "ip-address-1"
    }
    comment = "IP 1"}

item {
    value {
      ip = "ip-address-2"
    }
    comment = "IP 2"
}

I was thinking in dynamic lists but I don't know if it's the right way to go...

答案1

得分: 1

Dynamic blocks 是这里的正确方法。您的代码应该看起来像这样:

resource "cloudflare_list" "example" {

  // 其他属性

  dynamic "item" {
    for_each = toset(split(",", azurerm_linux_web_app.lwa.outbound_ip_addresses)) // 注意split
    content {
      value {
        ip = item.value
      }
      comment = "IP: ${item.value}"
    }
  }
}

如果Azure和Cloudflare位于两个不同的工作区(状态),您可以使用terraform_remote_statetfe_outputs来将Azure的输出传递给Cloudflare。

英文:

Dynamic blocks is the right way to go here. Your code should look something like this:

resource "cloudflare_list" "example" {

  // some other attributes

  dynamic "item" {
    for_each = toset(split(",", azurerm_linux_web_app.lwa.outbound_ip_addresses)) // notice split
    content {
      value {
        ip = item.value
      }
      comment = "IP: ${item.value}"
    }
  }
}

If Azure and Cloudflare are in two different workspaces (states) you can use either terraform_remote_state or tfe_outputs to pass Azure outputs to Cloudflare.

huangapple
  • 本文由 发表于 2023年3月3日 23:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定