Exercise - Update and delete a deployment stack

Completed

You're on sprint 2 for the new deposits application. You want to test updating the deployment stack by defining an Azure SQL database in the Bicep file. You want to verify that the stack is managing the newly defined resources.

In this exercise, you update the Bicep file you previously created to include new resources. You also update and delete the deployment stack that manages your resources.

  • Update the Bicep file to include an Azure SQL database.
  • Update the deployment stack to deploy the new database.
  • Validate the deployment stack's managed resources.
  • Delete the deployment stack and managed resources.
  • Validate the deletion of the deployment stack and the managed resources.

Update the Bicep file to include the Azure SQL database

  1. Open the main.bicep file in Visual Studio Code.

  2. Add the highlighted code to the parameters section of your file:

    // Parameters
    @description('The location for all resources.')
    param location string = 'eastus'
    
    @description('The name of the SQL database.')
    param sqlDatabaseName string = 'sqldb-${uniqueString(resourceGroup().id)}'
    
    @description('The password of the admin user.')
    param sqlServerAdminUserName string
    
    @description('The name of the admin user.')
    @secure()
    param sqlServerAdminPassword string
    
    @description('The name of the SQL server.')
    param sqlServerName string = 'sql-${uniqueString(resourceGroup().id)}'
    
    @description('The name of the web application.')
    param webApplicationName string = 'webapp-${uniqueString(resourceGroup().id)}'
    
    // Variables
    @description('The name of the app service plan.')
    var appServicePlanName = 'plan-deposits'
    
  3. Add the following code to the bottom of the file:

    // Resource - SQL Server
    resource sqlServer 'Microsoft.Sql/servers@2021-11-01' ={
      name: sqlServerName
      location: location
      properties: {
        administratorLogin: sqlServerAdminUserName
        administratorLoginPassword: sqlServerAdminPassword
      }
    }
    
    // Resource - SQL Database
    resource sqlServerDatabase 'Microsoft.Sql/servers/databases@2021-11-01' = {
      parent: sqlServer
      name: sqlDatabaseName
      location: location
      sku: {
        name: 'Standard'
        tier: 'Standard'
      }
    }
    
  4. Save the changes to the file.

Update the deployment stack

With the Bicep file updated, we want to update the deployment stack so that the new Azure SQL database is deployed to the environment as a managed resource.

  1. To update the deployment stack, run the following command from the terminal in Visual Studio Code.

    az stack group create \
        --name stack-deposits \
        --resource-group rg-depositsApplication \
        --template-file ./main.bicep \
        --action-on-unmanage deleteAll \
        --deny-settings-mode none
    
  2. You receive a message stating that the stack already exists in the current subscription. If the value of the action on unmanage parameter changed, the warning alerts you of the new values. Press y, followed by 'Enter`.

    A screenshot showing the Azure CLI command line warning that the deployment stack already exists.

  3. You're prompted to enter a value for sqlServerAdminUserName. Create a name for the SQL server administrator, followed by 'Enter.'

    A screenshot showing the Azure CLI command line prompting you to enter an SQL server administrator user name.

  4. You're prompted to enter a value for sqlServerAdminPassword. Create a complex password for the SQL server administrator, followed by 'Enter.'

    A screenshot showing the Azure CLI command line prompting you to enter an SQL server administrator password.

Wait for the update operation to complete before moving on to the next task.

With the Bicep file updated, we want to update the deployment stack so that the new Azure SQL database is deployed to the environment as a managed resource.

  1. To update the deployment stack, run the following command from the terminal in Visual Studio Code.

    Set-AzResourceGroupDeploymentStack `
        -Name stack-deposits `
        -ResourceGroupName rg-depositsApplication `
        -TemplateFile ./main.bicep `
        -ActionOnUnmanage DeleteAll `
        -DenySettingsMode none
    
  2. You're prompted to enter a value for sqlServerAdminUserName. Create a name for the SQL server administrator, followed by 'Enter.'

    A screenshot showing the Azure PowerShell command line prompting you to enter an SQL server administrator user name.

  3. You're prompted to enter a value for sqlServerAdminPassword. Create a complex password for the SQL server administrator, followed by 'Enter.'

    A screenshot showing the Azure PowerShell command line prompting you to enter an SQL server administrator password.

  4. Wait for the update operation to complete before moving on to the next task.

Verify the update to the deployment stack and the managed resources

With the update complete, we want to validate that the deployment stack is managing the Azure SQL database. To view the configuration of the deployment stack, run the following command from the terminal in Visual Studio Code.

az stack group show \
    --resource-group rg-depositsApplication \
    --name stack-deposits

The result should appear familiar to the following output:

{
  "actionOnUnmanage": {
    "managementGroups": "detach",
    "resourceGroups": "detach",
    "resources": "detach"
  },
  "bypassStackOutOfSyncError": null,
  "correlationId": ".",
  "debugSetting": null,
  "deletedResources": [],
  "denySettings": {
    "applyToChildScopes": false,
    "excludedActions": null,
    "excludedPrincipals": null,
    "mode": "none"
  },
  "deploymentId": "/subscriptions/././rg-depositsApplication/./Microsoft.Resources/deployments/stack-deposits",
  "deploymentScope": null,
  "description": null,
  "detachedResources": [],
  "duration": "PT2M53.2734284S",
  "error": null,
  "failedResources": [],
  "id": "/subscriptions/././rg-depositsApplication/./Microsoft.Resources/deploymentStacks/stack-deposits",
  "location": null,
  "name": "stack-deposits",
  "outputs": null,
  "parameters": {
    "sqlServerAdminPassword": {
      "reference": null,
      "type": "securestring",
      "value": ""
    },
    "sqlServerAdminUserName": {
      "reference": null,
      "type": "string",
      "value": "sqladmin"
    }
  },
  "parametersLink": null,
  "provisioningState": "succeeded",
  "resourceGroup": "rg-depositsApplication",
  "resources": [
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq/databases/sqldb-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/././rg-depositsApplication/././serverfarms/plan-deposits",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    },
    {
      "denyStatus": "none",
      "id": "/subscriptions/./resourceGroups/rg-depositsApplication/././sites/webapp-eque2jlrboltq",
      "resourceGroup": "rg-depositsApplication",
      "status": "managed"
    }
  ],
  "systemData": {
    "createdAt": "2024-01-01T00:00:01.000000+00:00",
    "createdBy": "depositsapplication@contoso.com",
    "createdByType": "User",
    "lastModifiedAt": "2024-01-01T00:00:01.000000+00:00",
    "lastModifiedBy": "depositsapplication@contoso.com",
    "lastModifiedByType": "User"
  },
  "tags": {},
  "template": null,
  "templateLink": null,
  "type": "Microsoft.Resources/deploymentStacks"
}

Take notice of the resources section of the output. We now see the SQL server and SQL database listed as managed resources.

With the update complete, we want to validate that the deployment stack is managing the Azure SQL database. To view the configuration of the deployment stack, run the following command from the terminal in Visual Studio Code.

Get-AzResourceGroupDeploymentStack `
    -ResourceGroupName rg-depositsApplication `
    -Name stack-deposits

The result should appear familiar to the following output:

Id                            : /subscriptions/././rg-depositsApplication/././deploymentStacks/stack-deposits
Name                          : stack-deposits
ProvisioningState             : succeeded
resourcesCleanupAction        : detach
resourceGroupsCleanupAction   : detach
managementGroupsCleanupAction : detach
CorrelationId                 : .
DenySettingsMode              : none
CreationTime(UTC)             : 1/01/2024 0:00:01 AM
DeploymentId                  : /subscriptions/././rg-depositsApplication/././deployments/stack-deposits
Resources                     : /subscriptions/././rg-depositsApplication/././serverfarms/plan-deposits
                                /subscriptions/././rg-depositsApplication/././sites/webapp-eque2jlrboltq
                                /subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq
                                /subscriptions/././rg-depositsApplication/././servers/sql-eque2jlrboltq/databases/sqldb-eque2jlrboltq
Parameters                    : 
                                Name                      Type                       Value     
                                ========================  =========================  ==========
                                sqlServerAdminUserName    string                     "sqladmin"
                                sqlServerAdminPassword    securestring               ""

Take notice of the resources section of the output. We now see the SQL server and SQL database listed as managed resources.

Delete the deployment stack

  1. To delete the deployment stack and its managed resources, run the following command from the terminal in Visual Studio Code.

    az stack group delete \
        --name stack-deposits \
        --resource-group rg-depositsApplication \
        --action-on-unmanage deleteAll
    
  2. You're asked to confirm if you would like to delete the stack and the specified resources.

    A screenshot showing a command line confirmation to delete the deployment stack using Azure CLI.

  3. After the delete operation completes, open the Azure portal and verify that the deployment stack and its resources are removed.

  4. To delete the resource group used in these exercises, run the following command from the terminal in Visual Studio Code.

    az group delete \
        -name rg-depositsApplication
    
  5. You're asked to confirm if you would like to remove the resource group. Press 'Y', followed by 'Enter.'

    A screenshot showing a command line confirmation to delete the resource group using Azure CLI.

  1. To delete the deployment stack and its managed resources, run the following command from the terminal in Visual Studio Code.

    Remove-AzResourceGroupDeploymentStack `
        -Name stack-deposits `
        -ResourceGroupName rg-depositsApplication `
        -ActionOnUnmanage DeleteAll
    
  2. You're asked to confirm if you would like to delete the stack and the specified resources.

    A screenshot showing a command line confirmation to delete the deployment stack using Azure PowerShell.

  3. After the delete operation completes, open the Azure portal and verify that the deployment stack and its resources are removed.

  4. To delete the resource group used in these exercises, run the following command from the terminal in Visual Studio Code.

    Remove-AzResourceGroup `
        -Name rg-depositsApplication
    
  5. You're asked to confirm if you would like to remove the resource group. Press 'Y', followed by 'Enter.'

    A screenshot showing a command line confirmation to delete the resource group using Azure PowerShell.