'Azure Devops - Terraform task fails with Error: Invalid backend configuration argument

I am tying to run terraform on my azure Devops pipeline. I am using the terraform extension version 0.1.8 from the marketplace by MicrosoftDevLabs My task looks as below :

task: TerraformTaskV1@0
displayName: 'Terraform - Init'
inputs:
provider: 'azurerm'
command: 'init'
commandOptions: '-input=false'
backendServiceArm: 'service-connection'
backendAzureRmResourceGroupName: 'Project-RG'
backendAzureRmStorageAccountName: 'projectsa'
backendAzureRmContainerName: 'tfstate'
backendAzureRmKey: 'terraform.tfstate'
workingDirectory: terraform

The command it tries to execute is

`/opt/hostedtoolcache/terraform/0.13.5/x64/terraform init -backend-config=storage_account_name=projectsa -backend-config=container_name=tfstate -backend-config=key=terraform.tfstate -backend-config=resource_group_name=Project-RG -backend-config=arm_subscription_id=xxxx-xxxx-xxxx -backend-config=arm_tenant_id=*** -backend-config=arm_client_id=*** -backend-config=arm_client_secret=***’

And the error message is:

Initializing the backend...  
Error: Invalid backend configuration argument
The backend configuration argument "storage_account_name" given on the command line is not expected for the selected backend type.   
Error: Invalid backend configuration argument  
The backend configuration argument "container_name" given on the command line is not expected for the selected backend type.   
Error: Invalid backend configuration argument  
The backend configuration argument "key" given on the command line is not expected for the selected backend type.


Solution 1:[1]

I ran into a similar error and found that using task TerraformTaskV2@2 in my pipeline yml as opposed to the older TerraformTaskV1@0 resolved the issue. This newer task also works with very a recent Terraform version like 1.1.4.

  # Install Terraform on Agent
  - task: TerraformInstaller@0
    displayName: 'install'
    inputs:
      terraformVersion: '1.1.4'

  # Initialize Terraform
  - task: TerraformTaskV2@2
    displayName: 'init'
    inputs:
      provider: 'azurerm'
      command: 'init'
      backendAzureRmResourceGroupName: 'prodbackendstf'
      backendAzureRmStorageAccountName: 'productiontfstate'
      backendAzureRmContainerName: 'tfstate'
      backendAzureRmKey: 'tf.state'
      backendServiceArm: 'IaC SPn'
      workingDirectory: '$(System.DefaultWorkingDirectory)/terraform'

Solution 2:[2]

I ran this

- task: TerraformInstaller@0
  inputs:
    terraformVersion: '0.13.5'
- task: TerraformTaskV1@0
  inputs:
    provider: 'azurerm'
    command: 'init'
    workingDirectory: '$(System.DefaultWorkingDirectory)/stackoverflow/74-terraform'
    backendServiceArm: 'rg-the-code-manual'
    backendAzureRmResourceGroupName: 'TheCodeManual'
    backendAzureRmStorageAccountName: 'thecodemanual'
    backendAzureRmContainerName: 'infra'
    backendAzureRmKey: 'tfstate-so-74'
    commandOptions: '-input=false'

and got it working

2020-12-04T10:06:25.4318809Z [command]/opt/hostedtoolcache/terraform/0.13.5/x64/terraform init -backend-config=storage_account_name=thecodemanual -backend-config=container_name=infra -backend-config=key=tfstate-so-74 -backend-config=resource_group_name=TheCodeManual -backend-config=arm_subscription_id=<subscriptionId> -backend-config=arm_tenant_id=*** -backend-config=arm_client_id=*** -backend-config=arm_client_secret=***
2020-12-04T10:06:25.4670082Z 
2020-12-04T10:06:25.4675423Z [0m[1mInitializing the backend...[0m
2020-12-04T10:06:25.4738557Z [0m[32m
2020-12-04T10:06:25.4740133Z Successfully configured the backend "azurerm"! Terraform will automatically
2020-12-04T10:06:25.4742265Z use this backend unless the backend configuration changes.[0m
2020-12-04T10:06:25.9242628Z [33m
2020-12-04T10:06:25.9244849Z [1m[33mWarning: [0m[0m[1m"arm_client_id": [DEPRECATED] `arm_client_id` has been replaced by `client_id`[0m
2020-12-04T10:06:25.9246980Z 
2020-12-04T10:06:25.9248608Z [0m[0m[0m
2020-12-04T10:06:25.9249659Z [33m
2020-12-04T10:06:25.9251909Z [1m[33mWarning: [0m[0m[1m"arm_client_secret": [DEPRECATED] `arm_client_secret` has been replaced by `client_secret`[0m
2020-12-04T10:06:25.9252897Z 
2020-12-04T10:06:25.9254321Z [0m[0m[0m
2020-12-04T10:06:25.9255028Z [33m
2020-12-04T10:06:25.9256913Z [1m[33mWarning: [0m[0m[1m"arm_tenant_id": [DEPRECATED] `arm_tenant_id` has been replaced by `tenant_id`[0m
2020-12-04T10:06:25.9261480Z 
2020-12-04T10:06:25.9262574Z [0m[0m[0m
2020-12-04T10:06:25.9263605Z [33m
2020-12-04T10:06:25.9264816Z [1m[33mWarning: [0m[0m[1m"arm_subscription_id": [DEPRECATED] `arm_subscription_id` has been replaced by `subscription_id`[0m
2020-12-04T10:06:25.9265629Z 

With info about deprecation of settings, but this at the moment doesn't lead to a fail. For this there is already issue and PR on github.

Did you run TerraformInstaller before TerraformTaskV1?

Solution 3:[3]

I'm using terraform v1.1.3 and the TerraformTaskV1@0 azure pipeline task. I was getting the same issue, strange as the same azurerm block used to work when I was using 0.14.X. To fix I edited the backend block so it would use access_key blob storage key instead, then removed TerraformTaskV1@0, and instead initialised terraform using a cmd task as shown below:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.92"
    }
  }

  backend "azurerm" {
    storage_account_name = "**terraformStorageAccount**"
    container_name       = "**terraformStateFileContainer**"
    key                  = "**terraformStateFile**"
    access_key           = "**storageKey**" #this is sensitive so should be retrieved from safe place via keyvault or dev ops pipeline variables
  }

  required_version = ">= 1.1.0"
}

I use this task to replace the pipeline variables with the ones in the tf files. You'll have to install this task btw:

- task: replacetokens@3
  displayName: Replace Variable Tokens
  inputs:
    rootDirectory: '$(Pipeline.Workspace)'
    targetFiles: '**/*.tf'
    encoding: 'auto'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    tokenPrefix: '**'
    tokenSuffix: '**'
    useLegacyPattern: false
    enableTelemetry: false

Once the variables are replaced use a cmd task to terraform init

- task: CmdLine@2
  inputs:
    script: 'terraform init'

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 isambay
Solution 2 Krzysztof Madej
Solution 3