TerraformでAzureのサービスプリンシパルをデプロイする

Terraformを利用すると、様々なAzureリソースを作成する事が可能です。

Resource Group、VNET、VMなどの基本的なリソースだけではなく、Microsoft  Entraアプリケーションオブジェクトやサービスプリンシパルオブジェクトといったリソースもデプロイできます。

本記事では、Terraformを利用してMicrosoft Entraアプリケーションオブジェクトやサービスプリンシパルオブジェクトをデプロイする方法について解説します。

なお、基本的なAzureリソースをTerraformでデプロイする方法について知りたい場合は、こちらの記事を是非参考にしてください。

blog.jbs.co.jp

前提

本記事では、下記の前提を満たしている事を想定しています。

  • Terraformをインストール済であること
  • 状態格納用のストレージアカウントを作成済であること

上記に加えて、サービスプリンシパルをデプロイする上で必要な前提を記載します。

Azure CLIがインストール済みであること

PowerShellまたはコマンドプロンプトから実行する場合、以下サイトを参照しインストールしてください。

※ Azure Cloudshellから実行する場合は不要です。 learn.microsoft.com

Entra ID権限が付与されていること

サービスプリンシパルをデプロイするためには、次のいずれかのMicrosoft Entra ビルトイン ロールが必要です。

  • アプリケーション管理者
  • アプリケーション開発者
  • クラウドアプリケーション管理者

使用するプロバイダー

Azureリソースではazurermプロバイダーを使用していましたが、Microsoft  Entra ID内にデプロイする場合は、azureadプロバイダーを使用します。

定義ファイル記載例

main.tf

プロバイダー情報、作成先サブスクリプション、状態格納用ストレージを記載します。

provider "azuread" {
  tenant_id = var.tenant_id
}

# Azure Provider
terraform {
  required_providers {
    azuread = {
      source  = "hashicorp/azuread"
      version = "2.47.0"
    }
  }

  # Store Terraform state in Azure Storage
  backend "azurerm" {
    resource_group_name  = "状態格納用ストレージアカウントがあるリソースグループ名"
    storage_account_name = "ストレージアカウント名"
    container_name       = "BLOBコンテナー名"
    # Statment File Name
    key = "任意のファイル名"
  }
}

variables.tf

定義ファイル内で使用するリソース名、変数を指定します。

#-------------------------------
# 基本変数定義
#-------------------------------

#テナントID
variable "tenant_id" {}

#---------------------------------
# Service Principal
#---------------------------------
#Entra ID アプリケーションオブジェクト名/サービスプリンシパルオブジェクト名
variable "name_app" {
  default = "test-sp001" #任意の名前
}

sp.tf

Microsoft Entraアプリケーションオブジェクト、サービスプリンシパルオブジェクトを定義します。

#----------------------------------------
#Azure App
#----------------------------------------

#Microsoft EntraIDにアクセス
data "azuread_client_config" "provider" {}

#AzureApp

#Microsoft Entraアプリケーションオブジェクト作成
resource "azuread_application" "azure_app" {
  display_name = var.name_app #Entra ID アプリケーションオブジェクト名を指定
  owners       = [data.azuread_client_config.provider.object_id] #所有者はapply実行者となる
  }

#クライアントシークレット作成
resource "azuread_application_password" "app_password" {
  application_id    = azuread_application.azure_app.id #アプリケーション(クライアント)ID
  end_date_relative = "17520h" #有効期限2年間
}

#----------------------------------------
#Service Principal
#----------------------------------------

#サービスプリンシパルオブジェクト作成
resource "azuread_service_principal" "service_principal" {
  client_id                    = azuread_application.azure_app.client_id #アプリケーション(クライアント)ID
  app_role_assignment_required = true #ロールの割り当て有効化
  owners                       = [data.azuread_client_config.provider.object_id] #所有者はapply実行者となる
}

Terraform実行

定義が完了したので、Terrformを実行していきます。

定義ファイル作成フォルダに移動し、初期化していきます。実行コマンドはterraform initです。

terraform init
Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/time from the dependency lock file
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Reusing previous version of hashicorp/azuread from the dependency lock file
- Using previously-installed hashicorp/time v0.10.0
- Using previously-installed hashicorp/azurerm v3.91.0
- Using previously-installed hashicorp/azuread v2.47.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

エラーがないことを確認します。今回作成するリソース数は3つです。実行コマンドはterraform planです。

terraform plan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Plan: 3 to add, 0 to change, 0 to destroy.

最後に、リソースを作成します。実行コマンドはterraform applyです。

確認が求められるため、yesを入力し、Enterをクリックします。

terraform apply
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Plan: 3 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

デプロイが完了すると、

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

と表示されます。

リソースの確認

AzureポータルからMicrosoft Entra ID>アプリの登録をクリックし、アプリケーションが存在するか確認します。

アプリの登録
Microsoft Entra ID>アプリの登録><作成したアプリの登録名>>証明書とシークレットをクリックし、クライアントシークレットが存在するか確認します。
クライアント

リソース削除

検証が完了したので、作成したアプリケーションを削除します。実行コマンドはterraform destroyです。

確認が求められるため、yesを入力し、Enterをクリックします。

terraform destroy
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Plan: 0 to add, 0 to change, 3 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value:yes

削除が完了すると、

Destroy complete! Resources: 3 destroyed.

と表示されます。

最後に

最後まで閲覧ありがとうございます。

この記事を見て、TerraformでMicrosoft Entra IDを操作することに興味を持っていただけたり、参考になったりすれば幸いです。

執筆担当者プロフィール
佐藤 宏樹

佐藤 宏樹(日本ビジネスシステムズ株式会社)

クラウドソリューション事業本部に所属しています。主にMicrosoft Azureに携わっています。バイク乗りです。

担当記事一覧