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

Microsoft.Network/virtualNetworks/subnets asking to delete on redeploy #2040

Closed
Scot-Bernard opened this issue Feb 7, 2024 · 4 comments
Closed

Comments

@Scot-Bernard
Copy link

Bicep version
Bicep CLI version 0.24.24 (5646341b0c)

Describe the bug
When declaring a subnet resource separately, referring the vnet as a parent, it works on the first deployment, but gives an error on deleting it on a subsequent deployment when the resource is already created and other resource depends on it, in my case a private endpoint network interface. This doesn't happen if the subnet is declared inside the VNET.

It's expected for the deployment process not trying to delete the subnet resource that hasn't changed on its template definition.

To Reproduce
Declare a vnet and a subnet separately, putting the vnet as parent of the subnet.
Declare a private endpoint pointing to the subnet.
run the az deploy command, the subnet is created.
run the az deploy again, the following error message displays:

{
  "code": "InUseSubnetCannotBeDeleted",
  "message": "Subnet <name> is in use by /subscriptions/<id>/resourceGroups/<name>/providers/Microsoft.Network/networkInterfaces/<private endpoint nic>/ipConfigurations/privateEndpointIpConfig.<id> and cannot be deleted. In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.",
  "details": []
}

Note that the link "aka.ms/deletesubnet" points to a page which mentions this kind of error but in another context.

Additional context
Why do I want to declare the subnet separately may you ask, to output a property referring to it directly avoiding using the vnet subnets array that can't be accessed by name or using a function.

@anthony-c-martin anthony-c-martin changed the title [isue template] Microsoft.Network/virtualNetworks/subnets asking to delete on redeploy Microsoft.Network/virtualNetworks/subnets asking to delete on redeploy Feb 8, 2024
@anthony-c-martin anthony-c-martin transferred this issue from Azure/bicep Feb 8, 2024
@jarz
Copy link

jarz commented Feb 9, 2024

@Scot-Bernard: This is covered in #1687. The third solution in this comment should address your needs.

From that comment...

resource vnet 'Microsoft.Network/virtualNetworks@2020-06-01' = {
  name: 'vnet1'
  location: resourceGroup().location
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'subnet1'
        properties: {
          addressPrefix: '10.0.0.1/24'
        }
      }
    ]
  }

  // pattern 3 extra resource block inside of parent
  resource subnet1 'subnets' existing = {
    name: 'subnet1'
  }
} // inside of vnet resource

resource networkInterface 'Microsoft.Network/networkInterfaces@2020-11-01' = {
  name: 'name'
  location: resourceGroup().location
  properties: {
    ipConfigurations: [
      {
        name: 'name'
        properties: {
          privateIPAllocationMethod: 'Dynamic'
          subnet: {
            id: vnet::subnet1.id  // pattern 3 reference for property e.g. id
          }
        }
      }
    ]
  }
}

Use the vnet::subnet1 syntax to reference the created subnets.

@Mike-E-angelo
Copy link

@Scot-Bernard: This is covered in #1687. The third solution in #1687 (comment) should address your needs.

Curious if there is something like this for #2042 ... would like to deploy without erasing all my websites each and every time. 🤞🙏

@Scot-Bernard
Copy link
Author

@Scot-Bernard: This is covered in #1687. The third solution in this comment should address your needs.

Thank you @jarz, that will do to refer the subnet by name while avoiding this issue. As in the same comment is mentioned, I'd like a more integrated support on bicep notation to easily refer to sub-resource array items by name, but that'll be another topic.

@Scot-Bernard
Copy link
Author

I consider this issue can be closed with the provided solution, thank you again.

@github-project-automation github-project-automation bot moved this from Todo to Done in Bicep Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants