在本快速入門中,您會使用 Terraform 在 Azure Functions 的 Flex 取用方案中建立函式應用程式,以及其他必要的 Azure 資源。 Flex Consumption 方案提供無伺服器裝載,可讓您視需要執行程序代碼,而不需要明確布建或管理基礎結構。 它用於處理數據、整合系統、物聯網運算,以及建置簡單的 API 和微服務。 在此設定中建立的資源包括唯一的資源群組、記憶體帳戶、Blob 記憶體容器、Flex 取用方案,以及函式應用程式本身。 函式應用程式會在Linux上執行,並設定為使用 Blob 記憶體進行程式碼部署。
Terraform 允許對雲端基礎結構進行定義、預覽和部署。 使用 Terraform,您可以使用 HCL 語法建立組態檔。 HCL 語法可讓您指定雲端提供者,例如 Azure,以及構成雲端基礎結構的專案。 建立組態檔之後,您會建立一個 執行計劃 ,讓您在部署基礎結構變更之前先預覽這些變更。 驗證變更之後,您會套用執行計劃來部署基礎結構。
- 建立具有唯一名稱的 Azure 資源群組。
- 產生13個小寫字母的隨機字串來命名資源。
- 在 Azure 中建立儲存體帳戶。
- 在記憶體帳戶中建立 Blob 記憶體容器。
- 在 Azure Functions 中建立 Flex Consumption 計畫。
- 在 Azure 中使用 Flex Consumption 方案建立函式應用程式。
- 輸出資源群組、記憶體帳戶、服務方案、函式應用程式和 Azure Functions Flex 取用方案的名稱。
先決條件
- 建立具有有效訂閱的 Azure 帳戶。 您可以免費建立帳戶。
- 安裝和設定 Terraform。
實作 Terraform 程式碼
本文的範例程式代碼位於 Azure Terraform GitHub 存放庫中。 您可以檢視包含 目前和舊版 Terraform 測試結果的記錄檔。 請參閱更多文章和範例程式碼,其中顯示如何使用 Terraform 來管理 Azure 資源。
建立要在其中測試及執行範例 Terraform 程式代碼的目錄,並將其設為目前目錄。
建立名為
main.tf
的檔案,並插入下列程序代碼:# This Terraform configuration creates a Flex Consumption plan app in Azure Functions # with the required Storage account and Blob Storage deployment container. # Create a random pet to generate a unique resource group name resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } # Create a resource group resource "azurerm_resource_group" "example" { location = var.resource_group_location name = random_pet.rg_name.id } # Random String for unique naming of resources resource "random_string" "name" { length = 8 special = false upper = false lower = true numeric = false } # Create a storage account resource "azurerm_storage_account" "example" { name = coalesce(var.sa_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = var.sa_account_tier account_replication_type = var.sa_account_replication_type } # Create a storage container resource "azurerm_storage_container" "example" { name = "example-flexcontainer" storage_account_id = azurerm_storage_account.example.id container_access_type = "private" } # Create a Log Analytics workspace for Application Insights resource "azurerm_log_analytics_workspace" "example" { name = coalesce(var.ws_name, random_string.name.result) location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name sku = "PerGB2018" retention_in_days = 30 } # Create an Application Insights instance for monitoring resource "azurerm_application_insights" "example" { name = coalesce(var.ai_name, random_string.name.result) location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name application_type = "web" workspace_id = azurerm_log_analytics_workspace.example.id } # Create a service plan resource "azurerm_service_plan" "example" { name = coalesce(var.asp_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location sku_name = "FC1" os_type = "Linux" } # Create a function app resource "azurerm_function_app_flex_consumption" "example" { name = coalesce(var.fa_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location service_plan_id = azurerm_service_plan.example.id storage_container_type = "blobContainer" storage_container_endpoint = "${azurerm_storage_account.example.primary_blob_endpoint}${azurerm_storage_container.example.name}" storage_authentication_type = "StorageAccountConnectionString" storage_access_key = azurerm_storage_account.example.primary_access_key runtime_name = var.runtime_name runtime_version = var.runtime_version maximum_instance_count = 50 instance_memory_in_mb = 2048 site_config { } }
建立名為
outputs.tf
的檔案,並插入下列程序代碼:output "resource_group_name" { value = azurerm_resource_group.example.name } output "sa_name" { value = azurerm_storage_account.example.name } output "asp_name" { value = azurerm_service_plan.example.name } output "fa_name" { value = azurerm_function_app_flex_consumption.example.name } output "fa_url" { value = "https://${azurerm_function_app_flex_consumption.example.name}.azurewebsites.net" }
建立名為
providers.tf
的檔案,並插入下列程序代碼:terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>4.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
建立名為
variables.tf
的檔案,並插入下列程序代碼:variable "resource_group_name" { type = string default = "" description = "The name of the Azure resource group. If blank, a random name will be generated." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." } variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "sa_account_tier" { description = "The tier of the storage account. Possible values are Standard and Premium." type = string default = "Standard" } variable "sa_account_replication_type" { description = "The replication type of the storage account. Possible values are LRS, GRS, RAGRS, and ZRS." type = string default = "LRS" } variable "sa_name" { description = "The name of the storage account. If blank, a random name will be generated." type = string default = "" } variable "ws_name" { description = "The name of the Log Analytics workspace. If blank, a random name will be generated." type = string default = "" } variable "ai_name" { description = "The name of the Application Insights instance. If blank, a random name will be generated." type = string default = "" } variable "asp_name" { description = "The name of the App Service Plan. If blank, a random name will be generated." type = string default = "" } variable "fa_name" { description = "The name of the Function App. If blank, a random name will be generated." type = string default = "" } variable "runtime_name" { description = "The name of the language worker runtime." type = string default = "node" # Allowed: dotnet-isolated, java, node, powershell, python } variable "runtime_version" { description = "The version of the language worker runtime." type = string default = "20" # Supported versions: see https://aka.ms/flexfxversions }
這很重要
如果您使用 4.x azurerm 提供者,您必須先 明確指定 Azure 訂閱識別碼來驗證 Azure,才能執行 Terraform 命令。
若要指定 Azure 訂用帳戶標識碼而不將它放在 區塊中 providers
,其中一個方法是在名為 ARM_SUBSCRIPTION_ID
的環境變數中指定訂用帳戶標識碼。
如需詳細資訊,請參閱 Azure 提供者參考檔。
初始化 Terraform
執行 terraform init 來初始化 Terraform 部署。 此命令會下載管理 Azure 資源所需的 Azure 提供者。
terraform init -upgrade
重點:
-
-upgrade
參數會將必要的提供者外掛程式升級至符合組態版本條件約束的最新版本。
建立 Terraform 執行計劃
執行 terraform 計劃 以建立執行計劃。
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
。
確認結果
取得 Azure 資源群組名稱。
resource_group_name=$(terraform output -raw resource_group_name)
取得記憶體帳戶名稱。
sa_name=$(terraform output -raw sa_name)
取得服務方案名稱。
asp_name=$(terraform output -raw asp_name)
取得函式應用程式方案名稱。
fa_name=$(terraform output -raw fa_name)
執行
az functionapp show
以檢視 Azure Functions Flex 取用方案。az functionapp show --name $function_app_name --resource-group $resource_group_name
開啟瀏覽器並輸入下列 URL:https://< fa_name.azurewebsites.net>。 將佔位元 <fa_name>
替換為 Terraform 所輸出的值。
清理資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform 計劃 並指定
destroy
旗標。terraform plan -destroy -out main.destroy.tfplan
重點:
-
terraform plan
命令會建立執行計劃,但不會執行它。 然而,它會決定哪些動作是必要的,以建立您組態檔中所指定的設定。 此模式可讓您在對實際資源進行任何變更之前,先確認執行計劃是否符合您的預期。 - 選擇性
-out
參數可讓您指定計劃的輸出檔。 使用-out
參數可確保您查看的計劃確切地被套用。
-
執行 terraform apply 來應用執行計劃。
terraform apply main.destroy.tfplan
排除 Azure 上 Terraform 的故障
針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解。
後續步驟
您現在可以將程式代碼專案部署到您在 Azure 中建立的函式應用程式資源。
您可以從這些本機環境建立、驗證程式代碼專案,並將程式代碼專案部署至新的函式應用程式: