DEV Community

Krzysztof Kopieczek
Krzysztof Kopieczek

Posted on

7 1

terraform apply in Azure DevOps fails with "resource with the ID already exists"

Recently I was developing a simple Terraform pipeline in Azure DevOps. Everything seemed intuitive and easy - install, init, plan, apply. Clear steps and UI forms for every setting.
terraform init settings in the Azure DevOps Pipeline

Everything was configured properly. Code was working fine from my local machine, but in Azure DevOps, pipeline run successfully only for the first time. Every next attempt resulted in the following error:
Error: The process 'C:\hostedtoolcache\windows\terraform\1.0.8\x64\terraform.exe' failed with exit code 1

And details from the log:
Error: [0m[0m[1mA resource with the ID "/subscriptions/45197771-2d3e-4685-aaef-e96f62dd80b4/resourceGroups/tfexample-sample-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.[0m

The problem was in my code

provider "azurerm" {
  subscription_id = "45197771-2d3e-4685-aaef-e96f62dd80b4"
  features {}
}
Enter fullscreen mode Exit fullscreen mode

It was missing the backend part! Code worked from my local machine, because terraform saved the state in local terraform.tfstate file (default terraform behavior), but in Azure DevOps the settings provided in the UI was ignored with no clear error message. terraform apply couldn't find the state file, so was creating every resource from scratch each time.

The solution

I fixed the code by adding empty properties. That was enough to have the pipeline working correctly. Note that no values are required...

provider "azurerm" {
  subscription_id = "45197771-2d3e-4685-aaef-e96f62dd80b4"
  features {}
}

terraform {
  backend "azurerm" {
    resource_group_name = ""
    storage_account_name = "" 
    container_name       = "" 
    key                  = ""  
  }
}
Enter fullscreen mode Exit fullscreen mode

Please leave some comment if this helped.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (5)

Collapse
 
patheyacine profile image
patheyacine

You saved me hours of struggle. Thank you so much. I implemented your suggestion and it started working :)

Collapse
 
mohameedtrabelsi profile image
hammatrb

hello, is this the solution ?
because , i have a similare one
when i create a resource using a pipeline azure and when i want to update the code with adding some resources, it give me the some error

Collapse
 
kopieczekdev profile image
Krzysztof Kopieczek

hello, sorry for late response
generally - yes. With this error, it's an issue with state - probably it's not stored anywhere

Collapse
 
annapurnamaha profile image
Anu

If we leave backend settings blank, then where would our state file have saved?

Thread Thread
 
kopieczekdev profile image
Krzysztof Kopieczek

in Azure Dev Ops

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay