快速入門:將 Azure Cosmos DB 部署至 Azure 容器執行個體
文章使用下列 Terraform 和 Terraform 提供者版本進行測試:
Terraform 可讓您定義、預覽和部署雲端基礎結構。 使用 Terraform 時,您可以使用 HCL 語法來建立設定檔。 HCL 語法可讓您指定雲端提供者 (例如 Azure) 和構成雲端基礎結構的元素。 建立設定檔之後,您可以建立執行計畫,讓您先預覽基礎結構變更,之後再部署。 驗證變更之後,您可以套用執行計畫來部署基礎結構。
本文說明如何使用 Terraform 將 Azure Cosmos DB 部署至 Azure 容器執行個體。
在本文中,您將學會如何:
- 建立 Azure Cosmos DB 執行個體
- 建立 Azure 容器實例
- 建立可跨這兩個資源運作的應用程式
注意
本文中的範例程式代碼位於 Microsoft Terraform GitHub 存放庫中。
必要條件
- Azure 訂用帳戶:如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
設定 Terraform:如果您尚未這麼做,請使用下列其中一個選項來設定 Terraform:
實作 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
的檔案,並插入下列程式碼:resource "azurerm_resource_group" "rg" { name = "${random_pet.rg_name.id}-rg" location = var.resource_group_location } resource "azurerm_cosmosdb_account" "vote_cosmos_db" { name = "${random_pet.rg_name.id}-${random_integer.ri.result}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name offer_type = "Standard" kind = "GlobalDocumentDB" consistency_policy { consistency_level = "BoundedStaleness" max_interval_in_seconds = 10 max_staleness_prefix = 200 } geo_location { location = azurerm_resource_group.rg.location failover_priority = 0 } } resource "random_integer" "ri" { min = 10000 max = 99999 } resource "random_pet" "rg_name" { prefix = var.prefix }
建立名為
aci.tf
的檔案,並插入下列程式碼:resource "azurerm_container_group" "main" { name = "${random_pet.rg_name.id}-vote-aci" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_address_type = "Public" dns_name_label = "vote-aci-${random_integer.ri.result}" os_type = "Linux" container { name = "vote-aci" image = "mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb" cpu = "0.5" memory = "1.5" ports { port = 80 protocol = "TCP" } secure_environment_variables = { "COSMOS_DB_ENDPOINT" = azurerm_cosmosdb_account.vote_cosmos_db.endpoint "COSMOS_DB_MASTERKEY" = azurerm_cosmosdb_account.vote_cosmos_db.primary_key "TITLE" = "Azure Voting App" "VOTE1VALUE" = "Cats" "VOTE2VALUE" = "Dogs" } } }
建立名為
variables.tf
的檔案,並插入下列程式碼:variable "resource_group_location" { default = "eastus" description = "Location of the resource group." } variable "prefix" { type = string default = "cosmos-db-aci" description = "Prefix of the resource name" }
建立名為
outputs.tf
的檔案,並插入下列程式碼:output "resource_group_name" { value = azurerm_resource_group.rg.name } output "cosmosdb_account_name" { value = azurerm_cosmosdb_account.vote_cosmos_db.name } output "dns" { value = azurerm_container_group.main.fqdn }
初始化 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
,不需要使用參數。
驗證結果
取得資源組名。
echo "$(terraform output resource_group_name)"
取得 Azure Cosmos DB 帳戶名稱。
echo "$(terraform output cosmosdb_account_name)"
執行 az cosmosdb sql database list/
az cosmosdb sql database list \ --resource-group <resource_group_name> \ --account-name <cosmosdb_account_name>
測試應用程式
取得 Azure Cosmos DB 帳戶名稱。
echo "$(terraform output dns)"
流覽至上一個步驟中指出的URL。 您應該會看到類似下列輸出的結果:
清除資源
當您不再需要透過 Terraform 建立的資源時,請執行下列步驟:
執行 terraform plan 並指定
destroy
旗標。terraform plan -destroy -out main.destroy.tfplan
重點︰
terraform plan
命令會建立執行計畫,但不會執行。 相反地,其會決定要在您指定的設定檔中建立設定所需的動作。 此模式可讓您在對實際資源進行任何變更之前,先確認執行方案是否符合您的預期。- 選用的
-out
參數可讓您指定計畫的輸出檔。 使用-out
參數可確保您所檢閱的方案就是所套用的方案。 - 若要閱讀保存執行方案和安全性的詳細資訊,請參閱安全性警告一節。
執行 terraform apply 以套用執行方案。
terraform apply main.destroy.tfplan
對 Azure 上的 Terraform 進行疑難排解
針對在 Azure 上使用 Terraform 時的常見問題進行疑難排解