Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create before destroy for azuread #1805

Merged
merged 3 commits into from
Sep 29, 2023
Merged

Create before destroy for azuread #1805

merged 3 commits into from
Sep 29, 2023

Conversation

chianw
Copy link
Contributor

@chianw chianw commented Sep 28, 2023

Issue-id

PR Checklist


Description

When renaming an existing NSG that is already associated with a subnet, the results of the apply shows creating first before destroying when the correct behavior is to destroy then create. This results in the apply failing with the error that complains that the subnet already exists and needs to be imported into Terraform for management.

aztfmod version is int-5.7.0 and terraform version is 1.4.6

│ Error: A resource with the ID "/subscriptions/xxxxx/resourceGroups/exampleRG1/providers/Microsoft.Network/virtualNetworks/xxxxx/subnets/xxxxxx" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_subnet_network_security_group_association" for more information.

Commenting out the create_before_destroy in modules/azuread/credentials/password.tf , modules/azuread/service_principal_password/module.tf and modules/azuread/applications/module.tf helps to resolve the issue

The relevant code used is below. Just change the name of the NSG from my_appgw_nsg to another name, and do plan and apply to recreate the issue.

main.tf

terraform {
 required_providers {
   azurerm = {
     source = "hashicorp/azurerm"
   }
 }
}

provider "azurerm" {
 features {}
}

provider "azurerm" {
 features {}
 skip_provider_registration = true
 alias                      = "vhub"
}

module "caf" {
 # source = "aztfmod/caf/azurerm"
 # version = "~>5.5.0"
 # version = "5.7.0"
 source = "git::https://github.com/aztfmod/terraform-azurerm-caf.git?ref=int-5.7.0"

 providers = {
   azurerm.vhub = azurerm.vhub
 }

 global_settings = var.global_settings
 resource_groups = var.resource_groups
 networking = {
   vnets = var.vnets
   network_security_group_definition = var.network_security_group_definition
 }
}

terraform.auto.tfvars

global_settings = {
 default_region = "region1"
 regions = {
   region1 = "australiaeast"
 }
 passthrough = true
}

resource_groups = {
 ntwk = {
   name   = "exampleRG1"
   region = "region1"
 }
}

vnets = {
 earth = {
   resource_group_key = "ntwk"
   vnet = {
     name          = "earthvnet"
     address_space = ["10.0.0.0/16", "10.1.0.0/16"]
   }
   subnets = {

     appgw = {
       name    = "appgwsubnet"
       cidr    = ["10.0.0.0/24"]
       nsg_key = "appgw_nsg"
     }
   }
 }
}

network_security_group_definition = {
 appgw_nsg = {
   resource_group_key = "ntwk"
   name               = "my_appgw_nsg"

   nsg = [
     {
       name                         = "AllowIntraSubnetTraffic"
       priority                     = "4095"
       direction                    = "Inbound"
       access                       = "Allow"
       protocol                     = "*"
       source_port_range            = "*"
       destination_port_range       = "*"
       source_address_prefixes      = ["10.0.0.0/24"]
       destination_address_prefixes = ["10.0.0.0/24"]
     },
     {
       name                         = "DenyInterSubnetTraffic"
       priority                     = "4096"
       direction                    = "Inbound"
       access                       = "Deny"
       protocol                     = "*"
       source_port_range            = "*"
       destination_port_range       = "*"
       source_address_prefixes      = ["10.0.0.0/24", "10.1.0.0/24"]
       destination_address_prefixes = ["10.0.0.0/24", "10.1.0.0/24"]
     }
   ]
 }
}

variables.tf

variable "global_settings" {
 default = {}
}
variable "resource_groups" {
 default = {}
}
variable "vnets" {
 default = {}
}
variable "network_security_group_definition" {
 default = {}
}

Does this introduce a breaking change

  • YES
  • NO

Testing

@arnaudlh arnaudlh changed the title Create before destroy Create before destroy for azuread Sep 29, 2023
@arnaudlh arnaudlh added this to the 5.7.5 milestone Sep 29, 2023
@arnaudlh arnaudlh added the bug Something isn't working label Sep 29, 2023
Copy link
Member

@arnaudlh arnaudlh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arnaudlh arnaudlh merged commit 39bfa32 into aztfmod:main Sep 29, 2023
ThomasRasmussen pushed a commit to ThomasRasmussen/terraform-azurerm-caf that referenced this pull request Oct 9, 2023
* Update password.tf

* Update module.tf

* Update module.tf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants