指定 Databricks 资产捆绑包工作流的运行标识

重要

此功能目前以公共预览版提供。

本文介绍如何使用 run_as 设置来指定运行 Databricks 资产捆绑包工作流时要使用的标识。

可以将 run_as 设置配置为应用于资源的顶级映射,也可以将其配置为捆绑包配置文件中的 target 部署映射。 可以将其设置为 user_nameservice_principal_name

此设置提供了将用于部署捆绑作业或管道的标识,与要运行的作业或管道工作流使用的标识分开的功能。 这提高了捆绑包开发和管理的灵活性,同时还允许为部署和运行建立保护机制。 具体而言:

  • 如果用于部署捆绑包的标识与捆绑包的 run_as 设置中配置的标识相同,则没有任何限制。 支持所有捆绑包资源
  • 如果用于部署捆绑包的标识与捆绑包的 run_as 设置中配置的标识不同,则仅支持一部分捆绑包资源。 不支持管道和模型服务终结点。

设置捆绑包运行标识

若要设置捆绑包资源的运行标识,请将 run_as 指定为顶级映射,如以下示例所示:

bundle:
  name: "run_as"

# This is the identity that will be used when "databricks bundle run my_test_job" is executed.
run_as:
  service_principal_name: 5cf3z04b-a73c-4x46-9f3d-52da7999069e

resources:
  jobs:
    my_test_job _1:
      name: Test job 1
      tasks:
        - task_key: "task_1"
          new_cluster:
            num_workers: 1
            spark_version: 13.2.x-snapshot-scala2.12
            node_type_id: i3.xlarge
            runtime_engine: PHOTON
          notebook_task:
            notebook_path: "./test.py"
    my_test_job_2:
      name: Test job 2
      run_as:
        service_principal_name: "my_service_principal_for_job_2"
      tasks:
        - task_key: "task_2"
          notebook_task:
            notebook_path: "./test.py"

重要

管道或模型服务终结点不支持 run_as 设置。 如果在定义这些资源的捆绑包中还配置了 run_as,则会发生错误。

设置目标部署标识

最佳做法是为暂存和生产目标部署配置运行标识。 此外,为生产目标的服务主体设置 run_as 标识是运行生产工作流的最安全方法,因为:

  • 确保工作流由同一服务主体部署,或者由对服务主体本身具有 CAN_USE 权限的人员部署。
  • 将运行生产工作流的权限与创建或部署了捆绑包的标识分离。
  • 允许用户使用比用于部署生产捆绑包的标识更少的权限来配置和设置生产服务主体。

在以下示例 databricks.yml 配置文件中,配置了三种目标模式:开发、暂存和生产。 开发模式配置为以个人用户身份运行,暂存和生产模式配置为使用两个不同的服务主体运行。

bundle:
  name: my_targeted_bundle

run_as:
  service_principal_name: 5cf3z04b-a73c-4x46-9f3d-52da7999069e

targets:
  # Development deployment settings, set as the default
  development:
    mode: development
    default: true
    workspace:
      host: https://my-host.cloud.databricks.com
    run_as:
      user_name: someone@example.com

  # Staging deployment settings
  staging:
    workspace:
      host: https://my-host.cloud.databricks.com
      root_path: /Shared/staging-workspace/.bundle/${bundle.name}/${bundle.target}
    run_as:
      service_principal_name: "my_staging_service_principal"

  # Production deployment settings
  production:
    mode: production
    workspace:
      host: https://my-host.cloud.databricks.com
      root_path: /Shared/production-workspace/.bundle/${bundle.name}/${bundle.target}
    run_as:
      service_principal_name: "my_production_service_principal"

resources:
  jobs:
    my_test_job:
      name: Test job
      tasks:
        - task_key: "task"
          new_cluster:
            num_workers: 1
            spark_version: 13.3.x-cpu-ml-scala2.12
            node_type_id: i3.xlarge
            runtime_engine: STANDARD
          notebook_task:
            notebook_path: "./test.py"