LoginSignup
0
1

More than 1 year has passed since last update.

TerraformでAzureVMの環境構築

Posted at

はじめに

実務でAzureを触っているので備忘録としてアウトプットしていきたいと思います。
Azure&Terraformに関して初心者なので間違っていることをかいていたらすみません。

環境

M1 Mac book Air

手順

まずはTerraformのインストール

Terraformのバージョンを複数管理するためにtfenvのインストールをします。

brew install tfenv

以下のコマンドを実行して利用するバージョンをコピーしてください。

tfenv list-remote
出力例
1.4.0-alpha20221207
1.4.0-alpha20221109
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0

自分は現在公開されている最新のバージョンにしました。

tfenv install 1.3.7
tfenv use 1.3.7

tfenv useを忘れずに。

リソース作成前の事前準備

sshの公開鍵の作成を行なってください。
設定は全てdefaultのままで問題ありません。

ssh-keygen

ソースコード

適当なディレクトリにmain.tfprovider.tfを作成して以下のソースコードをコピペしてください。

provider.tf
terraform {
  required_version = ">=0.12"

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

provider "azurerm" {
  features {}
}
main.tf
resource "azurerm_resource_group" "test" {
  name     = "test-resources"
  location = "japaneast"
}

resource "azurerm_virtual_network" "test" {
  name                = "test-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_subnet" "test" {
  name                 = "test-subnet"
  resource_group_name  = azurerm_resource_group.test.name
  virtual_network_name = azurerm_virtual_network.test.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_public_ip" "test" {
  name                = "test-public-ip"
  resource_group_name = azurerm_resource_group.test.name
  location            = azurerm_resource_group.test.location
  allocation_method   = "Dynamic"
}

resource "azurerm_network_interface" "test" {
  name                = "test-nic"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name

  ip_configuration {
    name                          = "test-ip-config"
    subnet_id                     = azurerm_subnet.test.id
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = azurerm_public_ip.test.id
  }
}

resource "azurerm_network_security_group" "test" {
  name                = "test-security-group"
  location            = azurerm_resource_group.test.location
  resource_group_name = azurerm_resource_group.test.name
}

resource "azurerm_network_security_rule" "test" {
  name                        = "SSH"
  priority                    = 300
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "22"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  network_security_group_name = azurerm_network_security_group.test.name
  resource_group_name         = azurerm_resource_group.test.name
}

resource "azurerm_network_interface_security_group_association" "test" {
  network_interface_id      = azurerm_network_interface.test.id
  network_security_group_id = azurerm_network_security_group.test.id
}

resource "azurerm_linux_virtual_machine" "test" {
  name                = "test-machine"
  resource_group_name = azurerm_resource_group.test.name
  location            = azurerm_resource_group.test.location
  size                = "Standard_B1ls"
  admin_username      = "adminuser"
  network_interface_ids = [
    azurerm_network_interface.test.id,
  ]

  admin_ssh_key {
    username   = "adminuser"
    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-gen2"
    version   = "latest"
  }
}

参考にしたドキュメント

リソースの作成

terraformの初期化を行います。
リソースの作成に必要なモジュールはprovider.tfの内容から自動で取得されます。

terraform init

planでどのようなリソースが展開されるのか確認して、
問題なさそうであればapplyを実行してください。

terraform plan -out main.tfplan
terraform apply main.tfplan

リソースの削除をする場合は以下のコマンドを実行してください。

terraform destroy

使わないリソースの削除は忘れずに。

慣れないうちはポータル上からリソースの作成をしてAzure terrafyでインポートするのが良さそう。

brew install aztfy
aztfy resource-group {リソースグループ名}

詳しい内容はこちら。

最後に

誰かの参考になれば嬉しいです!

0
1
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
0
1