hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.6k stars 4.65k forks source link

cannot name new resource provider with azurerm as prefix or suffix #12438

Open dzmitry-lahoda opened 3 years ago

dzmitry-lahoda commented 3 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v1.0.0
on windows_amd64
+ provider registry.terraform.io/hashicorp/azuread v1.5.1
+ provider registry.terraform.io/hashicorp/azurerm v2.62.1

Affected Resource(s)

Terraform Configuration Files

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 2.62.1"
    }

    azurermbackend = {
      source  = "hashicorp/azurerm"
      version = "~> 2.62.1"
    }
  }
}

provider "azurerm" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = "11111111111111111111"
  features {}
}

provider "azurermbackend" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = "2222222222222222222222" 
  features {}
}

Debug Output

Expected Behaviour

  1. Old resources are in state from sub 1.
  2. New resources from sub 2 visible.

Actual Behaviour

  1. TF state is cleaned because second provider becomes default.

Steps to Reproduce

  1. Create default k8s cluster.
  2. Name provider with prefix or suffix of azurerm
  3. Run - cluster gone as new sub grabs default provider.
jackofallops commented 3 years ago

Hi @dzmitry-lahoda - I think the functionality you are looking for may be better achieved by using Provider Aliases in this case? https://www.terraform.io/docs/language/providers/configuration.html#alias-multiple-provider-configurations This will allow you to inform specific resources to use particular values for properties such as subscription_id. The Providers in your config share the same source, so shares the same data.

dzmitry-lahoda commented 3 years ago

So if to add alias to azurerm but with other sub, I will not get my non aliased resources destroyed (aks)?

provider "azurerm" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = "11111111111111111111"
  features {}
}

provider "azurerm" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = "2222222222222222222222" 
  alias = "foo"
  features {}
}
dzmitry-lahoda commented 3 years ago

it wee weird issue happening. works well:

data "azurerm_key_vault" "multiplay" {
  for_each = var.targets
  provider = azurerm-backend
..
}

provider "azurerm-backend" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = var.consumer_azure_subscription
  features {}
}

but when I add

provider "azurerm-aom" {
  tenant_id       = var.main_azure_tenant_id
  subscription_id = var.main_azure_subscription_id
  features {}
}

it is if provider = azurerm-backend is not applied. or even they reverse order-mapping

dzmitry-lahoda commented 3 years ago

if I use alias I get

│ Error: Insufficient features blocks
│
│   on <empty> line 0:
│   (source code not available)
│
│ At least 1 "features" blocks are required.

but all feature blocks are defined i think

UPDATE: Oh it is about that must have features {} block. Not related. There is issue for that.

dzmitry-lahoda commented 3 years ago

@jackofallops

The Providers in your config share the same source, so shares the same data.

Not sure why that happens. I configure subscription_id differently. And it still uses same subscription? So sub first is 1111111111 and sub second is 2222222222 , but still one of them used and other is neglected?