Terraform 输出变量作为输入
Terraform output variables as input
我是 Terraform 的新手,正在尝试了解输出变量的使用。我们在 AKS 上,我正在部署以下资源:资源组、日志分析工作区、Azure Kubernetes。部署日志分析时,我将工作区 ID 捕获到输出变量中。现在,当 Terraform 部署 Kubernetes 时,它需要知道工作区 ID,我如何将输出值传递给 addon_profile
(下面代码的最后一行)?
错误:
environment = "${log_analytics_workspace_id.value}"
A managed resource "log_analytics_workspace_id" "value" has not been declared in the root module.
代码:
resource "azurerm_resource_group" "test" {
name = "${var.log}"
location = "${var.location}"
}
resource "azurerm_log_analytics_workspace" "test" {
name = "${var.logname}"
location = "${azurerm_resource_group.loganalytics.location}"
resource_group_name = "${azurerm_resource_group.loganalytics.name}"
sku = "PerGB2018"
retention_in_days = 30
}
**output "log_analytics_workspace_id" {
value = "${azurerm_log_analytics_workspace.test.workspace_id}"
}**
....................................................
addon_profile {
oms_agent {
enabled = true
**log_analytics_workspace_id = "${log_analytics_workspace_id.value}"**
}
}
Terraform's output values 就像模块的 "return values"。为了声明和使用 log_analytics_workspace_id
输出值,您需要将用于创建资源组、日志分析工作区和 Azure Kubernetes 基础结构的所有代码放入单个 Terraform 模块中,然后引用来自模块外部的输出值:
# declare your module here, which contains creation code for all your Azure infrastructure + the output variable
module "azure_analytics" {
source = "git::ssh://git@github.com..."
}
# now, you can reference the output variable in your addon_profile from outside the module:
addon_profile {
oms_agent {
enabled = true
log_analytics_workspace_id = "${module.azure_analytics.log_analytics_workspace_id}"
}
}
另一方面,如果您只想在同一代码中使用 azurerm_log_analytics_workspace
中的 workspace_id
值,只需像 azurerm_log_analytics_workspace.test.workspace_id
.
一样引用它
我是 Terraform 的新手,正在尝试了解输出变量的使用。我们在 AKS 上,我正在部署以下资源:资源组、日志分析工作区、Azure Kubernetes。部署日志分析时,我将工作区 ID 捕获到输出变量中。现在,当 Terraform 部署 Kubernetes 时,它需要知道工作区 ID,我如何将输出值传递给 addon_profile
(下面代码的最后一行)?
错误:
environment = "${log_analytics_workspace_id.value}"
A managed resource "log_analytics_workspace_id" "value" has not been declared in the root module.
代码:
resource "azurerm_resource_group" "test" {
name = "${var.log}"
location = "${var.location}"
}
resource "azurerm_log_analytics_workspace" "test" {
name = "${var.logname}"
location = "${azurerm_resource_group.loganalytics.location}"
resource_group_name = "${azurerm_resource_group.loganalytics.name}"
sku = "PerGB2018"
retention_in_days = 30
}
**output "log_analytics_workspace_id" {
value = "${azurerm_log_analytics_workspace.test.workspace_id}"
}**
....................................................
addon_profile {
oms_agent {
enabled = true
**log_analytics_workspace_id = "${log_analytics_workspace_id.value}"**
}
}
Terraform's output values 就像模块的 "return values"。为了声明和使用 log_analytics_workspace_id
输出值,您需要将用于创建资源组、日志分析工作区和 Azure Kubernetes 基础结构的所有代码放入单个 Terraform 模块中,然后引用来自模块外部的输出值:
# declare your module here, which contains creation code for all your Azure infrastructure + the output variable
module "azure_analytics" {
source = "git::ssh://git@github.com..."
}
# now, you can reference the output variable in your addon_profile from outside the module:
addon_profile {
oms_agent {
enabled = true
log_analytics_workspace_id = "${module.azure_analytics.log_analytics_workspace_id}"
}
}
另一方面,如果您只想在同一代码中使用 azurerm_log_analytics_workspace
中的 workspace_id
值,只需像 azurerm_log_analytics_workspace.test.workspace_id
.