Home > Enterprise >  Azure Pipelines terraform init -backend-config throwing exit code 127
Azure Pipelines terraform init -backend-config throwing exit code 127

Time:04-14

Have a simple pipeline for terraform planning and deployment

Getting an error (exit code 127) when running a terraform init in my pipeline via a template

Here's the template that holds the init steps

steps:
  - task: Bash@3
    displayName: 'Terraform Init'
    env:
      ARM_CLIENT_ID: $(AZURE_CLIENT_ID)
      ARM_CLIENT_SECRET: $(AZURE_CLIENT_SECRET)
      ARM_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
      ARM_TENANT_ID: $(AZURE_TENANT_ID)
    inputs:
      targetType: 'inline'
      workingDirectory: $(System.DefaultWorkingDirectory)
      script: |
        set -euo pipefail
        
        echo "Initialize"
        terraform init \
            -input=false \
            -backend-config="resource_group_name=${TF_STORAGE_RG}" \ 
            -backend-config="storage_account_name=${TF_STORAGE_ACCOUNT}" \
            -backend-config="container_name=${TF_STORAGE_BLOB_CONTAINER}" \
            -backend-config="key=${TF_STORAGE_BLOB_NAME}" 
    
     
        terraform validate
    
       
        terraform -v
        terraform providers

I have the values stored in a variable group accessible to the pipeline

but I'm getting the error

Terraform initialized in an empty directory!

The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
/home/vsts/work/_temp/d3a0a3ba-04aa-4b39-8c4a-78045790fbe4.sh: line 8: -backend-config=storage_account_name="statebucket": command not found
##[error]Bash exited with code '127'.
Finishing: Terraform Init

Struggling to understand why it's throwing a 'command not found' error at me. Any ideas?

CodePudding user response:

There's a space at the end of the line following the "\" after ${TF_STORAGE_RG}. See below - if I run a sed to remove the space at the end of the line, the "command not found" error goes away. I get a different error, but that's a different issue.

$ cat doit.sh
export TF_STORAGE_RG=a
export TF_STORAGE_ACCOUNT=b
export TF_STORAGE_BLOB_CONTAINER=c
export TF_STORAGE_BLOB_NAME=d

terraform init \
        -input=false \
        -backend-config="resource_group_name=${TF_STORAGE_RG}" \
        -backend-config="storage_account_name=${TF_STORAGE_ACCOUNT}" \
        -backend-config="container_name=${TF_STORAGE_BLOB_CONTAINER}" \
        -backend-config="key=${TF_STORAGE_BLOB_NAME}"

$ ./doit.sh
Too many command line arguments. Did you mean to use -chdir?
./doit.sh: line 10: -backend-config=storage_account_name=b: command not found

$ sed -i 's/ $//g' doit.sh
$ ./doit.sh

Initializing the backend...
╷
│ Error: Failed to get existing workspaces: Error retrieving keys for Storage Account "b": storage.AccountsClient#ListKeys: Invalid input: autorest/validation: validation failed: parameter=accountName constraint=MinLength value="b" details: value length must be greater than or equal to 3
  • Related