宣言型オートメーション バンドルを使用した一般的な Lakebase プロジェクトのセットアップ

Important

Lakebaseの宣言的自動化バンドルのサポートは ベータ版です。

このページでは、本番環境のLakebaseプロジェクト向けのDeclarative Automation Bundles(宣言的自動化バンドル)を、最もよく使われる機能とともに紹介しています:

  • 保護された運用ブランチ
  • 読み取り可能なセカンダリを使用した高可用性 (HA) 読み取り/書き込みエンドポイント
  • サービス プリンシパルのインライン ワークスペース レベルの CAN_MANAGE アクセス許可
  • Unity カタログからの継続的同期テーブル ストリーミング
  • Lakebase データベースの Unity カタログ バインド
  • Lakebase プロジェクトに接続されている Databricks アプリ

Lakebase を使用した宣言型オートメーション バンドルの詳細な概要については、「宣言型オートメーション バンドルを 使用した Lakebase の管理」を参照してください。

前提条件

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

  • Databricks CLI v1.0.0 以降。 バージョンを確認するには、 databricks --versionを実行します。 インストールまたはアップグレードするには、 Databricks CLI のインストールまたは更新に関する記事を参照してください。
  • Lakebase が有効になっているAzure Databricks ワークスペース。
  • OAuth マシン間 (M2M) 認証用に構成されたサービス プリンシパル。 バンドルにより、このプリンシパル ワークスペース CAN_MANAGE プロジェクトに対するアクセス許可が付与されます。 OAuth を使用した Azure Databricks へのサービス プリンシパル アクセスの承認プロジェクトのアクセス許可の管理に関するページを参照してください。
  • 同期ソースとして使用できるように変更データ フィード (CDF) が有効になっている Unity カタログ デルタ テーブル。 データ同期が必要ない場合は、 postgres_synced_tables ブロックと postgres_catalogs ブロックを削除します。

バンドル構成の完了

バンドルでは、ワークスペース固有のすべての値に変数が使用されます。 .databricks/bundle/<target>/variables.json ファイルに設定するか、--varを使用してデプロイ時に渡します。

プロジェクトを作成すると、Azure Databricksproductionブランチ、primary読み取り/書き込みエンドポイント、ID に関連付けられた所有者 Postgres ロール、およびdatabricks_postgres データベースが自動的に作成されます。 これらの暗黙的に作成されたリソースを構成するには、 replace_existing: trueで宣言します。

bundle:
  name: lakebase-typical-project

variables:
  project_id:
    description: 'Lakebase project ID (lowercase, hyphen-delimited)'
    default: 'my-lakebase-project'
  display_name:
    description: 'Human-readable project name shown in the UI'
    default: 'My Lakebase project'
  pg_version:
    description: 'Postgres major version'
    default: 17
  min_cu:
    description: 'Minimum compute units on the default endpoint'
    default: 0.5
  max_cu:
    description: 'Maximum compute units on the default endpoint'
    default: 4.0
  suspend_timeout:
    description: 'Idle time before the default endpoint suspends. Ignored when no_suspension is true.'
    default: '300s'
  admin_sp_app_id:
    description: 'Application ID of the service principal to grant CAN_MANAGE on the project'
    default: '<your-sp-application-id>'
  source_table:
    description: 'Unity Catalog three-part name of the Delta table to sync (catalog.schema.table)'
    default: '<catalog>.<schema>.<table>'
  primary_key_column:
    description: 'Primary key column of the source Delta table'
    default: '<pk>'
  storage_catalog:
    description: 'Unity Catalog catalog where the sync pipeline stores its metadata'
    default: '<catalog>'
  storage_schema:
    description: 'Unity Catalog schema where the sync pipeline stores its metadata'
    default: '<schema>'
  app_name:
    description: 'Databricks App name (must be unique in the workspace)'
    default: 'my-lakebase-app'
  uc_catalog_id:
    description: 'Name to register the Lakebase database in Unity Catalog'
    default: 'my_lakebase_uc_catalog'
  database_name:
    description: 'Postgres-internal name for the app database'
    default: 'app_database'

targets:
  prod:
    default: true
    workspace:
      host: https://<your-workspace>.cloud.databricks.com

    resources:
      # Project — top-level container for branches, endpoints, and databases.
      # The permissions block grants workspace-level CAN_MANAGE to the service principal.
      postgres_projects:
        lakebase_project:
          project_id: ${var.project_id}
          # purge_on_delete: true  # Uncomment to permanently delete on destroy (default: soft delete, 7-day retention).
          pg_version: ${var.pg_version}
          display_name: ${var.display_name}
          default_endpoint_settings:
            autoscaling_limit_min_cu: ${var.min_cu}
            autoscaling_limit_max_cu: ${var.max_cu}
            suspend_timeout_duration: ${var.suspend_timeout}
          permissions:
            - service_principal_name: ${var.admin_sp_app_id}
              level: CAN_MANAGE

      # Configure the implicitly created production branch as protected.
      postgres_branches:
        production:
          branch_id: production
          parent: ${resources.postgres_projects.lakebase_project.name}
          no_expiry: true
          is_protected: true
          replace_existing: true

      # Configure the implicitly created primary endpoint with HA.
      # HA requires no_suspension: true. group.min: 2 adds a standby for automatic failover.
      postgres_endpoints:
        primary:
          endpoint_id: primary
          parent: ${resources.postgres_branches.production.name}
          endpoint_type: ENDPOINT_TYPE_READ_WRITE
          autoscaling_limit_min_cu: ${var.min_cu}
          autoscaling_limit_max_cu: ${var.max_cu}
          no_suspension: true
          group:
            min: 2
            max: 2
            enable_readable_secondaries: true
          replace_existing: true

      # Postgres role that owns the app database.
      postgres_roles:
        app_role:
          role_id: app-role # Resource ID: lowercase letters, digits, and hyphens.
          parent: ${resources.postgres_branches.production.name}
          postgres_role: app_role # Postgres identifier: lowercase letters, digits, and underscores.

      # Named Postgres database for the app.
      postgres_databases:
        app_db:
          database_id: app-database
          parent: ${resources.postgres_branches.production.name}
          postgres_database: ${var.database_name}
          role: ${resources.postgres_roles.app_role.id}

      # Sync a Unity Catalog Delta table into the project continuously.
      postgres_synced_tables:
        orders_sync:
          synced_table_id: '${var.storage_catalog}.${var.storage_schema}.orders_synced'
          branch: ${resources.postgres_branches.production.name}
          postgres_database: ${var.database_name}
          source_table_full_name: ${var.source_table}
          primary_key_columns:
            - ${var.primary_key_column}
          scheduling_policy: CONTINUOUS
          create_database_objects_if_missing: true
          new_pipeline_spec:
            storage_catalog: ${var.storage_catalog}
            storage_schema: ${var.storage_schema}

      # Bind the Lakebase database into Unity Catalog so it is queryable as UC data.
      postgres_catalogs:
        lakebase_uc_catalog:
          catalog_id: ${var.uc_catalog_id}
          postgres_database: ${var.database_name}
          branch: ${resources.postgres_branches.production.name}
          create_database_if_missing: true

      # Databricks App connected to the project.
      # Update source_code_path to point to your app source directory.
      apps:
        lakebase_app:
          name: ${var.app_name}
          description: 'App backed by Lakebase autoscaling'
          source_code_path: ./app_src
          config:
            command:
              - flask
              - run
              - --host=0.0.0.0
              - --port=8000
          resources:
            - name: lakebase-db
              postgres:
                branch: ${resources.postgres_branches.production.name}
                database: ${resources.postgres_databases.app_db.name}
                permission: CAN_CONNECT_AND_CREATE

Note

すべての Lakebase プロジェクトでは、ID に関連付けられた Postgres ロールが所有する databricks_postgres データベースが自動的に作成されます。 このバンドルでは、専用アプリ ロールが所有する個別の名前付きデータベース (${var.database_name}) を作成して、代わりにアプリ データを分離します。 暗黙的なデータベースとロールを直接使用するには、postgres_rolespostgres_databasesリソース ブロックを削除し、postgres_database: databricks_postgrespostgres_synced_tablesに直接postgres_catalogsを設定し、アプリ リソースをdatabase: ${resources.postgres_branches.production.name}/databases/databricks-postgresに更新します。

代わりに、暗黙的な所有者ロールと databricks_postgres データベースをバンドル管理の下に置くには、既存の ID を使用して replace_existing: true で宣言します。 データベース ID は常に databricks-postgres。 ロール ID は、固定名ではなく Databricks ID から派生しているため、最初に参照してください。

databricks postgres list-roles projects/<project-id>/branches/production

次に、ロールに既に設定されているすべてのフィールドに一致する両方のリソースを宣言します。 membership_rolesを省略すると、DATABRICKS_SUPERUSERメンバーシップが採用されたときにロールから削除されるため、明示的に宣言します。

postgres_roles:
  owner:
    role_id: <role-id-from-list-roles>
    parent: ${resources.postgres_branches.production.name}
    postgres_role: user@databricks.com # Or the service principal application ID.
    identity_type: USER # Or SERVICE_PRINCIPAL.
    membership_roles:
      - DATABRICKS_SUPERUSER
    replace_existing: true

postgres_databases:
  databricks_postgres:
    database_id: databricks-postgres
    parent: ${resources.postgres_branches.production.name}
    postgres_database: databricks_postgres
    role: ${resources.postgres_roles.owner.id}
    replace_existing: true

Note

このバンドルによって作成されるリソースを破棄するには、 databricks bundle destroy -t prodを実行します。 既定では、プロジェクトは論理的に削除され、完全削除の前に 7 日間保持されるため、保持期間中に回復できます。 プロジェクトのみを直ちに削除するには、Databricks CLI を --purgeと共に使用するか、上記のプロジェクト リソースの purge_on_delete: true をコメント解除して、破棄ごとにハード削除します。

databricks postgres delete-project projects/<project-id> --purge

バンドルを適用する

検証とデプロイ:

databricks bundle validate -t prod
databricks bundle deploy -t prod

databricks bundle deploy最初の実行時に完了しない場合は、再実行します。

何がデプロイされるか

バンドルによって、次のリソースが作成されます。

  • あなたが指定した計算デフォルトを持つLakebaseプロジェクトです。
  • 保護された production ブランチ。
  • HA と読み取り可能なセカンダリを持つプライマリ読み取り/書き込みエンドポイント。
  • Unity カタログ デルタ テーブルをプロジェクト データベースにストリーミングする継続的同期パイプライン。
  • Unity カタログ データとしてクエリ可能な、Lakebase データベースによってサポートされる Unity カタログ カタログ。
  • プロジェクト データベースに接続されている Databricks アプリ。
  • 指定したサービス プリンシパルに対するワークスペース CAN_MANAGE のアクセス許可

その他のリソース