共用方式為


快速入門 - 使用 Terraform 來建立 Azure Cosmos DB 資料庫與容器

適用於:NoSQL

Azure Cosmos DB 是 Microsoft 的快速 NoSQL 資料庫,可支援任何規模的開放式 API。 您可以使用 Azure Cosmos DB 快速地建立及查詢機碼/值資料庫、文件資料庫與圖形資料庫。 如果沒有信用卡或 Azure 訂用帳戶,您可以設定免費的試用 Azure Cosmos DB 帳戶。 此快速入門著重在透過 Terraform 部署以建立 Azure Cosmos 資料庫並於該資料庫內建立容器的程序。 您稍後可以在此容器中儲存資料。

必要條件

Azure 訂用帳戶或免費的 Azure Cosmos DB 試用帳戶

  • 如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶

Terraform 應該安裝在本機電腦上。 您可以在這裡 (英文) 找到安裝指示。

檢閱 Terraform 檔案

您可以在 terraform 範例存放庫 (英文) 上找到此快速入門中使用的 Terraform 檔案。 建立下列三個檔案:providers.tf、main.tf 與 variables.tf。 變數可以在命令列中設定,也可以使用 terraforms.tfvars 檔案設定。

提供者 Terraform 檔案

terraform {
  required_version = ">= 1.0"

  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = ">= 3.0, < 4.0"
    }
    random = {
      source  = "hashicorp/random"
      version = ">= 3.0"
    }
  }
}

provider "azurerm" {
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
}

主要 Terraform 檔案

resource "azurerm_resource_group" "example" {
  name     = "${random_pet.prefix.id}-rg"
  location = var.location
}

resource "azurerm_cosmosdb_account" "example" {
  name                      = "${random_pet.prefix.id}-cosmosdb"
  location                  = var.cosmosdb_account_location
  resource_group_name       = azurerm_resource_group.example.name
  offer_type                = "Standard"
  kind                      = "GlobalDocumentDB"
  enable_automatic_failover = false
  geo_location {
    location          = var.location
    failover_priority = 0
  }
  consistency_policy {
    consistency_level       = "BoundedStaleness"
    max_interval_in_seconds = 300
    max_staleness_prefix    = 100000
  }
  depends_on = [
    azurerm_resource_group.example
  ]
}

resource "azurerm_cosmosdb_sql_database" "main" {
  name                = "${random_pet.prefix.id}-sqldb"
  resource_group_name = azurerm_resource_group.example.name
  account_name        = azurerm_cosmosdb_account.example.name
  throughput          = var.throughput
}

resource "azurerm_cosmosdb_sql_container" "example" {
  name                  = "${random_pet.prefix.id}-sql-container"
  resource_group_name   = azurerm_resource_group.example.name
  account_name          = azurerm_cosmosdb_account.example.name
  database_name         = azurerm_cosmosdb_sql_database.main.name
  partition_key_path    = "/definition/id"
  partition_key_version = 1
  throughput            = var.throughput

  indexing_policy {
    indexing_mode = "consistent"

    included_path {
      path = "/*"
    }

    included_path {
      path = "/included/?"
    }

    excluded_path {
      path = "/excluded/?"
    }
  }

  unique_key {
    paths = ["/definition/idlong", "/definition/idshort"]
  }
}

resource "random_pet" "prefix" {
  prefix = var.prefix
  length = 1
}

變數 Terraform 檔案

variable "prefix" {
  type        = string
  default     = "cosmosdb-manualscale"
  description = "Prefix of the resource name"
}

variable "location" {
  type        = string
  default     = "Canada Central"
  description = "Resource group location"
}

variable "cosmosdb_account_location" {
  type        = string
  default     = "Canada Central"
  description = "Cosmos db account location"
}

variable "throughput" {
  type        = number
  default     = 400
  description = "Cosmos db database throughput"
  validation {
    condition     = var.throughput >= 400 && var.throughput <= 1000000
    error_message = "Cosmos db manual throughput should be equal to or greater than 400 and less than or equal to 1000000."
  }
  validation {
    condition     = var.throughput % 100 == 0
    error_message = "Cosmos db throughput should be in increments of 100."
  }
}

這三個 Cosmos DB 資源都是在主要 terraform 檔案中定義。

透過 terraform 部署

  1. 在本機電腦中將 terraform 檔案儲存為 main.tf、variables.tf 與 providers.tf。
  2. 透過 Azure CLI 或 PowerShell 登入您的終端
  3. 透過 Terraform 命令部署
    • Terraform 初始化
    • terraform plan
    • terraform apply

驗證部署

使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來列出資源群組中已部署的資源。

az resource list --resource-group "your resource group name"

清除資源

如果您打算繼續進行後續的快速入門和教學課程,您可以讓這些資源留在原處。 不再需要時,請使用 Azure 入口網站、Azure CLI 或 Azure PowerShell 來刪除資源群組及其資源。

az group delete --name "your resource group name"

下一步

在此快速入門中,您已透過 terraform 建立 Azure Cosmos 帳戶、資料庫與容器,並已驗證部署。 若要深入了解 Azure Cosmos DB 與 Terraform,請繼續閱讀下列文章。