Azure DevOps pipelines - create and then select terraform workspace

1.5k Views Asked by At

I have prepared terraform modules for building some IaC infrastructure on Azure Cloud . Currently I am preparing Azure Pipelines release. I have created all the tasks in my pipeline. I want to use terraform deployment on multiple environments like: dev, test, prod, so I decided to use terraform workspaces. I have prepared in my release pipeline for Dev environment following tasks: terraform init, terraform validate, terraform plan, terraform apply. Problem is that I am not sure how can I create terraform workspace and then use terraform workspace. When I am creating azure devops pipelines from classic editor there are tasks for creating terraform workspace and selecting terraform workspace actions but they don't work as expected. I am trying to use pipeline variables to stora terraform workspace name, I am using terraform env variable TF_WORKSPACE. Provided by MS Tasks in Azure Devops YAML format looks like below.

terraform workspace new task

steps:
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
  displayName: 'terraform workspace'
  inputs:
    command: workspace
    workingDirectory: '$(System.DefaultWorkingDirectory)/Project-Name/drop/Terraform'
    workspaceSubCommand: new
    skipExistingWorkspace: true

terraform workspace select task

variables:
  TF_WORKSPACE: 'Development'

steps:

    - task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
      displayName: 'terraform workspace'
      inputs:
        command: workspace
        workingDirectory: '$(System.DefaultWorkingDirectory)/Project-Name/drop/Terraform'
        workspaceName: '-TF_WORKSPACE $(TF_WORKSPACE)'
1

There are 1 best solutions below

1
On

I have added in Azure DEvops pipeline additional task "Command Line Script", and removed both tasks for terraform workspace new, terraform workspace select actions. Finally pipeline works as expected.