次の方法で共有


Terraform を使用して Packer カスタム イメージから Azure 仮想マシン スケール セットを作成する

Terraformはクラウドインフラストラクチャの定義、プレビュー、そしてデプロイメントを可能にします。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 あなたの設定ファイルを作成した後、実行計画を作成します。これにより、インフラストラクチャの変更をデプロイする前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。

Azure 仮想マシン スケール セット を使用すると、同じ VM を構成できます。 VM インスタンスの数は、需要またはスケジュールに基づいて調整できます。 詳細については、「 Azure portal で仮想マシン スケール セットを自動的にスケーリングする」を参照してください。

この記事では、次の方法について説明します。

  • Terraform デプロイを設定する
  • Terraform デプロイに変数と出力を使用する
  • ネットワーク インフラストラクチャの作成とデプロイ
  • Packer を使用したカスタム仮想マシン イメージの作成
  • カスタム イメージを使用して仮想マシン スケール セットを作成してデプロイする
  • ジャンプボックスを作成してデプロイする

1. 環境を構成する

  • Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • Terraform の構成: まだ構成していない場合は、次のいずれかのオプションを使用して Terraform を構成します。

2. Packer イメージを作成する

  1. Packer をインストールします

    重要なポイント:

    • Packer 実行可能ファイルにアクセスできることを確認するには、次のコマンドを実行します: packer -v
    • 環境によっては、パスを設定してコマンドラインを再度開く必要がある場合があります。
  2. az group create を実行して、Packer イメージを保持するリソース グループを作成します。

    az group create -n myPackerImages -l eastus
    
  3. az ad sp create-for-rbac を実行して、Packer がサービス プリンシパルを使用して Azure に対して認証できるようにします。

    az ad sp create-for-rbac --role Contributor --scopes /subscriptions/<subscription_id> --query "{ client_id: appId, client_secret: password, tenant_id: tenant }"
    

    重要なポイント:

    • 出力値 (appIdclient_secrettenant_id) をメモします。
  4. az account show を実行して、現在の Azure サブスクリプションを表示します。

    az account show --query "{ subscription_id: id }"
    
  5. ubuntu.pkr.hcl という名前の Packer テンプレート変数ファイルを作成し、次のコードを挿入します。 強調表示された行を、サービス プリンシパルと Azure サブスクリプション情報で更新します。

    packer {
      required_plugins {
        azure = {
          source  = "github.com/hashicorp/azure"
          version = "~> 2"
        }
      }
    }
    
    variable client_id {
      type    = string
      default = null
    }
    variable client_secret {
      type    = string
      default = null
    }
    
    variable subscription_id {
      type    = string
      default = null
    }
    
    variable tenant_id {
      type    = string
      default = null
    }
    
    variable ___location {
      default = "eastus"
    }
    
    variable "image_resource_group_name" {
      description = "Name of the resource group in which the Packer image will be created"
      default     = "myPackerImages"
    }
    
    variable "oidc_request_url" {
      default = null
    }
    
    variable "oidc_request_token" {
      default = null
    }
    
    # arm builder
    source "azure-arm" "builder" {
      client_id                         = var.client_id
      client_secret                     = var.client_secret
      image_offer                       = "UbuntuServer"
      image_publisher                   = "canonical"
      image_sku                         = "16.04-LTS"
      ___location                          = var.___location
      managed_image_name                = "myPackerImage"
      managed_image_resource_group_name = var.image_resource_group_name
      os_type                           = "Linux"
      subscription_id                   = var.subscription_id
      tenant_id                         = var.tenant_id
      oidc_request_url                  = var.oidc_request_url
      oidc_request_token                = var.oidc_request_token
      vm_size                           = "Standard_DS2_v2"
      azure_tags = {
        "dept" : "Engineering",
        "task" : "Image deployment",
      }
    }
    
    build {
      sources = ["source.azure-arm.builder"]
      provisioner "shell" {
        execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
        inline = [
          "apt-get update",
          "apt-get upgrade -y",
          "apt-get -y install nginx",
          "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync",
        ]
      }
    }
    

    重要なポイント:

    • client_idclient_secrettenant_id の各フィールドを、サービス プリンシパルのそれぞれの値に設定します。
    • subscription_id フィールドを Azure サブスクリプション ID に設定します。
  6. Packer イメージをビルドします。

    packer build ubuntu.json
    

3. Terraform コードを実装する

  1. サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。

  2. main.tf という名前のファイルを作成し、次のコードを挿入します。

    terraform {
    
      required_version = ">=0.12"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        azapi = {
          source  = "Azure/azapi"
          version = "~> 1.0"
        }
        local = {
          source  = "hashicorp/local"
          version = "2.4.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "3.5.1"
        }
        tls = {
          source  = "hashicorp/tls"
          version = "4.0.4"
        }
      }
    }
    
    provider "azurerm" {
      features {
        resource_group {
          prevent_deletion_if_contains_resources = false
        }
      }
    }
    
    resource "random_pet" "id" {}
    
    resource "azurerm_resource_group" "vmss" {
      name     = coalesce(var.resource_group_name, "201-vmss-packer-jumpbox-${random_pet.id.id}")
      ___location = var.___location
      tags     = var.tags
    }
    
    resource "random_string" "fqdn" {
      length  = 6
      special = false
      upper   = false
      numeric = false
    }
    
    resource "azurerm_virtual_network" "vmss" {
      name                = "vmss-vnet"
      address_space       = ["10.0.0.0/16"]
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
      tags                = var.tags
    }
    
    resource "azurerm_subnet" "vmss" {
      name                 = "vmss-subnet"
      resource_group_name  = azurerm_resource_group.vmss.name
      virtual_network_name = azurerm_virtual_network.vmss.name
      address_prefixes     = ["10.0.2.0/24"]
    }
    
    resource "azurerm_public_ip" "vmss" {
      name                = "vmss-public-ip"
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
      allocation_method   = "Static"
      domain_name_label   = random_string.fqdn.result
      tags                = var.tags
    }
    
    resource "azurerm_lb" "vmss" {
      name                = "vmss-lb"
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
    
      frontend_ip_configuration {
        name                 = "PublicIPAddress"
        public_ip_address_id = azurerm_public_ip.vmss.id
      }
    
      tags = var.tags
    }
    
    resource "azurerm_lb_backend_address_pool" "bpepool" {
      loadbalancer_id = azurerm_lb.vmss.id
      name            = "BackEndAddressPool"
    }
    
    resource "azurerm_lb_probe" "vmss" {
      loadbalancer_id = azurerm_lb.vmss.id
      name            = "ssh-running-probe"
      port            = var.application_port
    }
    
    resource "azurerm_lb_rule" "lbnatrule" {
      loadbalancer_id                = azurerm_lb.vmss.id
      name                           = "http"
      protocol                       = "Tcp"
      frontend_port                  = var.application_port
      backend_port                   = var.application_port
      backend_address_pool_ids       = [azurerm_lb_backend_address_pool.bpepool.id]
      frontend_ip_configuration_name = "PublicIPAddress"
      probe_id                       = azurerm_lb_probe.vmss.id
    }
    
    data "azurerm_resource_group" "image" {
      name = var.packer_resource_group_name
    }
    
    data "azurerm_image" "image" {
      name                = var.packer_image_name
      resource_group_name = data.azurerm_resource_group.image.name
    }
    
    resource "azapi_resource" "ssh_public_key" {
      type      = "Microsoft.Compute/sshPublicKeys@2022-11-01"
      name      = random_pet.id.id
      ___location  = azurerm_resource_group.vmss.___location
      parent_id = azurerm_resource_group.vmss.id
    }
    
    resource "azapi_resource_action" "ssh_public_key_gen" {
      type        = "Microsoft.Compute/sshPublicKeys@2022-11-01"
      resource_id = azapi_resource.ssh_public_key.id
      action      = "generateKeyPair"
      method      = "POST"
    
      response_export_values = ["publicKey", "privateKey"]
    }
    
    resource "random_password" "password" {
      count  = var.admin_password == null ? 1 : 0
      length = 20
    }
    
    locals {
      admin_password = try(random_password.password[0].result, var.admin_password)
    }
    
    resource "azurerm_virtual_machine_scale_set" "vmss" {
      name                = "vmscaleset"
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
      upgrade_policy_mode = "Manual"
    
      sku {
        name     = "Standard_DS1_v2"
        tier     = "Standard"
        capacity = 2
      }
    
      storage_profile_image_reference {
        id = data.azurerm_image.image.id
      }
    
      storage_profile_os_disk {
        name              = ""
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
      }
    
      storage_profile_data_disk {
        lun           = 0
        caching       = "ReadWrite"
        create_option = "Empty"
        disk_size_gb  = 10
      }
    
      os_profile {
        computer_name_prefix = "vmlab"
        admin_username       = var.admin_user
        admin_password       = local.admin_password
      }
    
      os_profile_linux_config {
        disable_password_authentication = true
    
        ssh_keys {
          path     = "/home/azureuser/.ssh/authorized_keys"
          key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey
        }
      }
    
      network_profile {
        name    = "terraformnetworkprofile"
        primary = true
    
        ip_configuration {
          name                                   = "IPConfiguration"
          subnet_id                              = azurerm_subnet.vmss.id
          load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.bpepool.id]
          primary                                = true
        }
      }
    
      tags = var.tags
    }
    
    resource "azurerm_public_ip" "jumpbox" {
      name                = "jumpbox-public-ip"
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
      allocation_method   = "Static"
      domain_name_label   = "${random_string.fqdn.result}-ssh"
      tags                = var.tags
    }
    
    resource "azurerm_network_interface" "jumpbox" {
      name                = "jumpbox-nic"
      ___location            = var.___location
      resource_group_name = azurerm_resource_group.vmss.name
    
      ip_configuration {
        name                          = "IPConfiguration"
        subnet_id                     = azurerm_subnet.vmss.id
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = azurerm_public_ip.jumpbox.id
      }
    
      tags = var.tags
    }
    
    resource "azurerm_virtual_machine" "jumpbox" {
      name                  = "jumpbox"
      ___location              = var.___location
      resource_group_name   = azurerm_resource_group.vmss.name
      network_interface_ids = [azurerm_network_interface.jumpbox.id]
      vm_size               = "Standard_DS1_v2"
    
      storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
      }
    
      storage_os_disk {
        name              = "jumpbox-osdisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
      }
    
      os_profile {
        computer_name  = "jumpbox"
        admin_username = var.admin_user
        admin_password = local.admin_password
      }
    
      os_profile_linux_config {
        disable_password_authentication = true
    
        ssh_keys {
          path     = "/home/azureuser/.ssh/authorized_keys"
          key_data = azapi_resource_action.ssh_public_key_gen.output.publicKey
        }
      }
    
      tags = var.tags
    }
    
  3. プロジェクト変数を含む variables.tf という名前のファイルを作成し、次のコードを挿入します。

    variable "packer_resource_group_name" {
      description = "Name of the resource group in which the Packer image will be created"
      default     = "myPackerImages"
    }
    
    variable "packer_image_name" {
      description = "Name of the Packer image"
      default     = "myPackerImage"
    }
    
    variable "resource_group_name" {
      description = "Name of the resource group in which the Packer image  will be created"
      default     = null
    }
    
    variable "___location" {
      default     = "eastus"
      description = "Location where resources will be created"
    }
    
    variable "tags" {
      description = "Map of the tags to use for the resources that are deployed"
      type        = map(string)
      default = {
        environment = "codelab"
      }
    }
    
    variable "application_port" {
      description = "Port that you want to expose to the external load balancer"
      default     = 80
    }
    
    variable "admin_user" {
      description = "User name to use as the admin account on the VMs that will be part of the VM scale set"
      default     = "azureuser"
    }
    
    variable "admin_password" {
      description = "Default password for admin account"
      default     = null
    }
    
  4. output.tfという名前のファイルを作成して、Terraform に表示される値を指定し、次のコードを挿入します。

    output "vmss_public_ip_fqdn" {
      value = azurerm_public_ip.vmss.fqdn
    }
    
    output "jumpbox_public_ip_fqdn" {
      value = azurerm_public_ip.jumpbox.fqdn
    }
    
    output "jumpbox_public_ip" {
      value = azurerm_public_ip.jumpbox.ip_address
    }
    

4. Terraform の初期化

terraform init 実行して、Terraform デプロイを初期化します。 このコマンドは、Azureリソースを管理するために必要なAzureプロバイダーをダウンロードします。

terraform init -upgrade

重要なポイント:

  • -upgrade パラメーターは、必要なプロバイダー プラグインを、構成のバージョン制約に準拠する最新バージョンにアップグレードします。

5. Terraform 実行プランを作成する

実行計画を作成するために terraform plan を実行してください。

terraform plan -out main.tfplan

重要なポイント:

  • terraform plan コマンドは実行プランを作成しますが、実行はしません。 代わりに、それは設定ファイルで指定された設定を作成するために必要な手順を決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが期待と一致するかどうかを確認できます。
  • 任意の-outパラメーターを使用すると、プランの出力ファイルを指定することができます。 -out パラメーターを使用すると、レビューしたプランがそのまま適用されることが保証されます。

6. Terraform 実行プランを適用する

クラウドインフラストラクチャに対して実行計画を適用するには、terraform apply を実行してください。

terraform apply main.tfplan

重要なポイント:

  • terraform apply コマンドの例では、以前に terraform plan -out main.tfplanを実行していることを前提としています。
  • -out パラメーターに別のファイル名を指定した場合は、terraform apply への呼び出しで同じファイル名を使用してください。
  • -out パラメーターを使用しなかった場合は、パラメーターを指定せずに terraform apply を呼び出します。

7. 結果を確認する

  1. terraform apply コマンドの出力には、次の値が表示されます。

    • 仮想マシンの FQDN
    • ジャンプボックスの FQDN
    • ジャンプボックスの IP アドレス
  2. 仮想マシンの URL を参照して、既定のページを nginx へようこそ! というテキストで確認します。

  3. SSH を使用して、変数ファイルで定義されているユーザー名と、 terraform applyの実行時に指定したパスワードを使用してジャンプボックス VM に接続します。 たとえば、 ssh azureuser@<ip_address>と指定します。

8.リソースをクリーンアップする

仮想マシン スケール セットの削除

Terraform を使用して作成されたリソースが不要になったら、次の手順を実行します。

  1. terraform plan を実行し、destroy フラグを指定してください。

    terraform plan -destroy -out main.destroy.tfplan
    

    重要なポイント:

    • terraform plan コマンドは実行プランを作成しますが、実行はしません。 代わりに、それは設定ファイルで指定された設定を作成するために必要な手順を決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが期待と一致するかどうかを確認できます。
    • 任意の-outパラメーターを使用すると、プランの出力ファイルを指定することができます。 -out パラメーターを使用すると、レビューしたプランがそのまま適用されることが保証されます。
  2. terraform applyを実行して、実行プランを適用します。

    terraform apply main.destroy.tfplan
    

Packer イメージとリソース グループの削除

az group delete を実行して、Packer イメージを格納するために使用されるリソース グループを削除します。 Packer イメージも削除されます。

az group delete --name myPackerImages --yes

Azure での Terraform のトラブルシューティング

Azure で Terraform を使用する際の一般的な問題をトラブルシュートする

次のステップ