閱讀英文版本

分享方式:


快速入門:使用 Terraform 在 Azure 儲存體上部署靜態網站

在本快速入門中,您會了解如何部署已啟用靜態網站裝載的 Azure 儲存體帳戶

Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。

在本文中,您將學會如何:

必要條件

實作 Terraform 程式碼

  1. 建立目錄,然後在目錄中測試範例 Terraform 程式碼,並將其設為目前的目錄。

  2. 建立名為 providers.tf 的檔案,並插入下列程式碼:

    Terraform
    terraform {
      required_version = ">=1.0"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
    
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  3. 建立名為 main.tf 的檔案,並插入下列程式碼:

    Terraform
    data "azurerm_client_config" "current" {}
    
    # Generate random resource group name
    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    # Generate random value for the storage account name
    resource "random_string" "storage_account_name" {
      length  = 8
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_storage_account" "storage_account" {
      resource_group_name = azurerm_resource_group.rg.name
      location            = azurerm_resource_group.rg.location
    
      name = random_string.storage_account_name.result
    
      account_tier             = "Standard"
      account_replication_type = "LRS"
      account_kind             = "StorageV2"
    
      static_website {
        index_document = "index.html"
      }
    }
    
    resource "azurerm_storage_blob" "example" {
      name                   = "index.html"
      storage_account_name   = azurerm_storage_account.storage_account.name
      storage_container_name = "$web"
      type                   = "Block"
      content_type           = "text/html"
      source                 = "index.html"
    }
    
  4. 建立名為 variables.tf 的檔案,並插入下列程式碼:

    Terraform
    variable "resource_group_location" {
      type        = string
      description = "Location of the resource group."
      default     = "eastus"
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
      default     = "rg"
    }
    
  5. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    Terraform
    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "storage_account_name" {
      value = azurerm_storage_account.storage_account.name
    }
    
    output "primary_web_host" {
      value = azurerm_storage_account.storage_account.primary_web_host
    }
    
  6. 建立名為 index.html 的檔案,並插入下列程式碼:

    HTML
    <h1> This is a static website example <h1>
    

初始化 Terraform

執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。

主控台
terraform init -upgrade

重點︰

  • -upgrade 參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。

建立 Terraform 執行計畫

執行 terraform plan 以建立執行計畫。

主控台
terraform plan -out main.tfplan

重點︰

  • terraform plan 命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。
  • 選用的 -out 參數可讓您指定計畫的輸出檔。 使用 -out 參數可確保您所檢閱的方案就是所套用的方案。

套用 Terraform 執行計畫

執行 terraform apply 將執行計畫套用至您的雲端基礎結構。

主控台
terraform apply main.tfplan

重點︰

  • 範例 terraform apply 命令假設您之前已執行過 terraform plan -out main.tfplan
  • 如果您為 -out 參數指定了不同的檔案名稱,請在呼叫 terraform apply 時使用該檔案名稱。
  • 若您未使用 -out 參數,請呼叫 terraform apply,不需要使用參數。

驗證結果

  1. 取得靜態網站的 URL。

    主控台
    primary_web_host=$(terraform output -raw primary_web_host)
    
  2. 開啟瀏覽器並在瀏覽器的網址列中輸入 URL。

    Azure 記憶體帳戶中儲存的靜態網站螢幕快照。

清除資源

當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:

  1. 執行 terraform plan 並指定 destroy 旗標。

    主控台
    terraform plan -destroy -out main.destroy.tfplan
    

    重點︰

    • terraform plan 命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。
    • 選用的 -out 參數可讓您指定計畫的輸出檔。 使用 -out 參數可確保您所檢閱的方案就是所套用的方案。
  2. 執行 terraform apply 以套用執行方案。

    主控台
    terraform apply main.destroy.tfplan
    

對 Azure 上的 Terraform 進行疑難排解

針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解

下一步

備註:作者在 AI 協助下撰寫本文。 了解詳細資訊