'Terraform : Access output variables of module in the YML file

I am using following code to push static files to storage account using Azure CLI.

- stage: Apply_On_Dev
dependsOn: [Setup, Prepare, Plan_Dev, Build_Frontend_Dev]
jobs:
  - deployment: 'apply'
    displayName: Dev
    environment: dev
    strategy:
      runOnce:
        deploy:
          steps:
            - template: terraform-apply.yml
              parameters:
                environment: 'dev'
                environmentPath: 'dev'
                terraformStateFilename: 'frontend-devnew.tfstate'
                artifacts: 'dev_artifacts'

            - task: DownloadBuildArtifacts@0
              inputs:
                buildType: 'current' # Options: current, specific
                downloadType: 'single' # Options: single, specific
                artifactName: 'drop' # Required when  downloadType == Single content
                downloadPath: '.'

            - task: AzureCLI@2
              displayName: 'Copy Frontend to storage Account'
              inputs:
                azureSubscription: '$(subscription)'
                scriptType: bash
                scriptLocation: inlineScript
                inlineScript: |
                  echo "storage_account_name: $(terraform output -raw storage_account_name)"
                  echo "storage_account_primary_access_key: $(terraform output -raw module.frontend.storage_account_primary_access_key)"
                  az storage blob upload-batch -s drop -d \$web --account-name "" --account-key "" --overwrite
                addSpnToEnvironment: false

I would like to get the storage account name and key from output.tf in the YML file for the Azure CLI commands, how can I achieve this?

The terraform module does have output variable for the storage account (see below screen)

output.tf file

output "storage_account_primary_access_key" {
      value = azurerm_storage_account.storage.primary_access_key
}
    
output "storage_account_name" {
      value = azurerm_storage_account.storage.name
}

Module Call Code

module "frontend" {
      source              = "../modules/frontend"
      resource_group_name = var.resource_group_name
      location            = var.location
      tags                = { Project = "Frontend", Environment = "Dev" }
}


Solution 1:[1]

The way to use module outputs [1] is the following:

module.<module_name>.<output_name>

In your case, since it seems you already have defined the desired outputs on the module level, you can try adding the outputs in the root module by creating an outputs.tf file. Those outputs will reference the outputs from the module:

output "storage_account_name" {
  value       = module.frontend.storage_account_name
  description = "Storage account name."
}

output "storage_account_primary_access_key" {
  value       = module.frontend.storage_account_primary_access_key
  description = "Storage account primary access key."
}

Then, the rest echo commands and the AZ CLI command should look like this:

...
 echo "$(terraform output -raw storage_account_name)"
 echo "$(terraform output -raw storage_account_primary_access_key)"
 az storage blob upload-batch -s drop -d \$web --account-name $(terraform output -raw storage_account_name) --account-key $(terraform output -raw storage_account_primary_access_key) --overwrite
...

[1] https://www.terraform.io/language/values/outputs#accessing-child-module-outputs

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1