[A-00202] Terraform入門(Azure)
azure上でterraform使うための入門記事です。
azureでterraformを使用する場合、サービスプリンシパルを作成し、それを使用する必要があります。
それらはすでに作成されたという前提で内容を記載しております。
・Azure VMを作ってみる
下記のアーキテクチャを作成します。

ARM_SUBSCRIPTION_ID = "<サービスプリンシパルの値を追記>"
ARM_CLIENT_SECRET   = "<サービスプリンシパルの値を追記>"
ARM_TENANT_ID       = "<サービスプリンシパルの値を追記>"
ARM_CLIENT_ID       = "<サービスプリンシパルの値を追記>"terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "3.108.0"
    }
    azapi = {
        source = "azure/azapi"
        version = "1.13.1"
    }
  }
}
provider "azurerm" {
  skip_provider_registration = true
  features {
    resource_group {
      prevent_deletion_if_contains_resources = false
    }
  }
  subscription_id = var.ARM_SUBSCRIPTION_ID
  tenant_id       = var.ARM_TENANT_ID
  client_id       = var.ARM_CLIENT_ID
  client_secret   = var.ARM_CLIENT_SECRET
}
provider "azapi" {
}
resource "azurerm_resource_group" "example" {
  name = "example-resource"
  location = "Japan East"
}variable "ARM_SUBSCRIPTION_ID" {
  type = string
}
variable "ARM_CLIENT_SECRET" {
  type = string
}
variable "ARM_TENANT_ID" {
  type = string
}
variable "ARM_CLIENT_ID" {
  type = string
}resource "azurerm_virtual_network" "example_network" {
  name = "example-network"
  address_space = [ "10.0.0.0/16" ]
  location = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example_subnet" {
    name = "example-subnet"
    resource_group_name = azurerm_resource_group.example.name
    virtual_network_name = azurerm_virtual_network.example_network.name
    address_prefixes = [ "10.0.2.0/24" ]
}resource "azurerm_network_interface" "example_interface" {
    name = "example-interface"
    location = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    ip_configuration {
      name = "configuration1"
      subnet_id = azurerm_subnet.example_subnet.id
      private_ip_address_allocation = "Dynamic"
    }
}
resource "azurerm_virtual_machine" "example_vm" {
    name = "example-vm"
    location = azurerm_resource_group.example.location
    resource_group_name = azurerm_resource_group.example.name
    network_interface_ids = [azurerm_network_interface.example_interface.id]
    vm_size = "Standard_DS1_v2"
    storage_image_reference {
      publisher = "Canonical"
      offer = "UbuntuServer"
      sku = "16.04-LTS"
      version = "latest"
    }
    storage_os_disk {
      name = "myosdisk1"
      caching = "ReadWrite"
      create_option = "FromImage"
      managed_disk_type = "Standard_LRS"
    }
    os_profile {
      computer_name = "hostname"
      admin_username = "testadmin"
      admin_password = "Password1234!"
    }
    os_profile_linux_config {
      disable_password_authentication = false
    }
    tags = {
      environment = "stg"
    }
}上記のファイルを作成したら下記コマンドを実行してAzure上にデプロイします。
applyコマンドで成功したらdestroyコマンドでvmを削除します。
terraform init
terraform plan -var-file=azure.tfvars
terraform apply -var-file=azure.tfvars
terraform destroy -var-file=azure.tfvars・Appendix
参考文献はこちら
https://learn.microsoft.com/ja-jp/cli/azure/authenticate-azure-cli
https://github.com/hashicorp/terraform-provider-azurerm/issues/16155
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine
https://learn.microsoft.com/ja-jp/azure/developer/terraform/create-vm-scaleset-network-disks-hcl
https://stackoverflow.com/questions/73458004/azure-does-not-have-authorization-to-perform-action
https://qiita.com/cariandrum22/items/d153aac2d49f1562b41d
https://learn.microsoft.com/ja-jp/azure/developer/terraform/troubleshoot
https://future-architect.github.io/articles/20240325a
https://learn.microsoft.com/ja-jp/azure/virtual-machines/linux/quick-create-terraform?tabs=azure-cli
https://registry.terraform.io/providers/Azure/azapi/latest/docs
https://qiita.com/ytojima/items/d2b72ea3b620815318e6
https://learn.microsoft.com/ja-jp/azure/azure-portal/get-subscription-tenant-id
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
https://learn.microsoft.com/ja-jp/azure/developer/terraform/authenticate-to-azure?tabs=bash
https://learn.microsoft.com/ja-jp/cli/azure
https://learn.microsoft.com/ja-jp/cli/azure/azure-cli-sp-tutorial-1?tabs=bash
コメントを残す