Terraformを使ってAzureでVMインスタンスを立てる

AzureでVMインスタンスが立てられるようになったので、メモ。

```main.tf

provider "azurerm" {
  subscription_id = ""
  features {}
}

# リソースグループの作成
resource "azurerm_resource_group" "myazrg" {
  name     = "aztest_rg"
  location = "japaneast"
}

# 仮想ネットワークの作成
resource "azurerm_virtual_network" "vnet" {
  name                = "aztest_vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.myazrg.location
  resource_group_name = azurerm_resource_group.myazrg.name
}

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

# サブネットとNSGの関連付け
resource "azurerm_subnet_network_security_group_association" "subnet_nsg" {
  subnet_id                 = azurerm_subnet.subnet.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

# パブリックIPの作成
resource "azurerm_public_ip" "publicip" {
  name                         = "myPublicIP"
  location                     = azurerm_resource_group.myazrg.location
  resource_group_name          = azurerm_resource_group.myazrg.name
  allocation_method            = "Static"
  sku                          = "Standard"
}

# NSGの作成と通信ルールの設定（SSH許可）
resource "azurerm_network_security_group" "nsg" {
  name                = "mynsg"
  location            = azurerm_resource_group.myazrg.location
  resource_group_name = azurerm_resource_group.myazrg.name

  security_rule {
    name                       = "SSH"
    priority                   = 1001
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "22"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

# ネットワークインターフェイスの作成
resource "azurerm_network_interface" "nic" {
  name                      = "nic"
  location                  = azurerm_resource_group.myazrg.location
  resource_group_name       = azurerm_resource_group.myazrg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.subnet.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.publicip.id
  }
}

# SSHキーの作成
resource "tls_private_key" "myazssh" {
  algorithm = "RSA"
  rsa_bits  = 4096
}

output "tls_private_key" { 
  value     = tls_private_key.myazssh.private_key_pem
  sensitive = true
}

# 仮想マシンの作成
resource "azurerm_linux_virtual_machine" "myazvm" {
  name                = "myszvm"
  resource_group_name = azurerm_resource_group.myazrg.name
  location            = azurerm_resource_group.myazrg.location
  size                = "Standard_DS1_v2"
  admin_username      = "azureuser"
  network_interface_ids = [
    azurerm_network_interface.nic.id,
  ]
  
  admin_ssh_key {
    username   = "azureuser"
    public_key = tls_private_key.myazssh.public_key_openssh
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"  # Update to the correct version, 18.04-LTS or another preferred version.
    version   = "latest"
 }
}

```

subscription_idの入力とAzure loginはあらかじめやっておいてください。