azure-pipelines-terraform: Unable to specify subscription ID for backend in terraform init when multiple subscriptions are available

Currently, when running terraform init with a Service Connection that has access to more than one subscription, the command fails to specify the correct subscription ID for the backend. The -backend-config=subscription_id= option is set, causing the command to fail with an error.

Attempts to add the -backend-config=subscription_id=xxxx option to commandOptions in the pipeline also fail, as it is prepended to the command instead of appended, and the empty -backend-config=subscription_id= overrides the manually specified subscription ID.

This issue could be potentially fixed if commandOptions were appended instead of prepended to the terraform binary, allowing the subscription ID to be properly specified.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 23 (1 by maintainers)

Most upvoted comments

In the short term there is an undocumented environment variable, ENDPOINT_DATA_AzureRM_SUBSCRIPTIONID, that seems to work successfully.

Example use:

            - task: TerraformTaskV4@4
              displayName: terraform init
              inputs:
                provider: 'azurerm'
                command: 'init'
                workingDirectory: '$(workingDirectory)'
                backendServiceArm: '$(serviceConnection)'
                backendAzureRmResourceGroupName: '$(BACKEND_AZURE_RESOURCE_GROUP_NAME)'
                backendAzureRmStorageAccountName: '$(BACKEND_AZURE_STORAGE_ACCOUNT_NAME)'
                backendAzureRmContainerName: '$(BACKEND_AZURE_STORAGE_ACCOUNT_CONTAINER_NAME)'
                backendAzureRmKey: 'terraform.tfstate'
                backendAzureRmUseEnvironmentVariablesForAuthentication: true
                backendAzureRmUseEntraIdForAuthentication: true
              env:
                ARM_USE_AZUREAD: true
                ENDPOINT_DATA_AzureRM_SUBSCRIPTIONID: '$(BACKEND_AZURE_SUBSCRIPTION_ID)'

My config is similar to others - I have an OIDC workload identity (user-assigned managed identity) with Contributor on one subscription and Blob Contributor on a specific storage account container scope for the pipeline’s remote state.

Hi @JayDoubleu , Think there is an misunderstanding roaming in your reply… I’m not talking about single SP for entire environment. Quite the opposite… I want to have dedicated SP for each environment that ONLY have access to its own SA (in other subscription) for storing tfstate. This way you enhance security for state as well as storing it outside your deployed environment. This facilitates several layers of security as well.

We introduced a subscription ID override parameter in v5 which may help with this use case.

Closing for now.

@P4R4DiSi4C Ok, same thing though!

The Terratask backendrm configuration is just the same as if you specified terraform plan --backend-config=backend.hcl at the command line which points to where the statefile is. Plus with terratask it becomes the default location for resources, which I think is the unclear bit causing you the issue.

But the provider configuration describes where the resources are deployed. i.e… you can specify a different subscription for the default:

provider “azurerm” { subscription_id = var.subscription_id … } Plus you can specify multiple providers too and reference them in the terraform

provider “azurerm” { features {} alias = “sub_a” subscription_id = var.subA }

provider “azurerm” { features {} alias = “sub_b” subscription_id = var.subB }

resource “azurerm_virtual_network” “vnet” { provider = azurerm.sub_b … }

That works like a charm indeed ! Just tested it out and you were right on this with terratask it becomes the default location for resources, which I think is the unclear bit causing you the issue.

It’s now properly using the centralized storage for tfstate but deploying resources on the defined sub in the provider.

Thanks a lot

@theo-albers I can confirm this no longer work as expected with two service connections. I will take a look. this is error is unrelated the above thread which is about two subs but one service connection. I will create a separate Issue

Please implement this feature! For now we should use non-official task

task: TerraformCLI

https://github.com/jason-johnson/azure-pipelines-tasks-terraform/blob/main/overview.md

Which has this ability with

backendAzureRmSubscriptionId: 'my-backend-subscription-id'