> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Terraform を使用して Unified Phone Experience に移行する

> Auth0 Terraform Provider を使用して、レガシーな Guardian の電話設定から Unified Phone Experience に移行する方法を学びます。

Auth0 の [Terraform Provider](https://registry.terraform.io/providers/auth0/auth0/latest/docs) を使用して、テナントのレガシーな [Auth0 Guardian](/docs/docs/ja-jp/secure/multi-factor-authentication/auth0-guardian) の電話設定を [Unified Phone Experience (UPE)](/docs/docs/ja-jp/customize/phone-messages/unified-phone/configure-unified-phone) に移行します。

移行後は、すべての [多要素認証 (MFA) の電話通知](/docs/docs/ja-jp/customize/phone-messages/unified-phone/use-auth0s-unified-phone-experience-for-multi-factor-authentication) が、1 つのテナントレベルの電話プロバイダーを通じて送信されます。

## 前提条件

移行を開始する前に、次のものが必要です。

* [Auth0 Terraform Provider v1.49.0](https://registry.terraform.io/providers/auth0/auth0/latest) 以降のバージョン。
* カスタム電話プロバイダーを設定する場合は、メッセージ配信を処理するための [`custom-phone-provider`](/docs/docs/ja-jp/customize/phone-messages/configure-phone-messaging-providers/configure-a-custom-phone-provider) Auth0 Action を作成してデプロイします。

## 電話プロバイダーリソースを有効にし、レガシー設定を削除する

Terraform の `auth0_phone_provider` リソースを有効にし、レガシー設定を整理するには、次の手順を行います。

* [`phone_consolidated_experience` フラグを `true` に設定する](#set-the-phone-consolidated-experience-flag-to-true)
* [テナントレベルの電話プロバイダーを追加する](#add-the-tenant-level-phone-provider)
* [`auth0_branding_phone_notification_template` リソースを追加する (任意) ](#add-the-auth0-branding-phone-notification-template-resource-optional)
* [`auth0_guardian` リソースの phone ブロックを整理する](#clean-up-the-auth0-guardian-resource-phone-block)
* [`plan` と `apply` を実行する](#plan-and-apply)

### 統合エクスペリエンス フラグを設定する

Auth0 テナントで UPE を有効にし、すべての MFA の電話通知が統合プロバイダー経由で送信されるようにするには、[`auth0_tenant`](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/tenant) リソースを編集し、`phone_consolidated_experience` フラグを `true` に設定します。

```hcl wrap lines theme={null}
resource "auth0_tenant" "main" {
    # ... その他のテナント設定 ...

    phone_consolidated_experience = true
}
```

### テナントレベルの 電話プロバイダー を追加する

Unified Phone Experience 配下のすべての電話ベースのフローでの配信を処理するために、Terraform の [`auth0_phone_provider`](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/phone_provider) リソースを使用して、テナントレベルの 電話プロバイダー を作成します。

> [tab] Twilio
>
> ```hcl wrap lines theme={null}
> resource "auth0_phone_provider" "default" {
>     name = "twilio"
>
>     configuration {
>         delivery_methods = ["text", "voice"]
>         default_from     = "YOUR_DEFAULT_PHONE_NUMBER"
>         sid              = var.twilio_sid
>         mssid            = var.twilio_messaging_service_sid # 任意
>     }
>
>     credentials {
>         auth_token = var.twilio_auth_token
>     }
> }
> ```
>
> SMS のみを送信する場合は、`delivery_methods` を `["text"]` に設定します。

> [tab] Custom (Actions)
>
> ```hcl wrap lines theme={null}
> resource "auth0_phone_provider" "default" {
>     name = "custom"
>
>     configuration {
>         delivery_methods = ["text"]
>     }
> }
> ```
>
> [custom 電話プロバイダー](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/phone_provider) を使用する場合、`terraform apply` を実行する前に、Auth0 では [`custom-phone-provider`](/docs/docs/ja-jp/customize/phone-messages/configure-phone-messaging-providers/configure-a-custom-phone-provider) Action がデプロイ済みであることを前提としています。配信はこの Action が処理します。

### auth0\_branding\_phone\_notification\_template リソースを追加する (任意)

レガシーな Guardian の phone ブロックで `enrollment_message` または `verification_message` を使用してワンタイムパスコード (OTP) のメッセージ本文をカスタマイズしている場合は、[`auth0_branding_phone_notification_template`](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/branding_phone_notification_template) リソースでその動作を再現できます。

```hcl wrap lines theme={null}
resource "auth0_branding_phone_notification_template" "default" {
    # 利用可能なテンプレート変数については、Auth0 Terraform Provider のドキュメントを参照してください。
}
```

### Auth0 Guardian リソースの phone ブロックを整理する

[`auth0_guardian`](https://registry.terraform.io/providers/auth0/auth0/latest/docs/resources/guardian) の phone ブロックから、レガシーなプロバイダー固有の属性をすべて削除します。

整理後、このブロックに含めるのは `enabled` 属性と `message_types` 属性、および `auth0_tenant.main` リソースと `auth0_phone_provider.default` リソースを参照する `depends_on` ブロックのみです。これにより、レガシーな Guardian の phone ブロックより前に、UPE と phone-provider の設定が適用されます。

**変更前:**

```hcl wrap lines theme={null}
resource "auth0_guardian" "default" {
    phone {
        enabled       = true
        provider      = "twilio"
        message_types = ["sms"]

        options {
            enrollment_message   = "Your enrollment code is {{code}}"
            verification_message = "Your verification code is {{code}}"
        }

        twilio_config {
            sid                   = var.twilio_sid
            auth_token            = var.twilio_auth_token
            from                  = "YOUR_DEFAULT_PHONE_NUMBER"
            messaging_service_sid = var.twilio_messaging_service_sid
        }
    }
}
```

**変更後：**

```hcl wrap lines theme={null}
resource "auth0_guardian" "default" {
    phone {
        enabled       = true
        message_types = ["sms"]
    }
    depends_on = [auth0_tenant.main, auth0_phone_provider.default]
}
```

### プランを確認して適用する

適用する前に、プランを注意深く確認してください。次のように表示されるはずです。

* `auth0_phone_provider.default` が作成される
* `auth0_guardian.default` が更新される (属性は削除)
* `auth0_tenant.main` が更新される (`phone_consolidated_experience = true`)
* `auth0_branding_phone_notification_template.default` が作成される (追加した場合)

```bash wrap lines theme={null}
terraform plan
terraform apply
```

## トラブルシューティング

### terraform apply 中の 403 Forbidden エラー

このエラーは通常、Terraform がレガシーな Guardian の phone 属性を管理しようとした時点で、`phone_consolidated_experience` がすでに `true` に設定されていたことを示しています。Terraform が正しい順序で適用できるよう、[`depends_on`](#clean-up-the-auth0-guardian-resource-phone-block) ブロックで `auth0_tenant.main` と `auth0_phone_provider.default` を参照していることを確認してください。

### 409 Conflict エラー: Message Type は設定された電話プロバイダーでサポートされていません

このエラーは、[`auth0_guardian`](#clean-up-the-auth0-guardian-resource-phone-block) リソースの `message_types` の値が、設定された電話プロバイダーでサポートされていないことを示します。プロバイダーの `delivery_methods` に、要求した `message_types` が含まれていることを確認してください。`text` 配信には `message_types: ["sms"]` を、`voice` 配信には `message_types: ["voice"]` を使用してください。

### Terraform apply エラー: Provider または options は指定できません

UPE が有効な状態で、レガシー `auth0_guardian` リソースに `provider` 属性または `options` ブロックがまだ指定されています。`phone` ブロックに `enabled` と `message_types` の設定だけが含まれるように、[設定](#clean-up-the-auth0-guardian-resource-phone-block) から `provider` と `options` を削除してください。

### カスタム電話プロバイダーでメッセージが配信されない

`terraform apply` を実行する前に、[custom-phone-provider](/docs/docs/ja-jp/customize/phone-messages/configure-phone-messaging-providers/configure-a-custom-phone-provider) トリガーを持つ Auth0 Action が Auth0 に存在し、デプロイ済みであることを確認してください。

### 移行後にOTPメッセージが届かない

`auth0_phone_provider` の資格情報が正しいことと、SMS 用に `delivery_methods` に `"text"` が含まれていることを確認してください。以前 Messaging Service SID を使用していた場合は、新しいプロバイダーに `mssid` が設定されていることを確認してください。
