> ## Documentation Index
> Fetch the complete documentation index at: https://auth0.com/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/ja-jp/secure/multi-factor-authentication/auth0-guardian) の電話設定を [Unified Phone Experience (UPE)](/docs/ja-jp/customize/phone-messages/unified-phone/configure-unified-phone) に移行します。

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

<h2 id="prerequisites">
  前提条件
</h2>

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

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

<h2 id="enable-the-phone-provider-resource-and-remove-legacy-configuration">
  電話プロバイダーリソースを有効にし、レガシー設定を削除する
</h2>

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)

<h3 id="set-the-consolidated-experience-flag">
  統合エクスペリエンス フラグを設定する
</h3>

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
}
```

<h3 id="add-the-tenant-level-phone-provider">
  テナントレベルの 電話プロバイダー を追加する
</h3>

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

<Tabs>
  <Tab title="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>

  <Tab title="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/ja-jp/customize/phone-messages/configure-phone-messaging-providers/configure-a-custom-phone-provider) Action がデプロイ済みであることを前提としています。配信はこの Action が処理します。
  </Tab>
</Tabs>

<h3 id="add-the-auth0_branding_phone_notification_template-resource-optional">
  auth0\_branding\_phone\_notification\_template リソースを追加する (任意)
</h3>

レガシーな 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 のドキュメントを参照してください。
}
```

<h3 id="clean-up-the-auth0-guardian-resource-phone-block">
  Auth0 Guardian リソースの phone ブロックを整理する
</h3>

[`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]
}
```

<h3 id="plan-and-apply">
  プランを確認して適用する
</h3>

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

* `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
```

<h2 id="troubleshooting">
  トラブルシューティング
</h2>

<h3 id="403-forbidden-errors-during-terraform-apply">
  terraform apply 中の 403 Forbidden エラー
</h3>

このエラーは通常、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` を参照していることを確認してください。

<h3 id="409-conflict-error-message-type-is-not-supported-by-the-configured-phone-provider">
  409 Conflict エラー: Message Type は設定された電話プロバイダーでサポートされていません
</h3>

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

<h3 id="terraform-apply-error-provider-or-options-cannot-be-specified">
  Terraform apply エラー: Provider または options は指定できません
</h3>

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

<h3 id="custom-phone-provider-not-delivering-messages">
  カスタム電話プロバイダーでメッセージが配信されない
</h3>

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

<h3 id="otp-messages-not-arriving-after-migration">
  移行後にOTPメッセージが届かない
</h3>

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