【備忘録】Terraformを使用してAzure上にVMを構築する
■はじめに
現在SelenoidやGgrを構築している途中で色々と寄り道をしています。 実はちょっと理由があるのですが、今回のエントリもこの流れに繋がる手順の一つとなっています。
■前提
まず既にサブスクリプションを作成済みであることとします。
■Terraformファイル本体
SelenoidやGgrの環境を諸般の理由からAzure上に建てたいなと思い、一旦疎通レベルで書きました。 詳しい内容に関しましては後日改めて確認してゆくとして、本日は備忘録としてtfファイルを(晒して良さそな範囲で)そのまま載せようと思います。
provider "azurerm" { version = "~>2.0" features {} subscription_id = "サブスクリプションID" client_id = "appIdとして返却される情報" client_secret = "passwordとして返却される情報" tenant_id = "テナントID" } # Azure 接続とリソース グループを作成する resource "azurerm_resource_group" "rg" { name = "指定のリソース グループ名" location = "eastus" } # 仮想ネットワークの作成 resource "azurerm_virtual_network" "tfnetwork" { name = "指定の仮想ネットワーク名" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } # サブネットの設定 resource "azurerm_subnet" "tfsubnet" { name = "指定のサブネット名" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.tfnetwork.name address_prefix = "10.0.1.0/24" } # パブリック IP アドレスの作成 resource "azurerm_public_ip" "tfpublicip" { name = "指定の名前" location = "eastus" resource_group_name = azurerm_resource_group.rg.name allocation_method = "Dynamic" } # ネットワーク セキュリティ グループの作成 resource "azurerm_network_security_group" "tfnsg" { name = "指定のネットワーク セキュリティ グループ名" location = "eastus" 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 = "指定のポート番号" source_address_prefix = "*" destination_address_prefix = "*" } } # 仮想ネットワーク インターフェイス カードの作成 resource "azurerm_network_interface" "tfnic" { name = "指定の仮想ネットワーク インターフェイス カード名" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "myNicConfiguration" subnet_id = azurerm_subnet.tfsubnet.id private_ip_address_allocation = "Dynamic" public_ip_address_id = azurerm_public_ip.tfpublicip.id } } # セキュリティグループをネットワークインターフェースに接続 resource "azurerm_network_interface_security_group_association" "example" { network_interface_id = azurerm_network_interface.tfnic.id network_security_group_id = azurerm_network_security_group.tfnsg.id } # VMの作成 resource "azurerm_linux_virtual_machine" "tfvm" { name = "指定のVM名" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location size = "Standard_F2" admin_username = "test" network_interface_ids = [ azurerm_network_interface.tfnic.id, ] # SSHの公開鍵 admin_ssh_key { username = "test" public_key = file("~/.ssh/id_rsa.pub") } os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } # 使用するVMイメージ source_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "18.04-LTS" version = "latest" } }
■必要なパラメータ
terraform本体の頭で設定している各種パラメータは以下のコマンドの返却値を埋めます。
①サブスクリプション情報やテナント情報は以下のコマンドで確認ができます。
az account list --query "[].{name:name, subscriptionId:id, tenantId:tenantId}"
②サービスプリンシパル
その上でサービスプリンシパルにアクセスする際の情報は以下のコマンドで確認ができます。
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/上記で取得したサブスクリプションID
appIdとPasswordが返却されますので、それぞれ
client_id = appId
client_secret = password
で置き換えます。
上記までの手順を踏まえて、terraform plan、terraform applyするとAzure上にVMが作成されます。
本当はもっと細かく書くべきなんだけど今日はめちゃくちゃ疲れてしまったので、ちょっとここまで。。
ではでは。
(参考にしたページ)
terraformの公式ドキュメント
www.terraform.io
Azureの公式ドキュメント