共用方式為


如何在 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 入口網站 是以Web為基礎的介面,專為管理 Azure 資源而設計。 Azure 入口網站 功能:

  • 易於使用的 Web 型 UI,用於建立和管理 Azure 資源
  • 建立可設定的儀表板
  • 存取訂閱設定和帳單資訊

顯示 Azure 入口網站的螢幕快照。

VS Code Azure Tools 擴充功能套件

使用 Visual Studio Code 的開發人員會使用適用於 VS Code 的 Azure 工具延伸模組套件 ,直接在 VS Code 中管理 Azure 資源。 Azure 工具延伸模組套件可讓您:

  • 使用 Azure App Service 建立、管理程式碼,並將其部署至網站
  • 建立、瀏覽及查詢 Azure 資料庫
  • 直接在 VS Code 中建立、偵錯及部署 Azure Functions
  • 在 VS Code 中部署容器化應用程式

如需功能的完整清單,請參閱擴充功能的下載頁面。

螢幕快照,顯示已安裝 Azure Tools 延伸模組套件的 Visual Studio Code。

命令列工具

命令列工具提供效率、可重複性以及編寫重複任務腳本的能力。 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 是使用宣告式語法來部署 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 並進行版本管理。

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 可讓您透過對 RESTful 端點的 HTTP 要求來管理 Azure 資源。