title: A resource with the ID already exists - to be managed via Terraform this resource needs to be imported into the State
body: <p>I would not normally ask a question like this, however I feel stuck and I do not want to hack things, but rather take the time to understand. I am new to terraform and trying to learn it, a simple task that I have set myself is to create a SQL server.</p>
  <p>My Environment</p>
  <p>I have some resource groups created before, anytime I try to use the same name, I get the error.</p>
  <pre><code> Error: A resource with the ID &quot;/subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learning&quot; already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for &quot;azurerm_resource_group&quot; for more information.
  </code></pre>
  <p>Now, you look at the error and after 2 days of google research, i followed the steps here.
  <a href="https://stackoverflow.com/questions/47439848/using-terraform-to-import-existing-resources-on-azure">Using Terraform to import existing resources on Azure</a>
  and <a href="https://gmusumeci.medium.com/how-to-import-an-existing-azure-resource-in-terraform-6d585f93ea02" rel="nofollow noreferrer">https://gmusumeci.medium.com/how-to-import-an-existing-azure-resource-in-terraform-6d585f93ea02</a> and <a href="https://stackoverflow.com/questions/61418168/terraform-resource-with-the-id-already-exists">Terraform resource with the ID already exists</a></p>
  <p>I create a file called existing_statee.tf with the content below.</p>
  <pre><code>resource &quot;azurerm_resource_group&quot; &quot;tf_learning&quot; {
  }
  </code></pre>
  <p>Ran</p>
  <pre><code>terraform import azurerm_resource_group.tf_learning /subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learningterraform import azurerm_resource_group.tf_learning /subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learning
  </code></pre>
  <p>I edited the file again and saved it.</p>
  <pre><code>resource &quot;azurerm_resource_group&quot; &quot;tf_learning&quot; {
    # (resource arguments)
    name = &quot;tf_learning&quot;
    location = &quot;UK South&quot;
  }
  </code></pre>
  <p>Then ran the following.</p>
  <pre><code>terraform init
  terraform plan
  terraform apply
  </code></pre>
  <p>To my surprise I am still getting the error.</p>
  <pre><code> Error: A resource with the ID &quot;/subscriptions/00000-00000-0000-0000-00000000000/resourceGroups/tf_learning&quot; already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for &quot;azurerm_resource_group&quot; for more information.
  │
  │   with azurerm_resource_group.RG-Terraform,
  │   on main.tf line 1, in resource &quot;azurerm_resource_group&quot; &quot;RG-Terraform&quot;:
  │    1: resource &quot;azurerm_resource_group&quot; &quot;RG-Terraform&quot; {
  </code></pre>
  <p>My tf.main file.</p>
  <pre><code>resource &quot;azurerm_resource_group&quot; &quot;RG-Terraform&quot; {
    name     = var.resource-group-name
    location = var.my_location
  }
  
  resource &quot;azurerm_sql_server&quot; &quot;test&quot; {
    name                         = var.my_dev_server
    resource_group_name          = azurerm_resource_group.RG-Terraform.name
    location                     = azurerm_resource_group.RG-Terraform.location
    version                      = var.my_dev_sql_version
    administrator_login          = var.my_dev_sql_login
    administrator_login_password = &quot;change_me&quot;
  }
  
  resource &quot;azurerm_sql_database&quot; &quot;test&quot; {
    name                = var.my_dev_sql_database
    resource_group_name = azurerm_resource_group.RG-Terraform.name
    location            = azurerm_resource_group.RG-Terraform.location
    server_name         = azurerm_sql_server.test.name
    edition                          = var.my_dev_sql_database_sku
    requested_service_objective_name = var.my_dev_sql_database_objective
  
    tags = {
      environment = &quot;dev_database_build&quot;
    }
  }
  </code></pre>
  <h2>variables.tf file</h2>
  <pre><code>variable &quot;resource-group-name&quot; {
    default     = &quot;tf_learning&quot;
    description = &quot;The prefix used for all resources in this example&quot;
  }
  
  variable &quot;app-service-name&quot; {
    default     = &quot;terraform-app-service&quot;
    description = &quot;The name of the Web App&quot;
  }
  
  variable &quot;location&quot; {
    default     = &quot;UK South&quot;
    description = &quot;The Azure location where all resources in this example should be created&quot;
  }
  
  variable &quot;my_location&quot; {
    type = string
    default = &quot;UK South&quot;
  }
  
  variable &quot;my_dev_server&quot;{
    type = string
    default = &quot;test-server-test&quot;
  }
  
  variable &quot;my_dev_sql_login&quot; {
    type = string
    default = &quot;mylogin&quot;  
  }
  
  variable &quot;my_dev_sql_version&quot; {
    type = string
    default = &quot;12.0&quot;  
  }
  
  variable &quot;my_dev_sql_database&quot; {
    type = string
    default = &quot;dev_db&quot;  
  }
  
  variable &quot;my_dev_sql_database_tag&quot; {
    type = string
    default = &quot;dev sql database from TF&quot;  
  }
  
  variable &quot;my_dev_sql_database_sku&quot; {
    type = string
    default = &quot;Basic&quot;  
  }
  
  variable &quot;my_dev_sql_database_objective&quot; {
    type = string
    default = &quot;Basic&quot;  
  }
  </code></pre>
  <p>I am lost as to what to do next, for now I will carry on researching.</p>