﻿# 使用 Azure CLI 快速建立 Azure Web App 測試網站、資料庫與儲存體帳戶

架設一個測試網站，最常見的需求就是一個**網站**(Azure Web App)外加一個**資料庫**(Azure SQL Database)，如果有檔案儲存需求，頂多就再加一個**儲存體帳戶**(Azure Storage Account)就可以搞定。這篇文章我將分享如何透過 Azure CLI 快速建立網站、資料庫與儲存體帳戶，你完全可以不透過 Azure Portal 建立這些服務，使用 **無互動** (non-Interactive) 的方式就可以建立所有必要的 Azure 資源。

> 以下所有 Azure CLI 命令都是在 WSL 2 環境下執行。

### 登入 Azure CLI 並取得授權

1.  登入 Azure CLI
    
    ```bash
    az login --use-device-code
    ```
    
2.  切換到正確的 Azure 訂用帳戶
    
    ```bash
    az account set -s "Microsoft Azure Sponsorship"
    ```
    

### 建立資源群組

1.  建立資源群組
    
    ```bash
    az group create -l 'eastasia' -n 'myapp'
    ```
    
2.  建立資源群組標籤
    
    ```bash
    az group update -g 'myapp' \
        --set tags.申請人員='張OO' \
        --set tags.客戶名稱='台灣微軟' \
        --set tags.環境名稱='Testing' \
        --set tags.專案名稱='台灣微軟 - 神奇的系統'
    ```
    

### 建立 Azure Service 服務

1.  建立 Azure Service Plan 方案
    
    找出你想使用的價格等級 (pricing tier) ( `--sku`)
    
    ```bash
    az appservice plan create -h
    ```
    
    依據你想建立的 `--sku` 建立 Web App
    
    ```bash
    az appservice plan create -g 'myapp' -n 'myapp-plan' -l 'eastasia' --sku 'B1'
    ```
    
    > [az appservice plan](https://learn.microsoft.com/en-us/cli/azure/appservice/plan?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
2.  建立 Azure Web App 站台
    
    查出目前 Azure Web App 支援的 Runtimes 清單：
    
    ```bash
    az webapp list-runtimes
    ```
    
    ```log
    aspnet|V4.8
    aspnet|V3.5
    DOTNETCORE|2.1
    DOTNETCORE|3.1
    DOTNET|5.0
    DOTNET|6.0
    node|10.6
    node|10.10
    node|10.14
    node|12-lts
    node|14-lts
    php|7.3
    php|7.4
    python|3.6
    java|1.8|Tomcat|7.0
    java|1.8|Tomcat|8.5
    java|1.8|Tomcat|9.0
    java|1.8|Java SE|8
    java|7|Tomcat|7.0
    java|7|Tomcat|8.5
    java|7|Tomcat|9.0
    java|7|Java SE|8
    java|11|Tomcat|7.0
    java|11|Tomcat|8.5
    java|11|Tomcat|9.0
    java|11|Java SE|8
    ```
    
    依據不同的 Runtime 建立 Web App
    
    ```bash
    az webapp create -g 'myapp' -p 'myapp-plan' -n 'myapp' --runtime "DOTNET|5.0"
    ```
    
    取得 Azure Web App 站台完整屬性清單
    
    ```bash
    az webapp show -g 'myapp' -n 'myapp' -o json
    ```
    
    > [az webapp](https://learn.microsoft.com/en-us/cli/azure/webapp?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
3.  設定 Azure Web App 啟用 Always On 特性 (僅 `B1` 等級以上才支援 Always On 特性)
    
    ```bash
    az webapp config set -g 'myapp' -n 'myapp' --always-on true
    ```
    
    > [az webapp config](https://learn.microsoft.com/en-us/cli/azure/webapp/config?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
    取得 Azure Web App 預設域名
    
    ```bash
    az webapp show -g myapp -n myapp -o json | jq '.defaultHostName' -r
    ```
    
4.  設定 Azure Web App 應用程式參數
    
    列出目前有多少應用程式設定
    
    ```bash
    az webapp config appsettings list -g 'myapp' -n 'myapp'
    ```
    
    設定應用程式參數值
    
    ```bash
    az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'APPINSIGHTS_INSTRUMENTATIONKEY=2e938371-xxxx-xxxx-xxxx-cf43f85f91ac'
    az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=2e938371-xxxx-xxxx-xxxx-cf43f85f91ac;IngestionEndpoint=https://eastasia-0.in.applicationinsights.azure.com/'
    az webapp config appsettings set -g 'myapp' -n 'myapp' --settings 'WEBSITE_NODE_DEFAULT_VERSION=6.9.1'
    ```
    
    刪除應用程式參數
    
    ```bash
    az webapp config appsettings delete -g 'myapp' -n 'myapp' --setting-names 'WEBSITE_NODE_DEFAULT_VERSION' 'aaa'
    ```
    
    > [az webapp config appsettings](https://learn.microsoft.com/en-us/cli/azure/webapp/config/appsettings?view=azure-cli-latest)
    
5.  設定 Azure Web App 資料庫連接字串
    
    列出目前有多少資料庫連接字串
    
    ```bash
    az webapp config connection-string list -g 'myapp' -n 'myapp'
    ```
    
    設定資料庫連接字串
    
    ```bash
    az webapp config connection-string set -g 'myapp' -n 'myapp' -t 'SQLServer' --settings DefaultConnection='Server=ip.add.re.ss;Database=myDataBase;User Id=myUsername;Password=myPassword;MultipleActiveResultSets=true'
    ```
    
    刪除資料庫連接字串
    
    ```bash
    az webapp config connection-string delete -g 'myapp' -n 'myapp' --setting-names 'DefaultConnection'
    ```
    
    > [az webapp config connection-string](https://learn.microsoft.com/en-us/cli/azure/webapp/config/connection-string?view=azure-cli-latest)
    
6.  取得 Azure Web App 發行設定檔
    
    ```bash
    az webapp deployment list-publishing-profiles -g 'myapp' -n 'myapp' --xml > /mnt/g/myapp.pubxml
    ```
    
    > [az webapp deployment](https://learn.microsoft.com/en-us/cli/azure/webapp/deployment?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    

### 建立 Azure SQL Database 測試資料庫

1.  建立 Azure SQL Server 資料庫伺服器
    
    ```bash
    az sql server create -l 'eastasia' -g 'myapp' -n 'myapp-dbs' -u 'myapp' -p '9gZ8TCzsYzVf'
    ```
    
    取得 Azure SQL Server 完整屬性清單
    
    ```bash
    az sql server show -g 'myapp' -n 'myapp-dbs' -o json
    az sql server show -g 'myapp' -n 'myapp-dbs' -o json | jq '.fullyQualifiedDomainName' -r
    ```
    
    重新設定預設管理員密碼
    
    ```bash
    az sql server update -g 'myapp' -n 'myapp-dbs' -p '9gZ8TCzsYzVf'
    ```
    
    > [az sql server](https://learn.microsoft.com/en-us/cli/azure/sql/server?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
2.  列出 Azure SQL Database 所有資料庫等級 (Service Objective)
    
    ```bash
    az sql db list-editions --location eastasia --output table -a -d
    az sql db list-editions --location eastasia --output table --edition Basic
    az sql db list-editions --location eastasia --output table --edition Standard
    az sql db list-editions --location eastasia --output table --edition Premium
    az sql db list-editions --location eastasia --output table --edition GeneralPurpose
    ```
    
    > [az sql db list-editions](https://learn.microsoft.com/en-us/cli/azure/sql/db?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686#az_sql_db_list_editions)
    
3.  建立 Azure SQL Database 資料庫
    
    ```bash
    az sql db create -g 'myapp' -s 'myapp-dbs' -n 'myappDiscountDev' \
        --service-objective 'Basic' \
        --backup-storage-redundancy 'Local'
    ```
    
    > [az sql db](https://learn.microsoft.com/en-us/cli/azure/sql/db?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
4.  建立新的管理者登入帳號
    
    ```bash
    USE [myapp]
    GO
    CREATE LOGIN [myapp_admin] WITH PASSWORD=N'68SKYPPBGrws'
    GO
    
    USE [myapp]
    GO
    CREATE USER [myapp_admin] FOR LOGIN [myapp_admin]
    GO
    ALTER ROLE [db_owner] ADD MEMBER [myapp_admin]
    GO
    ```
    
5.  建立新的網站使用者登入帳號
    
    ```bash
    USE [master]
    GO
    CREATE LOGIN [myapp_user] WITH PASSWORD=N'Thw2DEahZPc9'
    GO
    
    USE [myapp]
    GO
    CREATE USER [myapp_user] FOR LOGIN [myapp_user]
    GO
    ALTER ROLE [db_owner] ADD MEMBER [myapp_user]
    GO
    
    CREATE USER [myapp_user] FOR LOGIN [myapp_user]
    ALTER ROLE [db_datareader] ADD MEMBER [myapp_user]
    ALTER ROLE [db_datawriter] ADD MEMBER [myapp_user]
    
    CREATE ROLE db_executor
    GRANT EXECUTE TO db_executor
    ALTER ROLE [db_executor] ADD MEMBER [myapp_user]
    ```
    

### 建立 Azure Storage 服務

1.  建立 Azure Storage 儲存體帳戶
    
    ```bash
    az storage account create -g 'myapp' -n 'myappstorage' -l 'eastasia' --sku Standard_LRS
    ```
    
    取得 Azure Storage Account 完整屬性清單
    
    ```bash
    az storage account show -g 'myapp' -n 'myappstorage' -o json
    ```
    
    > [az storage account](https://learn.microsoft.com/en-us/cli/azure/storage/account?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
2.  建立 Blob 容器
    
    ```bash
    az storage container create -n 'files' --account-name 'myappstorage' --public-access 'off'
    az storage container create -n 'files' --account-name 'myappstorage' --public-access 'blob'
    az storage container create -n 'files' --account-name 'myappstorage' --public-access 'conainer'
    ```
    
    > [az storage container](https://learn.microsoft.com/en-us/cli/azure/storage/container?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
3.  取得 Azure Storage 儲存體帳戶金鑰 (Account Key)
    
    ```bash
    az storage account keys list -g 'myapp' --account-name 'myappstorage' -o json
    ```
    
    > [az storage account keys](https://learn.microsoft.com/en-us/cli/azure/storage/account/keys?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
4.  建立 Azure Storage 特定容器的 SAS Token
    
    ```bash
    az storage blob generate-sas \
      --account-name 'myappstorage' \
      --account-key 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX+LSBqQtRk7Rhrd46RpidW25IbwSrg6D1AP9oL+/2W2gOht6XVBnBgb5bg==' \
      --container-name 'files' \
      --name '' \
      --permissions r \
      --expiry 2021-09-15 --https-only
    ```
    
    > [az storage account keys](https://learn.microsoft.com/en-us/cli/azure/storage/account/keys?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    
5.  建立 Azure Storage 特定容器與特定檔案的 SAS Token
    
    ```bash
    az storage blob generate-sas \
      --account-name 'myappstorage' \
      --account-key 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX+LSBqQtRk7Rhrd46RpidW25IbwSrg6D1AP9oL+/2W2gOht6XVBnBgb5bg==' \
      --container-name 'files' \
      --name 'folder1/abc.txt' \
      --permissions r \
      --expiry 2021-09-15 --https-only
    ```
    
    > [az storage account keys](https://learn.microsoft.com/en-us/cli/azure/storage/account/keys?view=azure-cli-latest&WT.mc_id=DT-MVP-4015686)
    

### 相關連結

-   [Azure Command-Line 介面 (CLI) 總覽 | Microsoft Docs](https://learn.microsoft.com/zh-tw/cli/azure/?WT.mc_id=DT-MVP-4015686)
-   [ConnectionStrings.com - Forgot that connection string? Get it here!](https://www.connectionstrings.com/)
-   [Azure CLI 2.0: Generate SAS Token For Blob In Azure Storage | Build5Nines](https://build5nines.com/azure-cli-2-0-generate-sas-token-for-blob-in-azure-storage/)
-   [Bicep 文件 | Microsoft Docs](https://learn.microsoft.com/zh-tw/azure/azure-resource-manager/bicep/?WT.mc_id=DT-MVP-4015686)
