1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TerraformでAzure VMを立ててSSH接続してみた

1
Posted at

はじめに

この記事は、インフラのコード化(IaC)を実現する Terraform の基本的な使い方を学習する最初の一歩 として作成しました。

検証環境として Azure を使用し、Linux VM(Ubuntu)のデプロイからローカルPCからの SSH 接続確認までの一連の手順をまとめています。

自分自身の備忘録を兼ねていますが、これから Terraform や Azure を触り始める方の参考になれば幸いです。

概要・構成

今回の検証を行ったローカルPCおよびツールのバージョン情報は以下の通りです。

項目 バージョン / 情報
OS macOS (Darwin Kernel Version 25.4.0 arm64 / Apple Silicon)
Terraform v1.16.0
azurerm Provider v3.117.1
Azure CLI 2.90.0

概要・構成

今回作成するリソースの一覧です。

リソース種別 Terraformでの定義名 (azurerm_...) 概要・役割
リソースグループ azurerm_resource_group 各リソースをまとめるグループ
仮想ネットワーク azurerm_virtual_network 仮想ネットワーク(VNet)領域
サブネット azurerm_subnet VNet内のネットワーク区画
パブリックIP azurerm_public_ip 外部から接続するための固定IP
ネットワークセキュリティグループ azurerm_network_security_group SSH(22番ポート)の通信許可ルール
ネットワークインターフェース azurerm_network_interface VMに割り当てるNIC
NSGとNICの関連付け azurerm_network_interface_security_group_association ★重要(忘れるとSSH不能になります)
Linux仮想マシン azurerm_linux_virtual_machine 接続対象となるUbuntu VM

前提条件

  • Azure CLI がインストールされ、az login でログイン済みであること
  • ローカルPCに Terraform がインストールされていること
  • SSH公開鍵 (~/.ssh/id_rsa.pub) がローカルPCに存在すること

1. コードの作成 (main.tf)

作業ディレクトリを作成し、以下の main.tf を用意します。

terraform {
  required_version = ">= 1.0.0"
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
  }
}

provider "azurerm" {
  features {}
}

# 1. リソースグループ
resource "azurerm_resource_group" "rg" {
  name     = "rg-tf-demo"
  location = "japaneast"
}

# 2. VNet / サブネット
resource "azurerm_virtual_network" "vnet" {
  name                = "vnet-tf-demo"
  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                 = "snet-tf-demo"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

# 3. パブリックIP
resource "azurerm_public_ip" "pip" {
  name                = "pip-tf-demo"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  allocation_method   = "Static"
  sku                 = "Standard"
}

# 5. NIC
resource "azurerm_network_interface" "nic" {
  name                = "nic-tf-demo"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

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

# 6. Linux VM
resource "azurerm_linux_virtual_machine" "vm" {
  name                = "vm-tf-demo"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  size                = "Standard_B1s"
  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 {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

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

output "public_ip_address" {
  value = azurerm_public_ip.pip.ip_address
}

デプロイ手順

初期化(init)を行ってから実行計画(plan)を確認・反映(apply)します。

# 初期化
terraform init

# 実行計画の確認
terraform plan

# リソースの作成
terraform apply

プロンプトでyesを入力し、Apply complete!と表示されれば完了です。

SSH接続の確認

出力されたパブリックIPに対してSSHコマンドを実行します。

ssh azureuser@$(terraform output -raw public_ip_address)

プロンプトがazureuser@vm-tf-demo:~$ に変われば接続成功です。が、想定していた通りこのままでは接続できませんでした。

NSGを追加してSSH接続を可能にする

main.cfに以下のコードを追加します。

# NSG(SSH 22番ポートの許可)
resource "azurerm_network_security_group" "nsg" {
  name                = "nsg-tf-demo"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.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 = "*"
  }
}

# NSG と NIC の紐付け
resource "azurerm_network_interface_security_group_association" "nsg_assoc" {
  network_interface_id      = azurerm_network_interface.nic.id
  network_security_group_id = azurerm_network_security_group.nsg.id
}

再適用(terraform apply)

terraform apply

SSH接続の再テスト

再度SSHコマンドを実行します。

ssh azureuser@$(terraform output -raw public_ip_address)
〜省略〜
azureuser@vm-tf-demo:~$ hostname
vm-tf-demo
azureuser@vm-tf-demo:~$ uname -a
Linux vm-tf-demo 6.8.0-1064-azure #72~22.04.1-Ubuntu SMP Wed Jul 22 23:39:45 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
azureuser@vm-tf-demo:~$ exit
logout
Connection to 1.2.3.4 closed.
〜省略〜

後片付け

検証完了後は以下のコマンドでリソースを削除します。

terraform destroy
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?