gpt4 book ai didi

Azure Bicep doesn't delete storage container(Azure Bicep不删除存储容器)

转载 作者:bug小助手 更新时间:2023-10-27 19:59:06 26 4
gpt4 key购买 nike



I have an Azure Bicep template that deploys a storage account and a container. However, when I rename the container or comment out the container resource in the template, the delete operation does not get triggered. Instead, it creates a new container and does not delete the old one. Any ideas why this happens?

我有一个Azure Bicep模板,它部署了一个存储帐户和一个容器。但是,当我在模板中重命名容器或注释掉容器资源时,删除操作不会被触发。相反,它会创建一个新容器,而不会删除旧容器。你知道为什么会发生这种事吗?



Note: I use Mode Complete to deploy



I have a main.bicep file containing

我有一个main.biep文件,其中包含


param location string = resourceGroup().location

param storageAccountName string = 'test${uniqueString('newstring')}'
param storageContainerName string = 'testcontainer'

module storagAccounts '../resources/storage/storageAccounts.bicep' = {
name: 'storageAccounts'
params: {
location: location
storageAccountName: storageAccountName
}
}

module blobServices '../resources/storage/blobServices.bicep' = {
name: 'blobServices'
params: {
storageAccountName: storagAccounts.outputs.storageAccountName
}
}

module blobContainer '../resources/storage/containers.bicep' = {
name: 'blobContainer'
params: {
containerName: storageContainerName
blobServicesName: blobServices.outputs.blobServicesName
storageAccountName: storagAccounts.outputs.storageAccountName
}
}

I have a storageAccounts.bicep containing

我有一个storageAccount。二头肌包含


targetScope = 'resourceGroup'

@minLength(3)
@maxLength(24)
param storageAccountName string

param location string

resource storageAccount 'Microsoft.Storage/storageAccounts@2021-02-01' = {
name: storageAccountName
location: location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}

tags: {
environment: 'test'
}


}

output storageAccountId string = storageAccount.id
output storageAccountName string = storageAccountName

a blobServices.bicep

一个blobServices.biep


param storageAccountName string 
param blobServicesName string = 'default'

resource blobServices 'Microsoft.Storage/storageAccounts/blobServices@2023-01-01' = {
name: '${storageAccountName}/${blobServicesName}'
}

output blobServicesName string = blobServicesName

and a container.bicep containing

还有一个集装箱。二头肌,里面装着


param storageAccountName string
param blobServicesName string
param containerName string

resource storageContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2023-01-01' = {
name: '${storageAccountName}/${blobServicesName}/${containerName}'
properties: {
publicAccess: 'None'
}
}

output storageContainerName string = storageContainer.name

To deploy I use a PowerShell script

要进行部署,我使用PowerShell脚本


Get-AzResourceGroupDeploymentWhatIfResult -Mode Complete -Location westeurope -TemplateFile modules/main.bicep -ResourceGroup testResourceGroup


New-AzResourceGroupDeployment -Name deployment1 -Mode Complete -Location westeurope -TemplateFile modules/main.bicep -ResourceGroup testResourceGroup -Force

更多回答
优秀答案推荐


According to the documentation, this is the expected behavior for storage account:

根据文档,这是存储帐户的预期行为:



Resource types may handle complete mode deletions differently. Parent resources are automatically deleted when not defined in a template deployed in complete mode. Also, child resources are automatically deleted when the parent isn't included in the template. However, some child resources are deleted when not defined in the template but other child resources aren't deleted. For a list of how resource types handle deletion, see Deletion of Azure resources for complete mode deployments.


For example, if your resource group contains a storage account (Microsoft.Storage/storageAccounts resource type) and a blob service (Microsoft.Storage/storageAccounts/blobServices resource type), the storage account is the parent resource for the blob service. If you deploy with complete mode and don't include the storage account in your template, both the storage account and the blob service are deleted. If you include the storage account in your template but don't include the blob service, the blob service isn't deleted.



Additional information can be found here:

有关更多信息,请访问此处:




Thomas' answer is correct. However, to get the functionality of tracking managed resources in the bicep template, DeploymentStacks is the way to go not the Complete Mode.

托马斯的回答是正确的。然而,要获得在BUBEPP模板中跟踪托管资源的功能,DeploymentStack是可行的,而不是Complete模式。


using New-AzResourceGroupDeploymentStack -Name deploy -ResourceGroupName testResourceGroup -TemplateFile modules/main.bicep -DenySettingsMode none -DeleteResources -Force deletes the resources that are not defined in the template.

使用New-AzResourceGroupDeploymentStack-Name Deploy-ResourceGroupName testResourceGroup-TemplateFileModels/main.biep-DenySettingsMode None-DeleteResources-Force删除模板中未定义的资源。


Note that the switch -DeleteResources must be added for this behavior to occur. And the name of the DeploymentStack must also be the same

请注意,必须添加开关-DeleteResources才能发生此行为。DeploymentStack的名称也必须相同


更多回答

Thanks for the reply, but then the only way to delete those resources is manually or using hard coded scripts which is not a good solution imo. I am not sure then if bicep is a proper iac tool.

谢谢你的回复,但删除这些资源的唯一方法是手动或使用硬编码脚本,这不是一个好的解决方案。我不确定二头肌是否是合适的IAC工具。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com