1
votes

I'm trying to create Azure resources from Azure web site CLI, but strangely it throws the error below after run "terraform apply". It wasn't like this before.

Error: A resource with the ID "/subscriptions/certdab-as441-4a670-bsda-2456437/resourceGroups/commapi" 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.

  on terra.tf line 14, in resource "azurerm_resource_group" "rg1":
  14: resource "azurerm_resource_group" "rg1" {

Terraform code is below;

provider "azurerm" {
  version = "=2.20.0"
  features {}
  subscription_id = "c413asdasdasdadsasda1c77"
  tenant_id       = "da956asdasdadsadsasd25a535"
}

resource "azurerm_resource_group" "rg" {
  name     = "commapi"
  location = "West Europe"
}


resource "azurerm_resource_group" "rg1" {
  name     = "commapi"
  location = "West Europe"
}

Regarding to their web site, if a resource group is alredy created, we should import it. But I don't get it how?

They said I should import like below, should I add this line to my terraform code? Or should I run this import command for every Azure tool?

terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1

So for example if I already created 10 Azure resources before and if I add 11.th tool to my Terraform code, should I run this "import" command for each that 10 resources which already created before? That's so weird.

How can I create these resources?

Edit: If I try again, throws the error below;

Error: A resource with the ID "/subscriptions/asdd-asde1-4asd-bsda-asasd/resourceGroups/commerceapi/providers

/Microsoft.ApiManagement/service/commapi-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.

  on terra.tf line 65, in resource "azurerm_api_management" "apm":
  65: resource "azurerm_api_management" "apm" {

Thanks!

Sample 2 :

For example when I create the API, it throws the error below;

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_api_management.apm: Creating...

Error: A resource with the ID "/subscriptions/c4112313-123-123-123-1c77/resourceGroups/testapi/providers/Microsoft.ApiManagement/service/api-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.

  on commerceApi.tf line 67, in resource "azurerm_api_management" "apm":
  67: resource "azurerm_api_management" "apm" {

After that, when I try to import command, this time it throws the error below;

PS C:\Users\set\Desktop\Terraform\Test_Commerceapi> terraform import azurerm_api_management.apm /subscriptions/c4asdb-234e1-23-234a-23424337/resourceGroups/testapi
azurerm_api_management.apm: Importing from ID "/subscriptions/c4234324-234-234-23-4347/resourceGroups/testapi"...
azurerm_api_management.apm: Import prepared!
  Prepared azurerm_api_management for import
azurerm_api_management.apm: Refreshing state... [id=/subscriptions/c4234324-23-4234-234324-77/resourceGroups/testapi]

Error: making Read request on API Management Service "" (Resource Group "testapi"): apimanagement.ServiceClient#Get: Invalid input: autorest/validation: validation failed: parameter=serviceName constraint=MinLength value="" details: value length must be greater than or equal to 1
1
Why do you create two resource groups with the same name in your terraform code?Nancy Xiong
It is just a sample, even if I add a vnet or VM, it throws the same error.sleepy

1 Answers

0
votes

If you want to bring existing infrastructure under Terraform management, you can use Import.

For example, import an existing resource group.

declare the resource in the .tf file like this.

resource "azurerm_resource_group" "rg" {
}

Then run terraform import azurerm_resource_group.rg /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1 to import the existing resource group.

Then you can edit the Terraform file and add the name and location of the existing resource to the Resource Group. After this, you are ready to use the resource. Read how to Import an Existing Azure Resource in Terraform for more details.

Please note that Terraform import can only import resources into the state. It does not generate configuration. If you just intend to create new resources relying on existing resources, you could use data sources.

Data sources allow data to be fetched or computed for use elsewhere in Terraform configuration. Use of data sources allows a Terraform configuration to make use of information defined outside of Terraform, or defined by another separate Terraform configuration.

For example, use Data Source: azurerm_resource_group to access information about an existing Resource Group. You can create new resources within the existing VNet.

data "azurerm_resource_group" "example" {
  name = "existing"
}

output "id" {
  value = data.azurerm_resource_group.example.id
}