microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
95 stars 59 forks source link

TF_WORKSPACE inconsistency on new workspaces #184

Open j4t1nd3r opened 9 months ago

j4t1nd3r commented 9 months ago

I have the following pipeline:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: "windows-latest"

jobs:
  - job: TF_WORKSPACE_def
    displayName: "TF_WORKSPACE def"
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          workingDirectory: tf
          backendServiceArm: "client-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp-test"

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "client-platform-nonprod (REMOVED)"

  - job: TF_WORKSPACE_dev
    displayName: "TF_WORKSPACE dev"
    dependsOn: TF_WORKSPACE_def
    variables:
      TF_WORKSPACE: dev
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          workingDirectory: tf
          backendServiceArm: "client-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp-test"
        env:
          TF_WORKSPACE: $(TF_WORKSPACE)

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "client-platform-nonprod (REMOVED)"
        env:
          TF_WORKSPACE: $(TF_WORKSPACE)

  - job: TF_WORKSPACE_test
    displayName: "TF_WORKSPACE test"
    dependsOn: TF_WORKSPACE_dev
    variables:
      TF_WORKSPACE: test
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          workingDirectory: tf
          backendServiceArm: "client-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp-test"
        env:
          TF_WORKSPACE: $(TF_WORKSPACE)

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "client-platform-nonprod (REMOVED)"
        env:
          TF_WORKSPACE: $(TF_WORKSPACE)

Backend Config:

terraform {
  backend "azurerm" {}

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

  required_version = "~> 1.0"
}

provider "azurerm" {
  features {}
}

Problem

On first run:

The currently selected workspace (test) does not exist. This is expected behavior when the selected workspace did not have an existing non-empty state. Please enter a number to select a workspace:

  1. default
  2. dev

Troubleshooting

Between each pipeline run, I am deleting the state file as I make changes to try and figure this out..

I have switched around the variable value for TF_WORKSPACE (dev and test) for the 2nd and 3rd job. *The first job is the one without TF_WORKSPACE set so it uses the default workspace

The second job creates the relevant workspace, the third job gives us the error outline previously.

Can someone explain why dev workspace is created but the test one is not? Surely either both should be or neither considering the test of the code but the variable TF_WORKSPACE is the same?

What is the correct way to create a workspace using this task and the variable TF_WORKSPACE if the method I am trying to use above is incorrect?

If this is not a task issue, do I need to raise this on the terraform github?

j4t1nd3r commented 9 months ago

I did get it to work by:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: "windows-latest"

jobs:
  - job: TF_WORKSPACE_def
    displayName: "TF_WORKSPACE def"
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          provider: "azurerm"
          command: "init"
          workingDirectory: tf
          backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp"

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"

  - job: TF_WORKSPACE_dev
    displayName: "TF_WORKSPACE dev"
    dependsOn: TF_WORKSPACE_def
    variables:
      scope: dev
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          provider: "azurerm"
          command: "init"
          workingDirectory: tf
          backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp"

      - task: TerraformTaskV4@4
        displayName: "Terraform_Workspace new|select"
        inputs:
          command: custom
          customCommand: workspace
          commandOptions: select -or-create $(scope)
          workingDirectory: tf
          environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
        env:
          TF_WORKSPACE: $(scope)

  - job: TF_WORKSPACE_test
    displayName: "TF_WORKSPACE test"
    dependsOn: TF_WORKSPACE_dev
    variables:
      scope: test
    steps:
      - task: TerraformInstaller@1
        displayName: "Terraform_install 1.5.7"
        inputs:
          terraformVersion: 1.5.7

      - task: TerraformTaskV4@4
        displayName: "Terraform_init"
        inputs:
          provider: "azurerm"
          command: "init"
          workingDirectory: tf
          backendServiceArm: "REMOVED-platform-nonprod (REMOVED)"
          backendAzureRmResourceGroupName: "REMOVED-nonprod-base-infra-rg"
          backendAzureRmStorageAccountName: REMOVED
          backendAzureRmContainerName: tfstate-jat
          backendAzureRmKey: "jr-sandbox-tf-wp"

      - task: TerraformTaskV4@4
        displayName: "Terraform_Workspace new|select"
        inputs:
          command: custom
          customCommand: workspace
          commandOptions: select -or-create $(scope)
          workingDirectory: tf
          environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"

      - task: TerraformTaskV4@4
        displayName: "Terraform_plan"
        inputs:
          workingDirectory: tf
          command: plan
          environmentServiceNameAzureRM: "REMOVED-platform-nonprod (REMOVED)"
        env:
          TF_WORKSPACE: $(scope)

However I would still like to know what is with the inconsistency of TF_WORKSPACE of creating the first workspace and not the second one when it is set as mentioned in my original post.

mericstam commented 8 months ago

Hi sorry for late reply. Looks like you solved the issue. I will take a look on the inconsistent behavior.