The following YAML snippet is a part of my Azure DevOps build pipeline:
- task: TerraformTaskV2@2
displayName: 'Terraform plan'
inputs:
provider: 'azurerm'
command: 'plan'
workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
commandOptions: '-out $(Build.BuildNumber)'
environmentServiceNameAzureRM: 'MASKED'
- task: TerraformTaskV2@2
displayName: 'Terraform approve and apply'
name: terraformApply
inputs:
provider: 'azurerm'
command: 'apply'
workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
commandOptions: '$(Build.BuildNumber)'
environmentServiceNameAzureRM: 'MASKED'
Also, "terraform plan" stage creates an output whose name is the same as the build number but terraform apply wouldn't pick that name to simulate a graceful skip if the resource group already exists. Terraform apply task always appends "auto-approve" per the following example where 1.0.0 is the build number:
terraform apply -auto-approve 1.0.0
This piece of YAML runs well and creates the resource group if it does not exist. There are a few other steps after this step that have to run too. terraformApply
stage fails if the resource group already exists and hence the following steps won't run. I would like to have a graceful pipeline to skip terraform apply
stage if the resource group already exists and execute the following steps in the pipeline after "apply". How can I achieve this goal?
The error details reads as below:
2021-06-01T13:30:26.3472705Z ##[section]Starting: Terraform approve and apply
2021-06-01T13:30:26.3481129Z ==============================================================================
2021-06-01T13:30:26.3481656Z Task : Terraform
2021-06-01T13:30:26.3482325Z Description : Execute terraform commands to manage resources on AzureRM, Amazon Web Services(AWS) and Google Cloud Platform(GCP)
2021-06-01T13:30:26.3482826Z Version : 2.188.1
2021-06-01T13:30:26.3483165Z Author : Microsoft Corporation
2021-06-01T13:30:26.3483587Z Help : [Learn more about this task](https://aka.ms/AA5j5pf)
2021-06-01T13:30:26.3484057Z ==============================================================================
2021-06-01T13:30:26.4679906Z [command]/opt/hostedtoolcache/terraform/0.15.4/x64/terraform providers
2021-06-01T13:30:27.0463781Z
2021-06-01T13:30:27.0465060Z Providers required by configuration:
2021-06-01T13:30:27.0465541Z .
2021-06-01T13:30:27.0466639Z ├── provider[registry.terraform.io/hashicorp/azurerm] >= 2.26.0
2021-06-01T13:30:27.0467592Z └── provider[registry.terraform.io/hashicorp/random]
2021-06-01T13:30:27.0467902Z
2021-06-01T13:30:27.0478744Z [command]/opt/hostedtoolcache/terraform/0.15.4/x64/terraform validate
2021-06-01T13:30:28.5103220Z [32m[1mSuccess![0m The configuration is valid.
2021-06-01T13:30:28.5104320Z [0m
2021-06-01T13:30:28.5192981Z [command]/opt/hostedtoolcache/terraform/0.15.4/x64/terraform apply -auto-approve 1.0.0
2021-06-01T13:30:34.9083339Z [0m[1mazurerm_resource_group.gf: Creating...[0m[0m
2021-06-01T13:30:34.9697353Z [31m╷[0m[0m
2021-06-01T13:30:34.9699616Z [31m│[0m [0m[1m[31mError: [0m[0m[1mA resource with the ID "/subscriptions/MASKED/resourceGroups/FooResourceZGroup" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.[0m
2021-06-01T13:30:34.9701090Z [31m│[0m [0m
2021-06-01T13:30:34.9702369Z [31m│[0m [0m[0m with azurerm_resource_group.gf,
2021-06-01T13:30:34.9703361Z [31m│[0m [0m on main.tf line 16, in resource "azurerm_resource_group" "gf":
2021-06-01T13:30:34.9704127Z [31m│[0m [0m 16: resource "azurerm_resource_group" "gf" [4m{[0m[0m
2021-06-01T13:30:34.9704722Z [31m│[0m [0m
2021-06-01T13:30:34.9705176Z [31m╵[0m[0m
2021-06-01T13:30:34.9828118Z ##[error]Error: The process '/opt/hostedtoolcache/terraform/0.15.4/x64/terraform' failed with exit code 1
2021-06-01T13:30:34.9843807Z ##[section]Finishing: Terraform approve and apply
UPDATE The terraform YAML looks like the following code snippet in the pipeline:
- task: TerraformTaskV2@2
displayName: 'Terraform init'
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
backendServiceArm: 'MASKED'
backendAzureRmResourceGroupName: 'masked'
backendAzureRmStorageAccountName: 'masked'
backendAzureRmContainerName: 'multitstate'
backendAzureRmKey: 'terraform.state'
- task: TerraformTaskV2@2
displayName: 'Terraform plan'
inputs:
provider: 'azurerm'
command: 'plan'
workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
commandOptions: '-out $(Build.BuildNumber)'
environmentServiceNameAzureRM: 'MASKED'
- task: TerraformTaskV2@2
displayName: 'Terraform approve and apply'
name: terraformApply
inputs:
provider: 'azurerm'
command: 'apply'
workingDirectory: '$(System.DefaultWorkingDirectory)/infrastructure'
commandOptions: '$(Build.BuildNumber)'
environmentServiceNameAzureRM: 'MASKED'