Azure に Terraform で Ubuntu 22.04 LTS 仮想マシンを作成してみた

私の場合、検証用に Azure 仮想マシンを用意するなら Azure CLI でサクッと作成してしまいます。例えば、コストを節約するためとか、微妙にリソース名や構成を変えた複数の検証環境を作成するとか、何度も作っては消してを繰り返す場合は Terraform が便利だと思います。そこで今回は、Ubuntu 22.04 LTS 仮想マシンを Terraform で作成してみました。

## 仮想マシンを作成する Terraform サンプル

```terraform:main.tf
# 環境変数をセット
locals {
  region = "japaneast"
  prefix = "mnrcode"
}

# Azure プロバイダー
provider "azurerm" {
  features {}
}

# リソースグループを作成
resource "azurerm_resource_group" "rg" {
  name     = "${local.prefix}-rg"
  location = local.region
}

# 仮想ネットワークを作成
resource "azurerm_virtual_network" "vnet" {
  name                = "${local.prefix}-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

# サブネットを作成
resource "azurerm_subnet" "subnet" {
  name                 = "default-subnet"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.0.0/24"]
}

# セキュリティグループを作成
# 接続元の IP アドレスから SSH を許可するルールを作成
resource "azurerm_network_security_group" "nsg" {
  name                = "${local.prefix}-nsg"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  security_rule {
    name                       = "Allow-SSH"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = data.http.myip.response_body
    destination_address_prefix = "*"
  }
}

# 接続元の IP アドレスを取得
data "http" "myip" {
  url = "http://inet-ip.info/ip"
}

# サブネットとセキュリティグループを登録
resource "azurerm_subnet_network_security_group_association" "subnet" {
  subnet_id                 = azurerm_subnet.subnet.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

# 仮想マシン用のパブリック IP アドレスを作成
resource "azurerm_public_ip" "pip" {
  name                = "${local.prefix}-pip"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  allocation_method   = "Static"
  domain_name_label   = local.prefix
}

# NIC を作成
# NIC にパブリック IP アドレスを設定
resource "azurerm_network_interface" "nic" {
  name                = "${local.prefix}-nic"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  ip_configuration {
    name                          = "${local.prefix}-ipconfig1"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.pip.id
  }
}

# Standard_B1ms サイズの Ubuntu 22.04 LTS 仮想マシンを作成
resource "azurerm_linux_virtual_machine" "vm" {
  name                = "${local.prefix}-vm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_B1ms"
  admin_username      = "azureuser"
  network_interface_ids = [
    azurerm_network_interface.nic.id,
  ]
  admin_ssh_key {
    username   = "azureuser"
    public_key = file("~/.ssh/id_rsa.pub")
  }
  os_disk {
    name                 = "${local.prefix}-osdisk"
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-jammy"
    sku       = "22_04-lts-gen2"
    version   = "latest"
  }
}
```

## 実行コマンド

```bash:bash
$ terraform init

$ terraform plan

$ terraform apply

$ ssh azureuser@mnrcode.japaneast.cloudapp.azure.com

$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

$ exit

$ terraform destroy
```

## 参考

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs