4

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.

My Environment

I have some resource groups created before, anytime I try to use the same name, I get the error.

 Error: A resource with the ID "/subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learning" 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.

Now, you look at the error and after 2 days of google research, i followed the steps here. Using Terraform to import existing resources on Azure and https://gmusumeci.medium.com/how-to-import-an-existing-azure-resource-in-terraform-6d585f93ea02 and Terraform resource with the ID already exists

I create a file called existing_statee.tf with the content below.

resource "azurerm_resource_group" "tf_learning" {
}

Ran

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

I edited the file again and saved it.

resource "azurerm_resource_group" "tf_learning" {
  # (resource arguments)
  name = "tf_learning"
  location = "UK South"
}

Then ran the following.

terraform init
terraform plan
terraform apply

To my surprise I am still getting the error.

 Error: A resource with the ID "/subscriptions/00000-00000-0000-0000-00000000000/resourceGroups/tf_learning" 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 azurerm_resource_group.RG-Terraform,
│   on main.tf line 1, in resource "azurerm_resource_group" "RG-Terraform":
│    1: resource "azurerm_resource_group" "RG-Terraform" {

My tf.main file.

resource "azurerm_resource_group" "RG-Terraform" {
  name     = var.resource-group-name
  location = var.my_location
}

resource "azurerm_sql_server" "test" {
  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 = "change_me"
}

resource "azurerm_sql_database" "test" {
  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 = "dev_database_build"
  }
}

variables.tf file

variable "resource-group-name" {
  default     = "tf_learning"
  description = "The prefix used for all resources in this example"
}

variable "app-service-name" {
  default     = "terraform-app-service"
  description = "The name of the Web App"
}

variable "location" {
  default     = "UK South"
  description = "The Azure location where all resources in this example should be created"
}

variable "my_location" {
  type = string
  default = "UK South"
}

variable "my_dev_server"{
  type = string
  default = "test-server-test"
}

variable "my_dev_sql_login" {
  type = string
  default = "mylogin"  
}

variable "my_dev_sql_version" {
  type = string
  default = "12.0"  
}

variable "my_dev_sql_database" {
  type = string
  default = "dev_db"  
}

variable "my_dev_sql_database_tag" {
  type = string
  default = "dev sql database from TF"  
}

variable "my_dev_sql_database_sku" {
  type = string
  default = "Basic"  
}

variable "my_dev_sql_database_objective" {
  type = string
  default = "Basic"  
}

I am lost as to what to do next, for now I will carry on researching.

learner
  • 545
  • 2
  • 9
  • 23

1 Answers1

3

I would like to point out this, you have configured in existing_statee.tf after importing the state

resource "azurerm_resource_group" "tf_learning" {
   name = "tf_learning"
   location = "UK South"
}

But in main.tf you also define again

resource "azurerm_resource_group" "RG-Terraform" {
  name     = var.resource-group-name
  location = var.my_location
}
variable "resource-group-name" {
  default     = "tf_learning"
  description = "The prefix used for all resources in this example"
}
variable "my_location" {
  type = string
  default = "UK South"
}

Since you have already declared this resource in existing_statee.tf, maybe you should remove it from main.tf

Tolis Gerodimos
  • 3,782
  • 2
  • 7
  • 14