4

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, that's exactly what I needed. the second option seems easier to implement for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.