快速入門:使用 Terraform 建立具有公用 IP 位址的 Azure 容器實例

使用 Azure 容器執行個體在 Azure 中簡潔且快速地執行無伺服器 Docker 容器。 當您不需要像 Azure Kubernetes Service 的完整容器協調流程平台時,請視需要將應用程式部署至容器執行個體。 在本文中,您會使用 Terraform 來部署隔離的 Docker 容器,並使其 Web 應用程式可供公用 IP 位址使用。

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

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

必要條件

實作 Terraform 程式碼

注意

本文中的範例程式碼位於 Azure Terraform GitHub 存放庫 (英文)。 您可以檢視記錄檔,內含目前和舊版 Terraform 的測試結果 (英文)。

請參閱更多文章和範例程式碼,其中顯示如何使用 Terraform 來管理 Azure 資源

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

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

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      name     = random_pet.rg_name.id
      location = var.resource_group_location
    }
    
    resource "random_string" "container_name" {
      length  = 25
      lower   = true
      upper   = false
      special = false
    }
    
    resource "azurerm_container_group" "container" {
      name                = "${var.container_group_name_prefix}-${random_string.container_name.result}"
      location            = azurerm_resource_group.rg.location
      resource_group_name = azurerm_resource_group.rg.name
      ip_address_type     = "Public"
      os_type             = "Linux"
      restart_policy      = var.restart_policy
    
      container {
        name   = "${var.container_name_prefix}-${random_string.container_name.result}"
        image  = var.image
        cpu    = var.cpu_cores
        memory = var.memory_in_gb
    
        ports {
          port     = var.port
          protocol = "TCP"
        }
      }
    }
    
  3. 建立名為 outputs.tf 的檔案,並插入下列程式碼:

    output "container_ipv4_address" {
      value = azurerm_container_group.container.ip_address
    }
    
  4. 建立名為 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 {}
    }
    
  5. 建立名為 variables.tf 的檔案,並插入下列程式碼:

    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location for all resources."
    }
    
    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random value so name is unique in your Azure subscription."
    }
    
    variable "container_group_name_prefix" {
      type        = string
      description = "Prefix of the container group name that's combined with a random value so name is unique in your Azure subscription."
      default     = "acigroup"
    }
    
    variable "container_name_prefix" {
      type        = string
      description = "Prefix of the container name that's combined with a random value so name is unique in your Azure subscription."
      default     = "aci"
    }
    
    variable "image" {
      type        = string
      description = "Container image to deploy. Should be of the form repoName/imagename:tag for images stored in public Docker Hub, or a fully qualified URI for other registries. Images from private registries require additional registry credentials."
      default     = "mcr.microsoft.com/azuredocs/aci-helloworld"
    }
    
    variable "port" {
      type        = number
      description = "Port to open on the container and the public IP address."
      default     = 80
    }
    
    variable "cpu_cores" {
      type        = number
      description = "The number of CPU cores to allocate to the container."
      default     = 1
    }
    
    variable "memory_in_gb" {
      type        = number
      description = "The amount of memory to allocate to the container in gigabytes."
      default     = 2
    }
    
    variable "restart_policy" {
      type        = string
      description = "The behavior of Azure runtime if container has stopped."
      default     = "Always"
      validation {
        condition     = contains(["Always", "Never", "OnFailure"], var.restart_policy)
        error_message = "The restart_policy must be one of the following: Always, Never, OnFailure."
      }
    }
    

初始化 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. 當您套用執行計劃時,Terraform 會輸出公用IP位址。 若要再次顯示 IP 位址,請執行 terraform 輸出

    terraform output -raw container_ipv4_address
    
  2. 在瀏覽器的網址列中輸入範例的公用IP位址。

    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 時的常見問題進行疑難排解

下一步