terraform-azurerm-lz-vending: bug: Onboarding subscriptions created outside the module is destructive

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave “+1” or “me too” comments, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Versions

Please paste the output of terraform version command from within the initialized directory:

Terraform v1.8.4
on windows_amd64
+ provider registry.terraform.io/azure/azapi v1.13.1
+ provider registry.terraform.io/hashicorp/azurerm v3.106.1
+ provider registry.terraform.io/hashicorp/time v0.11.2

Please enter the module version that you are using:

4.1.2

Description

When trying to on-board existing subscriptions into the module for resource management (tags, display name, etc.), imports result in destructive behavior. Short of manually creating a Terraform state file by hand, this effectively prevents the ability to manage subscriptions that may have been created outside the module.

Steps to Reproduce

  1. Create a root module that includes both an import block and the terraform-azurerm-lz-vending module
  2. Execute terraform plan
import {
  id = "/providers/Microsoft.Subscription/aliases/my-sub-123"
  to = module.lz_vending_subscription.module.subscription[0].azurerm_subscription.this[0]
}

module "lz_vending_subscription" {
  source                     = "../Modules/terraform-azurerm-lz-vending/4.1.2"

  subscription_billing_scope = "<appropriate_billing_scope>"
  subscription_id            = "" # Managing subscription, not Management Groups
  subscription_alias_enabled = true
  subscription_alias         = "my-sub-123"
  subscription_display_name  = "My Subscription 123"
  subscription_workload      = "Production"
  location                   = var.sub_location

  subscription_management_group_association_enabled = false
  virtual_network_enabled                           = false
  
  role_assignment_enabled = true
  role_assignments        = var.sub_role_assignments

  subscription_tags = var.sub_tags
}

Expected Output

  # module.lz_vending_subscription.module.subscription[0].azurerm_subscription.this[0] will be imported
    resource "azurerm_subscription" "this" {
        alias             = "my-sub-123"
        billing_scope_id  = "***"
        id                = "/providers/Microsoft.Subscription/aliases/my-sub-123"
        subscription_id   = "***"
        subscription_name = "My Subscription 123"
        tags              = {
            "Key1"    = "Value1"
            "Key2"    = "Value2"
        }
        tenant_id         = "***"
        workload          = "Production"

Actual Output

  # module.lz_vending_subscription.module.subscription[0].azurerm_subscription.this[0] must be replaced
  # (imported from "/providers/Microsoft.Subscription/aliases/my-sub-123")
  # Warning: this will destroy the imported resource
-/+ resource "azurerm_subscription" "this" {
        alias             = "my-sub-123"
      + billing_scope_id  = "***"
      ~ id                = "/providers/Microsoft.Subscription/aliases/my-sub-123" -> (known after apply)
      ~ subscription_id   = "***" -> (known after apply)
        subscription_name = "My Subscription 123"
      ~ tags              = {
          + "Key1"    = "Value1"
          + "Key2"    = "Value2"
        }
      ~ tenant_id         = "***" -> (known after apply)
      + workload          = "Production" # forces replacement

Additional context

  • There is an option in the module to provide a subscription_id variable. However, the implemented functionality it provides is geared toward updating the Management Group associations without managing the subscription resource, which is the opposite of the desired outcome.

  • This does look like a chained issue, given the module relies on the the azurerm_subscription resource. The root cause appears to be the underlying REST API not providing the same context on subsequent refresh operations as it does on the initial create operation.

  • Directed by Microsoft support to submit this issue against this repository.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 17 (10 by maintainers)

Most upvoted comments

@csmith66 For this specific use case it seems like setting subscription_update_existing = true would be a happy compromise here. This in conjunction with setting the subscription_id will allow you to update the subscription_tags and subscription_display_name arguments of the existing subscription.

@kevinmccurdybrd

You can do this now, for the existing subscription id, just set that to null in the instances where you have vended a subscription - either that or use lookup and set the default to null in that function.

Specifying null is the same as not specifying the value at all.

@kevinmccurdybrd thank you. This context really helps.

@matt-FFFFFF I can give you an example of where it isn’t an edge case: We’re an organization where we used to be on a legacy EA but moved our Azure spend to a CSP, which means we no longer can create our own subscriptions. In this case the CSP partner creates our subscription and then we assume control of it with the LZVM to manage other aspects, as well as our EA created subscriptions. We may move back to an MSA at some point in the future (I don’t love the CSP thing TBH). That said, I understand that the primary intent of this module is for scale automation, but there are cases where managing outside previously created subs is relevant.

Edit: I gave this a read again with more coffee, and I think I missed a key detail above that affects relevance of my comment, however I’ll leave this comment as an example of edge use-cases.

Hi @csmith66

This is incredibly surprising that import of existing subscriptions has not been a considered use case. I would suggest it is a highly important use case specifically applicable to this module.

The primary use case for this module is to simplify the creation of subscriptions at scale - hence we have not encountered this before and it seems to be an edge case.

Adding lifecycle block

We will consider the effect of this:

lifecycle {
  ignore_changes = [
    workload,
  ]
}

The alias resource

The problem is that the alias resource is awfully strange. It is a proxy to create a subscription, OR it can be created for an existing subscription (though the benefits of doing so are no clear). Once created, changes to properties.aditionalProperties are not passed onto the subscription that has been created and they are effectively read-only. You can also have multiple aliases pointing at one subscription (again, no idea why you’d want to do this).

Deleting the alias does NOT cancel the subscription. However azurerm tries to be helpful and do this for you. Unless you are managing all resources within that subscription in the same plan, it is highly likely that this cancel operation will fail.

Using AzAPI

We introduced an alternative way of managing the subscription alias resource for customers that had no access to the default management group - azurerm creates the sub in default mg and then moves it, this is problematic in some organizations.

The use of this…

module "lz_vending_subscription" {
  # ...
  subscription_use_AzAPI = true
}

Produces an expected result. Do not be concerned, the subscription cancel operation has when = "destroy" set, which means it will only fire on destroy and not on create. We have tried to mimic all of the functionality of the azurerm_subscription resource using azapi.

In the case that you had the subscription_use_AzAPI = true enabled, you would be able to import the existing alias (if there was one):

import {
  to = module.azure_landing_zone_subscription.module.subscription[0].azapi_resource.subscription[0]
  id = "/providers/Microsoft.Subscription/aliases/foo"
}

If there is no alias for the existing subscription then things are more tricky. If all you want to do is manage tags and display name then I suggest copying the azapi_update_resource (tags) and azapi_resource_action (rename) from this module and implementing yourself.

Again, managing naming and tags for existing subscriptions is not something we have expressly designed for. We only manage MG membership and deploy resources through the other sub-modules.

Thanks for following this slightly lengthy response.