Terraformはクラウドインフラストラクチャの定義、プレビュー、そしてデプロイメントを可能にします。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 あなたの設定ファイルを作成した後、実行計画を作成します。これにより、インフラストラクチャの変更をデプロイする前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
Azure リソースのマネージド ID は、Azure Active Directory に対する認証に使用されます。 HashiCorp では、Terraform を非対話型の方法で実行している場合は、サービス プリンシパルまたはマネージド ID を使用することをお勧めします。 マネージド ID には、システム割り当てとユーザー割り当ての 2 種類があります。 この記事では、システム割り当て ID の使用方法について説明します。
システム割り当てマネージド ID を定義する
システム割り当てマネージド ID を使用するには、次の手順に従います。
identity
ブロックを指定し、type
をSystemAssigned
に設定します。resource "azurerm_linux_virtual_machine" "example" { # ... identity { type = "SystemAssigned" } }
ID に
Contributor
ロールを付与します。data "azurerm_subscription" "current" {} data "azurerm_role_definition" "contributor" { name = "Contributor" } resource "azurerm_role_assignment" "example" { scope = data.azurerm_subscription.current.id role_definition_name = "Contributor" principal_id = azurerm_linux_virtual_machine.example.identity[0].principal_id }
環境変数を使用して構成し、Azure の資格情報を指定します。
export ARM_USE_MSI=true export ARM_SUBSCRIPTION_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx export ARM_TENANT_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
例: マネージド ID を使用して仮想マシンを作成する
サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。
providers.tf
という名前のファイルを作成し、次のコードを挿入します。terraform { required_version = ">=0.12" required_providers { azapi = { source = "azure/azapi" version = "~>1.5" } azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
main.tf
という名前のファイルを作成し、次のコードを挿入します。resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "azurerm_resource_group" "rg" { ___location = var.resource_group_location name = random_pet.rg_name.id } data "azurerm_subscription" "current" {} resource "azurerm_virtual_network" "example" { name = "myVnet" address_space = ["10.0.0.0/16"] ___location = azurerm_resource_group.rg.___location resource_group_name = azurerm_resource_group.rg.name } resource "azurerm_subnet" "example" { name = "mySubnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.example.name address_prefixes = ["10.0.2.0/24"] } resource "azurerm_network_interface" "example" { name = "myNic" ___location = azurerm_resource_group.rg.___location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = "internal" subnet_id = azurerm_subnet.example.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_linux_virtual_machine" "example" { name = "myVm" resource_group_name = azurerm_resource_group.rg.name ___location = azurerm_resource_group.rg.___location size = "Standard_F2" network_interface_ids = [ azurerm_network_interface.example.id, ] computer_name = "hostname" admin_username = var.username admin_ssh_key { username = var.username public_key = azapi_resource_action.ssh_public_key_gen.output.publicKey } identity { type = "SystemAssigned" } 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" } } data "azurerm_role_definition" "contributor" { name = "Contributor" } resource "azurerm_role_assignment" "example" { scope = data.azurerm_subscription.current.id role_definition_name = "Contributor" principal_id = azurerm_linux_virtual_machine.example.identity[0].principal_id }
ssh.tf
という名前のファイルを作成し、次のコードを挿入します。resource "random_pet" "ssh_key_name" { prefix = "ssh" separator = "" } resource "azapi_resource_action" "ssh_public_key_gen" { type = "Microsoft.Compute/sshPublicKeys@2022-11-01" resource_id = azapi_resource.ssh_public_key.id action = "generateKeyPair" method = "POST" response_export_values = ["publicKey", "privateKey"] } resource "azapi_resource" "ssh_public_key" { type = "Microsoft.Compute/sshPublicKeys@2022-11-01" name = random_pet.ssh_key_name.id ___location = azurerm_resource_group.rg.___location parent_id = azurerm_resource_group.rg.id } output "key_data" { value = azapi_resource_action.ssh_public_key_gen.output.publicKey } ```
variables.tf
という名前のファイルを作成し、次のコードを挿入します。variable "resource_group_location" { type = string description = "Location of the resource group." default = "eastus" } variable "resource_group_name_prefix" { type = string description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." default = "rg" } variable "username" { type = string description = "The username for the local account that will be created on the new VM." default = "azureadmin" }
outputs.tf
という名前のファイルを作成し、次のコードを挿入します。output "resource_group_name" { value = azurerm_resource_group.rg.name } output "azurerm_linux_virtual_machine_name" { value = azurerm_linux_virtual_machine.example.name }