如何? 在 Azure 中建立和管理資源?

Azure 提供各種工具來建立和管理應用程式所使用的 Azure 資源。

不同的工具是設計來支援不同的使用案例,而大部分的 Azure 開發人員會根據需要執行的作業,使用不同的工具組合。 例如,您可能會:

  • 在為新應用程式建立 Azure 資源的原型時,請使用適用於 VS Code 的 Azure 入口網站 或 Azure Tools 擴充功能等 GUI 工具。 GUI 工具會引導您完成建立新服務的程式,並可讓您使用下拉功能表和其他圖形元素來檢閱並選取服務的選項。

  • 使用 Azure CLI 或 Azure PowerShell 撰寫腳本,以自動化一般工作。 例如,您可以建立腳本,針對由 Azure App 服務、資料庫和 Blob 記憶體組成的新 Web 應用程式建立基本開發環境。 撰寫腳本可確保每次建立資源的方式都相同,而且執行速度比按兩下UI更快。

  • 使用基礎結構即程序代碼 (IaC) 工具來以宣告方式部署和管理 Azure 資源。 Terraform、Ansible 或 Bicep 之類的工具可讓您在宣告式語法中編纂解決方案所需的 Azure 資源,確保跨環境部署 Azure 資源,並防止環境漂移。

Azure 入口網站

Azure 入口網站 是以Web為基礎的介面,專為管理 Azure 資源而設計。 Azure 入口網站 功能:

  • 輕鬆使用的 Web 型 UI,可用來建立和管理 Azure 資源
  • 建立可設定儀錶板的能力
  • 存取訂用帳戶設定和帳單資訊

A screenshot showing the Azure portal.

VS Code Azure Tools 擴充功能套件

使用 Visual Studio Code開發人員可以使用適用於 VS Code 的 Azure 工具擴充套件,直接從 VS Code 管理 Azure 資源。 使用 Azure 工具擴充功能套件可以:

  • 使用 Azure App 服務 建立、管理及部署程式代碼至網站。
  • 建立、瀏覽及查詢 Azure 資料庫
  • 直接從 VS Code 建立、偵錯及部署 Azure Functions
  • 從 VS Code 部署容器化應用程式

A screenshot showing Visual Studio Code with the Azure Tools extension pack installed.

命令列工具

命令行工具提供效率、可重複性,以及編寫週期性工作腳本的能力。 Azure 提供兩種不同的命令行工具可供選擇。 Azure CLI 和 Azure PowerShell 的功能相同。 您只需要選取並使用最適合您個別工作流程的工具。

Azure CLI

Azure CLI 是在 Windows、Linux 和 macOS 上執行的跨平臺命令行工具。 Azure CLI:

  • 提供簡潔且有效率的語法來管理 Azure 資源。
  • 將結果輸出為 JSON(預設)。 結果也可以格式化為 YAML、ASCII 資料表或不含索引鍵的索引標籤隔值。
  • 提供透過使用 JMESPath 查詢來查詢和塑造輸出的能力。

Azure CLI 命令可以輕鬆地併入熱門的腳本語言,例如 Bash ,讓您能夠編寫一般工作的腳本。

LOCATION='eastus'                                        
RESOURCE_GROUP_NAME='msdocs-expressjs-mongodb-tutorial'

WEB_APP_NAME='msdocs-expressjs-mongodb-123'
APP_SERVICE_PLAN_NAME='msdocs-expressjs-mongodb-plan-123'    
RUNTIME='NODE|14-lts'

# Create a resource group
az group create \
    --location $LOCATION \
    --name $RESOURCE_GROUP_NAME

# Create an app service plan
az appservice plan create \
    --name $APP_SERVICE_PLAN_NAME \
    --resource-group $RESOURCE_GROUP_NAME \
    --sku B1 \
    --is-linux

# Create the web app in the app service
az webapp create \
    --name $WEB_APP_NAME \
    --runtime $RUNTIME \
    --plan $APP_SERVICE_PLAN_NAME \
    --resource-group $RESOURCE_GROUP_NAME 

Azure PowerShell

Azure PowerShell 是一組 Cmdlet,可直接從 PowerShell 管理 Azure 資源。 Azure PowerShell 會安裝為 PowerShell 模組,且可在 Windows、macOS 和 Linux 等所有平臺上使用 PowerShell 7.0.6 LTS 和 PowerShell 7.1.3 或更高版本。 它也與 Windows PowerShell 5.1 相容。

Azure PowerShell 與 PowerShell 語言緊密整合。 命令會遵循動詞名詞格式,並將數據當做 PowerShell 物件傳回。 如果您已經熟悉 PowerShell 腳本,Azure PowerShell 是自然選擇。

$location = 'eastus'
$resourceGroupName = 'msdocs-blob-storage-demo-azps'
$storageAccountName = 'stblobstoragedemo999'

# Create a resource group
New-AzResourceGroup `
    -Location $location `
    -Name $resourceGroupName

# Create the storage account
New-AzStorageAccount `
    -Name $storageAccountName `
    -ResourceGroupName $resourceGroupName `
    -Location $location `
    -SkuName Standard_LRS

如需在 Azure CLI 與 Azure PowerShell 之間選擇的詳細資訊,請參閱選擇正確的命令行工具一文

基礎結構即程式代碼工具

基礎結構即程式代碼 是透過宣告式組態檔管理和布建資源的程式。 基礎結構即程式代碼工具會使用宣告式結束狀態規格來保證每次建立和設定一組資源的方式相同。 此外,大部分基礎結構即程式碼工具會監視資源,以確保它們仍以所需的狀態進行設定。

針對自動化、重複且可靠的基礎結構部署,Azure 支援 各種基礎結構即程式代碼工具。

Bicep

Bicep 是使用宣告式語法來部署 Azure 資源的特定領域語言 (DSL)。 其提供簡潔的語法、可靠的類型安全,並支援程式碼重複使用。

param location string = resourceGroup().location
param storageAccountName string = 'toylaunch${uniqueString(resourceGroup().id)}'

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

Terraform

Hashicorp Terraform 是開放原始碼工具,可用來佈建和管理雲端基礎結構。 它會在組態檔中制訂基礎結構,以說明雲端資源的拓撲。 Terraform CLI 提供簡單的機制,將組態檔部署至 Azure,並將組態檔版本設定為 Azure。

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "main" {
  name     = "${var.prefix}-resources"
  location = var.location
}

resource "azurerm_app_service_plan" "main" {
  name                = "${var.prefix}-asp"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
  kind                = "Linux"
  reserved            = true

  sku {
    tier = "Standard"
    size = "S1"
  }
}

resource "azurerm_app_service" "main" {
  name                = "${var.prefix}-appservice"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name
  app_service_plan_id = azurerm_app_service_plan.main.id

  site_config {
    linux_fx_version = "NODE|10.14"
  }
}

Ansible

Ansible 是一項開放原始碼產品,可自動佈建雲端、進行組態管理和應用程式部署。 您可以使用 Ansible 佈建虛擬機、容器和網路,以及完整的雲端基礎結構。 此外,Ansible 也可讓您自動化環境中資源的部署和設定。

- hosts: localhost
  connection: local
  vars:
    resource_group: myResourceGroup
    webapp_name: myfirstWebApp
    plan_name: myAppServicePlan
    location: eastus
  tasks:
    - name: Create a resource group
      azure_rm_resourcegroup:
        name: "{{ resource_group }}"
        location: "{{ location }}"

    - name: Create App Service on Linux with Java Runtime
      azure_rm_webapp:
        resource_group: "{{ resource_group }}"
        name: "{{ webapp_name }}"
        plan:
          resource_group: "{{ resource_group }}"
          name: "{{ plan_name }}"
          is_linux: true
          sku: S1
          number_of_workers: 1
        frameworks:
          - name: "java"
            version: "8"
            settings:
              java_container: tomcat
              java_container_version: 8.5

Azure SDK 和 REST API

您也可以從程式代碼以程序設計方式建立 Azure 資源。 這可讓您撰寫可動態布建 Azure 資源以回應使用者要求的應用程式。 Azure SDK 會在 .NET、Go、Java、JavaScript 和 Python 中提供資源管理套件,以允許直接在程式代碼中建立和管理 Azure 資源。 或者,Azure REST API 可讓 Azure 資源透過對 RESTful 端點的 HTTP 要求來管理。