分享方式:


快速入門:使用 Bicep 建立及部署部署堆疊

本快速入門描述如何建立部署堆疊 (部分機器翻譯)。

必要條件

建立 Bicep 檔案

建立 Bicep 檔案以建立儲存體帳戶和虛擬網路。

param resourceGroupLocation string = resourceGroup().location
param storageAccountName string = 'store${uniqueString(resourceGroup().id)}'
param vnetName string = 'vnet${uniqueString(resourceGroup().id)}'

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

resource virtualNetwork 'Microsoft.Network/virtualNetworks@2023-11-01' = {
  name: vnetName
  location: resourceGroupLocation
  properties: {
    addressSpace: {
      addressPrefixes: [
        '10.0.0.0/16'
      ]
    }
    subnets: [
      {
        name: 'Subnet-1'
        properties: {
          addressPrefix: '10.0.0.0/24'
        }
      }
      {
        name: 'Subnet-2'
        properties: {
          addressPrefix: '10.0.1.0/24'
        }
      }
    ]
  }
}

將 Bicep 檔案另存為 main.bicep

建立部署堆疊

在本快速入門中,您會在資源群組範圍建立部署堆疊。 您也可以在訂用帳戶範圍或管理群組範圍建立部署堆疊。 如需詳細資訊,請參閱建立部署堆疊 (部分機器翻譯)。

az group create \
  --name 'demoRg' \
  --location 'centralus'

az stack group create \
  --name demoStack \
  --resource-group 'demoRg' \
  --template-file './main.bicep' \
  --action-on-unmanage 'detachAll' \
  --deny-settings-mode 'none'

如需 和的詳細資訊action-on-unmanage,請參閱部署堆疊deny-setting-mode

檢查部署

列出在資源群組層級部署的部署堆疊:

az stack group show \
  --resource-group 'demoRg' \
  --name 'demoStack'

輸出會顯示兩個受控資源:一個儲存體帳戶和一個虛擬網路:

{
  "actionOnUnmanage": {
    "managementGroups": "detach",
    "resourceGroups": "detach",
    "resources": "detach"
  },
  "debugSetting": null,
  "deletedResources": [],
  "denySettings": {
    "applyToChildScopes": false,
    "excludedActions": null,
    "excludedPrincipals": null,
    "mode": "none"
  },
  "deploymentId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Resources/deployments/demoStack-24051714epybc",
  "deploymentScope": null,
  "description": null,
  "detachedResources": [],
  "duration": "PT32.5330364S",
  "error": null,
  "failedResources": [],
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Resources/deploymentStacks/demoStack",
  "location": null,
  "name": "demoStack",
  "outputs": null,
  "parameters": {},
  "parametersLink": null,
  "provisioningState": "succeeded",
  "resourceGroup": "demoRg",
  "resources": [
    {
      "denyStatus": "none",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Network/virtualNetworks/vnetthmimleef5fwk",
      "resourceGroup": "demoRg",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Storage/storageAccounts/storethmimleef5fwk",
      "resourceGroup": "demoRg",
      "status": "managed"
    }
  ],
  "systemData": {
    "createdAt": "2024-05-17T14:50:18.382948+00:00",
    "createdBy": "johndoe@contoso.com",
    "createdByType": "User",
    "lastModifiedAt": "2024-05-17T14:50:18.382948+00:00",
    "lastModifiedBy": "johndoe@contoso.com",
    "lastModifiedByType": "User"
  },
  "tags": {},
  "template": null,
  "templateLink": null,
  "type": "Microsoft.Resources/deploymentStacks"
}

您也可以列出部署堆疊中的受控資源來驗證部署:

az stack group show \
  --name 'demoStack' \
  --resource-group 'demoRg' \
  --output 'json'

輸出如下:

{
  "actionOnUnmanage": {
    "managementGroups": "detach",
    "resourceGroups": "detach",
    "resources": "detach"
  },
  "debugSetting": null,
  "deletedResources": [],
  "denySettings": {
    "applyToChildScopes": false,
    "excludedActions": null,
    "excludedPrincipals": null,
    "mode": "none"
  },
  "deploymentId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Resources/deployments/demoStack-24051714epybc",
  "deploymentScope": null,
  "description": null,
  "detachedResources": [],
  "duration": "PT32.5330364S",
  "error": null,
  "failedResources": [],
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Resources/deploymentStacks/demoStack",
  "location": null,
  "name": "demoStack",
  "outputs": null,
  "parameters": {},
  "parametersLink": null,
  "provisioningState": "succeeded",
  "resourceGroup": "demoRg",
  "resources": [
    {
      "denyStatus": "none",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Network/virtualNetworks/vnetthmimleef5fwk",
      "resourceGroup": "demoRg",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/demoRg/providers/Microsoft.Storage/storageAccounts/storethmimleef5fwk",
      "resourceGroup": "demoRg",
      "status": "managed"
    }
  ],
  "systemData": {
    "createdAt": "2024-05-17T14:50:18.382948+00:00",
    "createdBy": "johndoe@contoso.com",
    "createdByType": "User",
    "lastModifiedAt": "2024-05-17T14:50:18.382948+00:00",
    "lastModifiedBy": "johndoe@contoso.com",
    "lastModifiedByType": "User"
  },
  "tags": {},
  "template": null,
  "templateLink": null,
  "type": "Microsoft.Resources/deploymentStacks"
}

建立堆疊之後,您可以透過 Azure 入口網站存取及檢視堆疊本身和與其相關聯的受控資源。 瀏覽至已部署堆疊的資源群組,您可以存取所有相關資訊與設定。

Azure 入口網站 中部署堆疊的螢幕快照。

更新部署堆疊

若要更新部署堆疊,您可以修改底層 Bicep 檔案,然後重新執行建立部署堆疊命令。

編輯 main.bicep,以將 SKU 名稱從 Standard_LRS 變更為 Standard_GRS

執行以下命令:

az stack group create \
  --name 'demoStack' \
  --resource-group 'demoRg' \
  --template-file './main.bicep' \
  --action-on-unmanage 'detachAll' \
  --deny-settings-mode 'none'

從 Azure 入口網站,檢查儲存體帳戶的屬性以確認變更。

使用相同的方法,您可以將資源新增至部署堆疊,或從部署堆疊移除受控資源。 如需詳細資訊,請參閱將資源新增至部署堆疊 (部分機器翻譯) 和從部署堆疊中刪除受控資源 (部分機器翻譯)。

刪除部署堆疊

刪除部署堆疊,然後刪除受控資源:

az stack group delete \
  --name 'demoStack' \
  --resource-group 'demoRg' \
  --action-on-unmanage 'deleteAll'

若要刪除部署堆疊,但保留受控資源:

az stack group delete \
  --name 'demoStack' \
  --resource-group 'demoRg' \
  --action-on-unmanage 'detachAll'

如需詳細資訊,請參閱刪除部署堆疊 (部分機器翻譯)。

移除命令只會移除受控資源和受控資源群組。 您仍須負責刪除未由部署堆疊管理的資源群組。

清除資源

刪除非受控資源群組。

az group delete \
  --name 'demoRg'

下一步

部署堆疊 (部分機器翻譯)