英文:
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_state或tfe_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论