LoginSignup
0
0

概要

クイックスタートを参考にTerraformからAzure リソースグループを作成してみました。
作成した際の注意点や実際に使用したコマンドをまとめます。
(Azureのアカウントは個人のアカウントのため、業務などに適用する場合は環境の差異が多少なりともありそう。。)

環境

検証開始

1. 環境作成:Windows x Terraform

「Bash を使用して Windows に Terraform をインストールする」を参考にまずは環境を整えます。
Azureアカウントは個人のアカウントでも大丈夫です!(リソースグループ作るだけなら無料!)
リンク先で特に重要なのが「5. Azure に対して Terraform を認証する」のサービスプリンシパルの作成で、コマンドの実行結果はテキストに保存しておくことをお勧めします。
image.png
サービスプリンシパルを作成するとAzurePortalで確認できます。
image.png

2. Terraformのコード実装

環境ができたところでクイックスタートに沿ってコードを作成していきます。

  • providers.tf
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "~>3.0"
    }
  }
}

provider "azurerm" {
# 環境によっては下記コードを有効にする
#  skip_provider_registration = true
  features {}

  subscription_id   = "{サブスクリプション ID}"
  tenant_id         = "{ディレクトリ (テナント) ID}"
  client_id         = "{アプリケーション (クライアント) ID}"
  client_secret     = "{クライアントシークレット値}"
}
  • 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
}
  • variables.tf
variable "resource_group_location" {
  type        = string
  default     = "japaneast"
  description = "Location of the resource group."
}

variable "resource_group_name_prefix" {
  type        = string
  default     = "rg"
  description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
}
  • outputs.tf
output "resource_group_name" {
  value = azurerm_resource_group.rg.name
}

3. Terraform初期化

作成したコードが配置されているディレクトリに移動し初期化コマンドを実行します。

terraform init -upgrade

4. Terraform実行プラン作成

実装したコードをもとにAzureのリソースグループを作成する実行プランを作成します。

terraform plan -out main.tfplan

コマンドが成功するとこんな結果が返ってきます。
image.png

このコマンドを実行した場合、環境によっては失敗することがあります。
Terraformは実行に必要なリソースプロバイダーを自動で登録するようになっているのですが、権限などが適切に設定されていない場合失敗します。
参考にさせていただいたサイト:https://future-architect.github.io/articles/20240325a/

私の個人の環境ではこの問題が一度だけ起き、Terraformによる自動登録をスキップし手動でAzureCLIから対象となるリソースプロバイダーを有効にしました。
自動登録をスキップするには本記事の「providers.tf」にある「skip_provider_registration = true」を有効にします。
リソースプロバイダを登録するAzureCLIのコマンドは長いためページ下部に貼ります。

5. Terraform実行プラン適用

作成した実行プランを実行し、Azure Portalに作成されたことを確認します。

terraform apply main.tfplan

コマンドが成功するとこんな結果が返ってきます。
image.png

実際にAzure Portalにアクセスすると出来上がったリソースグループが確認できます。
image.png

6. 最後に作成したリソースのクリーンアップ

クリーンアップする実行プランを作成し、実行して完了!

terraform plan -destroy -out main.destroy.tfplan

image.png

terraform apply main.destroy.tfplan

image.png

AzureCLIからリソースプロバイダを登録

対象とするリソースプロバイダはこちらを参照
https://github.com/hashicorp/terraform-provider-azurerm/blob/main/internal/resourceproviders/required.go

az provider register --namespace Microsoft.AppConfiguration
az provider register --namespace Microsoft.ApiManagement
az provider register --namespace Microsoft.AppPlatform
az provider register --namespace Microsoft.Authorization
az provider register --namespace Microsoft.Automation
az provider register --namespace Microsoft.AVS
az provider register --namespace Microsoft.Blueprint
az provider register --namespace Microsoft.BotService
az provider register --namespace Microsoft.Cache
az provider register --namespace Microsoft.Cdn
az provider register --namespace Microsoft.CognitiveServices
az provider register --namespace Microsoft.Compute
az provider register --namespace Microsoft.ContainerInstance
az provider register --namespace Microsoft.ContainerRegistry
az provider register --namespace Microsoft.ContainerService
az provider register --namespace Microsoft.CostManagement
az provider register --namespace Microsoft.CustomProviders
az provider register --namespace Microsoft.Databricks
az provider register --namespace Microsoft.DataFactory
az provider register --namespace Microsoft.DataLakeAnalytics
az provider register --namespace Microsoft.DataLakeStore
az provider register --namespace Microsoft.DataMigration
az provider register --namespace Microsoft.DataProtection
az provider register --namespace Microsoft.DBforMariaDB
az provider register --namespace Microsoft.DBforMySQL
az provider register --namespace Microsoft.DBforPostgreSQL
az provider register --namespace Microsoft.DesktopVirtualization
az provider register --namespace Microsoft.Devices
az provider register --namespace Microsoft.DevTestLab
az provider register --namespace Microsoft.DocumentDB
az provider register --namespace Microsoft.EventGrid
az provider register --namespace Microsoft.EventHub
az provider register --namespace Microsoft.HDInsight
az provider register --namespace Microsoft.HealthcareApis
az provider register --namespace Microsoft.GuestConfiguration
az provider register --namespace Microsoft.KeyVault
az provider register --namespace Microsoft.Kusto
az provider register --namespace microsoft.insights
az provider register --namespace Microsoft.Logic
az provider register --namespace Microsoft.MachineLearningServices
az provider register --namespace Microsoft.Maintenance
az provider register --namespace Microsoft.ManagedIdentity
az provider register --namespace Microsoft.ManagedServices
az provider register --namespace Microsoft.Management
az provider register --namespace Microsoft.Maps
az provider register --namespace Microsoft.MarketplaceOrdering
az provider register --namespace Microsoft.Media
az provider register --namespace Microsoft.MixedReality
az provider register --namespace Microsoft.Network
az provider register --namespace Microsoft.NotificationHubs
az provider register --namespace Microsoft.OperationalInsights
az provider register --namespace Microsoft.OperationsManagement
az provider register --namespace Microsoft.PolicyInsights
az provider register --namespace Microsoft.PowerBIDedicated
az provider register --namespace Microsoft.Relay
az provider register --namespace Microsoft.RecoveryServices
az provider register --namespace Microsoft.Resources
az provider register --namespace Microsoft.SignalRService
az provider register --namespace Microsoft.Search
az provider register --namespace Microsoft.Security
az provider register --namespace Microsoft.SecurityInsights
az provider register --namespace Microsoft.ServiceBus
az provider register --namespace Microsoft.ServiceFabric
az provider register --namespace Microsoft.Sql
az provider register --namespace Microsoft.Storage
az provider register --namespace Microsoft.StreamAnalytics
az provider register --namespace Microsoft.TimeSeriesInsights
az provider register --namespace Microsoft.Web

参考にしたページ

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