Terraform を使用して Azure Compute Gallery を構成する
Terraform を使用すると、クラウド インフラストラクチャの定義、プレビュー、およびデプロイを行うことができます。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文では、Azure などのクラウド プロバイダーと、クラウド インフラストラクチャを構成する要素を指定できます。 構成ファイルを作成したら、"実行プラン" を作成します。これにより、インフラストラクチャの変更をデプロイ前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
この記事では、Azure Compute Gallery を構成する方法について説明します。
この記事では、次のことについて説明します。
- Terraform を使用して Azure Compute Gallery (以前のShared Image Gallery) を構成する
1. 環境を構成する
- Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。
Terraform の構成: まだ行っていない場合は、次のいずれかのオプションを使用して Terraform を構成します。
2. Terraform コードを実装する
サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。
providers.tf
という名前のファイルを作成し、次のコードを挿入します。terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } azuread = { source = "hashicorp/azuread" } } } provider "azurerm" { features {} }
main.tf
という名前のファイルを作成し、次のコードを挿入します。resource "azurerm_resource_group" "sigrg" { location = var.deploy_location name = var.rg_shared_name } # generate a random string (consisting of four characters) # https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string resource "random_string" "rando" { length = 4 upper = false special = false } # Creates Shared Image Gallery # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image_gallery resource "azurerm_shared_image_gallery" "sig" { name = "sig${random_string.random.id}" resource_group_name = azurerm_resource_group.sigrg.name location = azurerm_resource_group.sigrg.location description = "Shared images" tags = { Environment = "Demo" Tech = "Terraform" } } #Creates image definition # https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/shared_image resource "azurerm_shared_image" "example" { name = "avd-image" gallery_name = azurerm_shared_image_gallery.sig.name resource_group_name = azurerm_resource_group.sigrg.name location = azurerm_resource_group.sigrg.location os_type = "Windows" identifier { publisher = "MicrosoftWindowsDesktop" offer = "office-365" sku = "20h2-evd-o365pp" } }
variables.tf
という名前のファイルを作成し、次のコードを挿入します。
variable "deploy_location" {
type = string
default = "eastus"
description = "The Azure Region in which all resources in this example should be created."
}
variable "rg_shared_name" {
type = string
default = "rg-shared-resources"
description = "Name of the Resource group in which to deploy shared resources"
}
output.tf
という名前のファイルを作成し、次のコードを挿入します。
output "location" {
description = "The Azure region"
value = azurerm_resource_group.sigrg.location
}
output "Compute_Gallery" {
description = "Azure Compute Gallery"
value = azurerm_shared_image_gallery.sig.name
}
3. Terraform 実行プランを作成する
terraform plan を実行して、実行プランを作成します。
terraform plan -out main.tfplan
重要なポイント:
terraform plan
コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。- 省略可能な
-out
パラメーターを使用すると、プランの出力ファイルを指定できます。-out
パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。
4. Terraform 実行プランを適用する
terraform apply を実行して、クラウド インフラストラクチャに実行プランを適用します。
terraform apply main.tfplan
重要なポイント:
terraform apply
コマンドの例は、以前にterraform plan -out main.tfplan
が実行されたことを前提としています。-out
パラメーターに別のファイル名を指定した場合は、terraform apply
の呼び出しで同じファイル名を使用します。-out
パラメーターを使用しなかった場合は、パラメーターを指定せずにterraform apply
を呼び出します。
5.リソースをクリーンアップする
Terraform を使用して作成したリソースが不要になった場合は、次の手順を実行します。
terraform plan を実行して、
destroy
フラグを指定します。terraform plan -destroy -out main.destroy.tfplan
重要なポイント:
terraform plan
コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。- 省略可能な
-out
パラメーターを使用すると、プランの出力ファイルを指定できます。-out
パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。
terraform apply を実行して、実行プランを適用します。
terraform apply main.destroy.tfplan
Azure での Terraform のトラブルシューティング
Azure で Terraform を使用する場合の一般的な問題のトラブルシューティング
次のステップ
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示