Home > other >  Terraform resource with the ID already exists error when running Azure DevOps pipeline with Github C
Terraform resource with the ID already exists error when running Azure DevOps pipeline with Github C

Time:09-18

I was following the tutorial with this video: https://www.youtube.com/watch?v=Ff0DoAmpv6w&t=5905s (Azure DevOps: Provision API Infrastructure using Terraform) T

This is his github code, and mine was very similar: https://github.com/binarythistle/S03E03---Azure-Devops-and-Terraform

The problem was when the resource group doesn't exist on azure, say I deleted it manually, running the pipeine creates it as well as my container instance. But when I execute the pipline again when I try to commit some code change and push to github, it shows

azurerm_resource_group.rg: Creating...
╷
│ Error: A resource with the ID "/subscriptions/xxxxx/resourceGroups/xxx" already exists
│ 
│   with azurerm_resource_group.rg,
│   on main.tf line 30, in resource "azurerm_resource_group" "rg":
│   30: resource "azurerm_resource_group" "rg" {

Shouldn't it remember that a resource HAS been created before and skip this step - or perform some other action?

My observations

It looked like when the first time run, log shows there were some extra steps exceuted

azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/xxxxx/resourceGroups/xxx]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the
last "terraform apply" which may have affected this plan:

  # azurerm_resource_group.rg has been deleted
  - resource "azurerm_resource_group" "rg" {
        id       = "/subscriptions/xxxxx/resourceGroups/xxx"
      - location = "australiaeast" -> null
      - name     = "myTFResourceGroup" -> null
    }
Unless you have made equivalent changes to your configuration, or ignored the
relevant attributes using ignore_changes, the following plan may include
actions to undo or respond to these changes.

The second time pipeline was ran, the log shows:

Successfully configured the backend "azurerm"! Terraform will automatically
use this backend unless the backend configuration changes.

but then the error mentioned above was shown.

CodePudding user response:

deleting the .terraform local folder to clean the cache, then run terraform init again and retry running the pipeline.

  • Related