【Terraform】Error: `subscription_id` is a required provider property
Error: subscription_id is a required provider property when performing a plan/apply operation
terraform planを実行したところ、Error: subscription_id is a required provider property when performing a plan/apply operationというエラーが発生しました。
$ terraform plan
Planning failed. Terraform encountered an error while generating this plan.
╷
│ Error: `subscription_id` is a required provider property when performing a plan/apply operation
│
│ with provider["registry.terraform.io/hashicorp/azurerm"],
│ on provider.tf line 1, in provider "azurerm":
│ 1: provider "azurerm" {
│
╵
エラーの原因
エラーの原因は、文字通り「terraform planやterraform applyを実行するときはproviderブロックにsubscription_idが必須」というものです。
修正前
元々、providerブロックにsubscription_idを指定していません。
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "4.1.0"
}
}
}
provider "azurerm" {
features {}
}
修正後
providerブロックに、リソースを更新する対象のsubscription_idを指定します。
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "4.1.0"
}
}
}
provider "azurerm" {
subscription_id = "<SUBSCRIPTION_ID>"
features {}
}
変更した後、再度初期化しなおします。
$ terraform init -upgrade
すると、terraform planが成功しました。
$ terraform plan
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:
...略
Plan: 3 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ container_name = "sample-container"
+ storage_account_name = "samplestorageaccountdev"
version4ではsubscription_idが必須(Azure CLIの場合)
「あれ?以前はsubscription_idを指定しなくても動いたけどな...?」と思ったのですが、どうやらversion4.0以降でsubscription_idが必須に変わったようです。
以前は、Azure CLIでログインしていれば、現在アクティブのサブスクリプションのIDが自動で指定されたため、以下のようにサブスクリプションIDを指定しなくても問題なかったようです。しかし、version4.0以降では明示的なサブスクリプションIDの指定が必要になっています。
provider "azurerm" {
features {}
}
公式にも
Azure Provider のバージョン 4.0 では、構成でプロバイダ インスタンスを構成するときに Azure サブスクリプション ID を指定する必要があります。
とあります。
This change will not impact users who use any authentication method other than Azure CLI, for example those authenticating as a service principal using the provider's native support, users authenticating using OIDC, or authenticating using managed identity.
なお、この件はAzure CLIを使っている場合に限られるようです。サービスプリンシパルやマネージドIDを使っている場合は、subscription_idは必須ではありません(未検証)。
取り急ぎ、「azurermのversionが4.0以降 かつ Azure CLIを使う 場合、providerブロックにsubscription_idの指定が必要」という状態でした。
以上です。
Discussion