Azure Pipeline TerraformCLI task trying to recreate already existing resource

1.4k Views Asked by At

I am trying to create an azure pipeline with Terraform. But when I ran this for the first time, it created half of the resources and failed in apply step. When I corrected the steps it failed with below error.

Error: A resource with the ID "/subscriptions/2c13ad21-ae92-4e09-b64f-2e24445dc076/resourceGroups/apim-resource-gp" 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.
│ 
│   with module.resource_gp.azurerm_resource_group.apim_rg,
│   on resourcegroup/resource-group.tf line 1, in resource "azurerm_resource_group" "apim_rg":
│    1:  resource "azurerm_resource_group" "apim_rg" {

Here I observed the problem, the plan step again creating a plan file which says all resources to be 'created' rather than skipping the already created resource. Another observation is that my tfstate file which was supposed to be created in storage-account, didn't get created. But I am unable to figure out what has gone wrong here. Pasting my azure-pipelines.yaml

azure-pipelines.yaml

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
variables:
  tf_version: "latest"
  tf_state_rg: "blogpost-tfstate-rg"
  tz_state_location: "centralus"
  tf_state_sa_name: "apimstrgaccount"
  tf_state_container_name: "tfstate"
  tf_state_tags: ("env=blogpost-terraform-devops-pipeline" "deployedBy=devops")
  tf_environment: "dev"
  tf_state_sku: "Standard_LRS"
  SUBSCRIPTION_NAME: "pipeline-terraform"
trigger:
- main

pool:
  vmImage: ubuntu-latest

steps:
  - task: terraformInstaller@0
    displayName: "Install Terraform $(tf_version)"
    inputs:
      terraformVersion: "$(tf_version)"
  - task: TerraformCLI@0
    inputs:
      command: "init"
      backendType: "azurerm"
      backendServiceArm: "$(SUBSCRIPTION_NAME)"
      ensureBackend: true
      backendAzureRmResourceGroupName: "$(tf_environment)-$(tf_state_rg)"
      backendAzureRmResourceGroupLocation: "$(tz_state_location)"
      backendAzureRmStorageAccountName: "$(tf_state_sa_name)"
      backendAzureRmStorageAccountSku: "$(tf_state_sku)"
      backendAzureRmContainerName: $(tf_state_container_name)
      backendAzureRmKey: "$(tf_environment).terraform.tstate"
    displayName: "Run > terraform init"

  - task: TerraformCLI@0
    inputs:
      command: "validate"
      environmentServiceName: "$(SUBSCRIPTION_NAME)"
    displayName: "Run > terraform validate"

  - task: TerraformCLI@0
    displayName: 'terraform plan'
    inputs:
      command: plan
      publishPlanResults: "$(SUBSCRIPTION_NAME)"
      environmentServiceName: "$(SUBSCRIPTION_NAME)"
      commandOptions: '-out=$(System.DefaultWorkingDirectory)/terraform.tfplan -detailed-exitcode'

  - task: TerraformCLI@0
    displayName: 'terraform apply'
    condition: and(succeeded(), eq(variables['TERRAFORM_PLAN_HAS_CHANGES'], 'true'))
    inputs:
      command: apply
      environmentServiceName: "$(SUBSCRIPTION_NAME)"
      commandOptions: '$(System.DefaultWorkingDirectory)/terraform.tfplan'
1

There are 1 best solutions below

0
On BEST ANSWER

I came across similar error :resource with the ID "/subscriptions/xxxx/resourceGroups/<rg>" 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 when I tried to Terraform pipeline in azure devops .

The devops pipeline was not be able to find state in the Azure UI and I even had this azure_rm provider set.

terraform {
  required_providers {
   
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.2"
      
    }

  }

enter image description here

The error happens when terraform state may have not matched with the real state.

  • I have this terraform.tfstate file where the current state is stored in local. But still it was occurring. But when I tried to add terraform backend in “main.tf” file., then the file was executed without that error.

Try with no values like below.

terraform {
  backend "azurerm" {
    resource_group_name = ""
    storage_account_name = "" 
    container_name       = "" 
    key                  = ""  
  }
}

Or give the values :

terraform {
  backend "azurerm" {
    resource_group_name      = "<rg>"
    storage_account_name     = "<give acct >"
    container_name           = "terraform"
    key                      = "terraform.tfstate"
  }

And state lock the terraform state to store in azure storage account.

enter image description here

Also try to import state using terraform import <terraform_id> <azure_resource_id>