0
votes

I'm trying to run Terraform from a PowerShell step in a YAML-configured Azure DevOps Pipeline, but I can't get it to accept my incantation: instead of initializing the state, it prints the usage instructions and exits.

I've tried a number of combinations of with/without quotes for the -backend-config parameter, but nothing seems to work.

If I cut-and-paste the script into a ps1 file locally and run it, everything works as expected.

What is wrong with my incantation of ./terraform.exe init here?

Here's the pipeline configuration, in its entirety:

pool:
  vmImage: 'win1803'

steps:
- powershell: |
    # Download the Terraform executable into the current directory
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

    $url = 'https://releases.hashicorp.com/terraform/0.11.8/terraform_0.11.8_windows_amd64.zip'
    $output = "$PSScriptRoot\terraform.zip"

    Write-Host "Downloading from $url to $output"
    $wc = New-Object System.Net.WebClient
    $wc.DownloadFile($url, $output)

    Expand-Archive $output -DestinationPath . -Force

    Get-ChildItem
  displayName: 'Download Terraform executable'
- powershell: |
    # Create a config file with secrets, and initialize Terraform
    "storage_account_name = `"$env:TerraformStateAccountName`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "access_key = `"$env:TerraformStateAccountKey`"" | Out-File "./terraform-state.secrets.tfvars" -Append
    "container_name = `"$env:TerraformStateContainer`"" | Out-File "./terraform-state.secrets.tfvars" -Append

    ./terraform.exe init -backend-config=./terraform-state.secrets.tfvars -input=false
  env:
    TerraformStateAccountName: $(Terraform.State.StorageAccountName)
    TerraformStateAccountKey: $(Terraform.State.StorageAccountKey)
    TerraformStateContainer: $(Terraform.State.ContainerName)
  displayName: 'Initialize Terraform'
1
what gets printed exactly?4c74356b41
@4c74356b41: The same thing as when I run ./terraform.exe init -foo locally, except locally I get a final line saying flag provided but not defined: -foo while on the build server I get no message at all.Tomas Aschan
@4c74356b41: As I wrote, it is not exactly the same. It is the same with one exception: the last line of the local output of ./terraform.exe init -foo is not included on the build server. (If you want to be rude, at least don't put words in my mouth in order to have something to be rude about.)Tomas Aschan

1 Answers

0
votes

I would remove the blanks surrounding the = sign.

I prefer using environment variables, documented here:

  1. ARM_SUBSCRIPTION_ID
  2. ARM_CLIENT_ID
  3. ARM_CLIENT_SECRET
  4. ARM_TENANT_ID
  5. ARM_OBJECT_ID
  6. ARM_ACCESS_KEY

You can also initialize Terraform variables using environment variables as documented here.