😀

Terraform の AzAPI 2.0 のみで Azure リソース作成を試してみた

に公開

Terraform の AzureRM プロバイダーにリソースの定義がない場合、Azure REST API の仕組みが理解できれば AzAPI プロバイダーを使うことで、例えばリリースされたばかりの Azure サービスにも Terraform を使うことができます。今回は AzAPI がバージョン 2.0 になったので、復習も兼ねて AzAPI だけで Azure リソースを作成を試してみました。

検証用 main.tf

リソースグループとストレージアカウントを作成します。

main.tf
terraform {
  required_providers {
    azapi = {
      source = "azure/azapi"
    }
  }
}

provider "azapi" {
}

variable "prefix" {
  type    = string
  default = "mnrazapi"
}

variable "region" {
  type    = string
  default = "japaneast"
}

resource "azapi_resource" "rg" {
  type     = "Microsoft.Resources/resourceGroups@2022-09-01"
  name     = "${var.prefix}-rg"
  location = var.region
}

resource "azapi_resource" "sa" {
  type      = "Microsoft.Storage/storageAccounts@2018-02-01"
  name      = "${var.prefix}sa"
  location  = var.region
  parent_id = azapi_resource.rg.id

  body = {
    sku = {
      name = "Standard_LRS"
    }
    kind = "StorageV2"
  }
}

Terraform 初期化

azure/azapi v2.0.1 が使用されていることを確認できます。

bash
$ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of azure/azapi...
- Installing azure/azapi v2.0.1...
- Installed azure/azapi v2.0.1 (signed by a HashiCorp partner, key ID 6F0B91BDE98478CF)

Terraform 実行

bash
$ terraform plan

$ terraform apply -auto-approve

作成された Azure リソース

terraform-azapi2-01.png

後片付け

bash
$ terraform destroy

参考

https://techcommunity.microsoft.com/t5/azure-tools-blog/announcing-azapi-2-0/ba-p/4275733

https://registry.terraform.io/providers/Azure/azapi/latest/docs

https://github.com/Azure/terraform-provider-azapi

https://learn.microsoft.com/ja-jp/rest/api/storagerp/storage-sample-create-account

Discussion