タダです.
Azure を触る機会が増えそうかつ Terraform を使った管理をする必要があるため,備忘録としてこの記事では Azure のリソースグループを Terraform で作ってみた内容をまとめていきます.
事前準備
事前準備として次のものを用意します.2つ目の Azure CLI は Terraform 実行で必要で,今回の作業でインストールせず terraform plan
を実行すると怒られます.
- Azure アカウント(無料試用版を使っています)
- Azure CLI
az
- Terraform のインストール
怒られたレスポンス
❯❯ terraform plan ╷ │ Error: building AzureRM Client: please ensure you have installed Azure CLI version 2.0.79 or newer. Error parsing json result from the Azure CLI: launching Azure CLI: exec: "az": executable file not found in $PATH. │ │ with provider["registry.terraform.io/hashicorp/azurerm"], │ on providers.tf line 16, in provider "azurerm": │ 16: provider "azurerm" { │ ╵
関連情報
リソースグループ作成のコード作成と適用
リソースグループ作成の Terraform のコードを書いていきます.ちなみに,リソースグループとは Azure の中で 管理グループ > サブスクリプション
の下にある概念で Azure のサービスリソースを管理するために必要です.リソースグループで管理されたサービスリソースは一覧表示化できたり,まとめて削除できたりします.コードは Azure のドキュメントをベースに一部変更しており,東日本リージョンにリソースグループを作るようにしています.
providers.tf
terraform { required_version = ">=0.12" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>2.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
resource_group.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" { default = "japaneast" description = "Location of the resource group." } variable "resource_group_name_prefix" { default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." }
リソースグループの作成
❯❯❯ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # azurerm_resource_group.rg will be created + resource "azurerm_resource_group" "rg" { + id = (known after apply) + location = "japaneast" + name = (known after apply) } # random_pet.rg_name will be created + resource "random_pet" "rg_name" { + id = (known after apply) + length = 2 + prefix = "rg" + separator = "-" } Plan: 2 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes random_pet.rg_name: Creating... random_pet.rg_name: Creation complete after 0s [id=rg-darling-mastodon] azurerm_resource_group.rg: Creating... azurerm_resource_group.rg: Creation complete after 1s [id=/subscriptions/7a60c864-0425-42d2-96d1-4b70f1741ecf/resourceGroups/rg-darling-mastodon] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
作成後の確認
関連情報
まとめ
リソースグループの作成を Terraform でやってみました.次は IAM リソースの管理をやってみたいと思います.