事件
3月31日 下午11時 - 4月2日 下午11時
規模最大的 Fabric、Power BI 與 SQL 學習盛會。 3 月 31 日至 4 月 2 日。 使用代碼 FABINSIDER 可節省 $400。
立即報名在本快速入門中,您會了解如何部署已啟用靜態網站裝載的 Azure 儲存體帳戶。
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
在本文中,您將學會如何:
備註
本文中的範例程式碼位於 Azure Terraform GitHub 存放庫。 您可以檢視內含目前和舊版 Terraform 測試結果的記錄檔。
建立目錄,然後在目錄中測試範例 Terraform 程式碼,並將其設為目前的目錄。
建立名為 providers.tf
的檔案,並插入下列程式碼:
terraform {
required_version = ">=1.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
random = {
source = "hashicorp/random"
version = "~>3.0"
}
}
}
provider "azurerm" {
features {}
}
建立名為 main.tf
的檔案,並插入下列程式碼:
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"
}
建立名為 variables.tf
的檔案,並插入下列程式碼:
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"
}
建立名為 outputs.tf
的檔案,並插入下列程式碼:
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
}
建立名為 index.html
的檔案,並插入下列程式碼:
<h1> This is a static website example <h1>
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點︰
-upgrade
參數會將必要的提供者外掛程式升級至符合設定版本條件約束的最新版本。執行 terraform plan 以建立執行計畫。
terraform plan -out main.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 terraform apply 將執行計畫套用至您的雲端基礎結構。
terraform apply main.tfplan
重點︰
terraform apply
命令假設您之前已執行過 terraform plan -out main.tfplan
。-out
參數指定了不同的檔案名稱,請在呼叫 terraform apply
時使用該檔案名稱。-out
參數,請呼叫 terraform apply
,不需要使用參數。取得靜態網站的 URL。
primary_web_host=$(terraform output -raw primary_web_host)
開啟瀏覽器並在瀏覽器的網址列中輸入 URL。
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform plan 並指定 destroy
旗標。
terraform plan -destroy -out main.destroy.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。-out
參數可讓您指定計畫的輸出檔。 使用 -out
參數可確保您所檢閱的方案就是所套用的方案。執行 terraform apply 以套用執行方案。
terraform apply main.destroy.tfplan
事件
3月31日 下午11時 - 4月2日 下午11時
規模最大的 Fabric、Power BI 與 SQL 學習盛會。 3 月 31 日至 4 月 2 日。 使用代碼 FABINSIDER 可節省 $400。
立即報名訓練
認證
Microsoft Certified: Azure Developer Associate - Certifications
在 Microsoft Azure 中建置端對端解決方案,以建立 Azure Functions、實作和管理 Web 應用程式、開發使用 Azure 儲存體的解決方案等等。
文件
使用 GitHub Actions 將靜態網站部署至 Azure 儲存體
使用 GitHub Actions Azure 儲存體靜態網站裝載
本教學課程會引導您建立及部署使用 Service Connector 連線到 Azure Blob 儲存體 的 Web 應用程式。
Azure 儲存體靜態網站代管,提供符合成本效益且可延展的解決方案來代管新式 Web 應用程式。