Mario Jacobo
Mario Jacobo

Reputation: 117

Terraform output variables as input

I'm new to Terraform and trying to wrap my head around the use of output variables. we are on AKS, and I'm deploying the following resources: resource group, log analytics workspace, Azure Kubernetes. When Log analytics is deployed, I capture the workspace ID into an output variable. Now, when Terraform deploys Kubernetes, it needs to know the workspace ID, how can I pass the output value to the addon_profile (last line in the code below)?

Error:

environment = "${log_analytics_workspace_id.value}"

A managed resource "log_analytics_workspace_id" "value" has not been declared in the root module.

Code:

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}"**
      }
}

Upvotes: 4

Views: 11861

Answers (1)

Adil B
Adil B

Reputation: 16866

Terraform's output values are like the "return values" of a module. In order to declare and use the log_analytics_workspace_id output value, you would need to put all of the code for the creation of the resource group, log analytics workspace, and Azure Kubernetes infrastructure into a single Terraform module, and then reference the output value from outside of the module:

# declare your module here, which contains creation code for all your Azure infrastructure + the output variable
module "azure_analytics" {
  source = "git::ssh://[email protected]..."
}

# 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}"
      }
}

On the other hand, if you just want to use the workspace_id value from your azurerm_log_analytics_workspace within the same code, just reference it like azurerm_log_analytics_workspace.test.workspace_id.

Upvotes: 2

Related Questions