View a markdown version of this page

搭配 Amazon SageMaker Unified Studio 使用受信任身分傳播 - Amazon EMR

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

搭配 Amazon SageMaker Unified Studio 使用受信任身分傳播

本教學課程說明如何使用受信任身分傳播 (TIP) 設定 EMR Serverless 應用程式。然後,您可以透過 Amazon SageMaker Unified Studio 使用身分型存取控制來查詢範例資料表。透過此設定,EMR Serverless 會使用自己的 IAM Identity Center 身分執行每個使用者的 Spark 查詢,而 Lake Formation 會對 Glue Data Catalog AWS 資料表強制執行每個使用者的許可。

EMR Studio 互動式工作負載不支援 TIP

透過 EMR Studio 的互動式工作負載不支援 EMR Serverless 的受信任身分傳播。使用 Amazon SageMaker Unified Studio 做為啟用 TIP 互動式工作負載的用戶端應用程式。

成本

本教學課程使用可能產生費用 AWS 的服務,包括 EMR Serverless AWS Lake Formation、 AWS IAM Identity Center、Amazon Simple Storage Service 和 Amazon SageMaker Unified Studio。若要避免持續收費,請遵循本教學課程結尾的清除步驟。如需定價資訊,請參閱每個服務的定價頁面。

先決條件和設定

完成下列步驟,在查詢具有受信任身分傳播的資料之前設定所有必要的資源。

步驟 1:設定 IAM Identity Center

  1. 在 AWS 帳戶中啟用 IAM Identity Center。IAM Identity Center 執行個體只能存在於每個帳戶的單一區域中。

    重要

    請注意您的 IAM Identity Center 部署在哪個區域。所有後續資源 (Lake Formation、EMR Serverless、 AWS Glue) 必須在同一區域中建立。例如,如果您的 IAM Identity Center 位於 us-west-2 中,則所有資源也必須位於 us-west-2 中。

  2. 手動或從外部身分提供者 (例如 Okta 或 Microsoft Entra ID) 同步,將使用者和群組佈建至 IAM Identity Center。

  3. 請記下您的 IAM Identity Center 執行個體 ARN。您將需要在後續步驟中使用它。在 IAM Identity Center 主控台的設定下找到它。

    若要以程式設計方式尋找執行個體,請執行:

    aws sso-admin list-instances --region your-region

    輸出範例:

    { "Instances": [ { "InstanceArn": "arn:aws:sso:::instance/ssoins-1234567890abcdef", "IdentityStoreId": "d-1234567890", "OwnerAccountId": "123456789012", "Status": "ACTIVE" } ] }

步驟 2:使用 IAM Identity Center 設定 Lake Formation

  1. 開啟 Lake Formation 主控台。

  2. 設定資料湖管理員。

    重要

    確保您用來執行這些命令的 IAM 角色或使用者已在 Lake Formation 中新增為 Data Lake 管理員。否則,您在建立資料庫或資料表時會收到「Lake Formation (不足) 許可錯誤。若要將您的角色新增為管理員:

    aws lakeformation put-data-lake-settings \ --data-lake-settings '{ "DataLakeAdmins": [ {"DataLakePrincipalIdentifier": "arn:aws:iam::account-id:role/your-role"} ], "CreateDatabaseDefaultPermissions": [], "CreateTableDefaultPermissions": [], "Parameters": {"CROSS_ACCOUNT_VERSION": "3", "SET_CONTEXT": "TRUE"} }'
  3. 使用您在步驟 1 中記下的執行個體 ARN 建立 Lake Formation 的 IAM Identity Center 整合:

    aws lakeformation create-lake-formation-identity-center-configuration \ --catalog-id account-id \ --instance-arn "arn:aws:sso:::instance/ssoins-instance-id"
  4. 為 Lake Formation 位置註冊建立 IAM 角色。請勿將服務連結角色用於信任的身分傳播。如需詳細資訊,請參閱使用 IAM Identity Center 設定 AWS Lake Formation。

    信任政策:

    { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "lakeformation.amazonaws.com" }, "Action": [ "sts:AssumeRole", "sts:SetContext" ] } ] }
    sts:SetContext 為必要項目

    信任政策必須包含 sts:SetContext,這是信任身分傳播的必要項目。預設精靈建立的 IAM 角色是服務連結角色,不包含 sts:SetContext。如果您先前已使用服務連結角色註冊位置,則必須使用包含 的自訂角色重新註冊該位置sts:SetContext。

    許可政策 (授予 Amazon S3 資料位置的存取權):

    { "Version": "2012-10-17", "Statement": [ { "Sid": "LakeFormationDataAccessPermissionsForS3", "Effect": "Allow", "Action": [ "s3:PutObject", "s3:GetObject", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::your-data-bucket/*" ] }, { "Sid": "LakeFormationDataAccessPermissionsForS3ListBucket", "Effect": "Allow", "Action": [ "s3:ListBucket" ], "Resource": [ "arn:aws:s3:::your-data-bucket" ] } ] }

    建立角色並連接政策:

    aws iam create-role \ --role-name LFLocationRegistrationRole \ --assume-role-policy-document file://lf-trust-policy.json \ --description "Lake Formation location registration role with sts:SetContext for TIP" aws iam put-role-policy \ --role-name LFLocationRegistrationRole \ --policy-name LFDataAccessPolicy \ --policy-document file://lf-permissions-policy.json
  5. 使用您建立的角色向 Lake Formation 註冊 Amazon S3 資料位置:

    aws lakeformation register-resource \ --resource-arn "arn:aws:s3:::your-data-bucket" \ --role-arn "arn:aws:iam::account-id:role/LFLocationRegistrationRole"
  6. 在指向您範例資料的 AWS Glue Data Catalog 中建立資料庫和資料表。

  7. 授予資料庫和資料表的 IAM Identity Center 使用者或群組許可。

    重要

    除了資料表上的 SELECT 和 DESCRIBE 之外,您還必須在資料庫上授予 DESCRIBE。如果沒有資料庫層級許可,使用身分型存取時,資料庫將不會出現在 SHOW DATABASES 結果中。

    使用 主控台:

    1. 在 Lake Formation 主控台中,選擇資料湖許可 > 授予。

    2. 在主體下,選擇 IAM Identity Center 使用者和群組,然後選取您的使用者或群組。

    3. 在具名資料目錄資源下,選取您的資料庫。

    4. 在資料庫許可下,選取描述。

    5. 選擇 Grant (授予)。

    6. 重複步驟 1 到 5,這次選取資料表,並在資料表下授予選取和描述許可。

    使用 AWS CLI:

    重要

    Lake Formation 授予中 IAM Identity Center 使用者的主要識別符格式為:arn:aws:identitystore:::user/<UserId>。請注意三元冒號 (:::)。未指定區域或帳戶。從 Identity Store 取得 UserId:

    # List users to get their UserIds aws identitystore list-users --identity-store-id identity-store-id --region your-region # Grant DESCRIBE on the database aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:identitystore:::user/UserId"}' \ --permissions '["DESCRIBE"]' \ --resource '{"Database": {"DatabaseName": "tip_tutorial_db", "CatalogId": "account-id"}}' # Grant SELECT and DESCRIBE on the table aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:identitystore:::user/UserId"}' \ --permissions '["SELECT","DESCRIBE"]' \ --resource '{"Table": {"DatabaseName": "tip_tutorial_db", "Name": "employees", "CatalogId": "account-id"}}'

步驟 3:建立啟用 TIP 的 EMR Serverless 應用程式

確保您的 IAM 角色具有建立應用程式所需的 IAM Identity Center 許可:

"sso:DescribeInstance", "sso:CreateApplication", "sso:DeleteApplication", "sso:PutApplicationAuthenticationMethod", "sso:PutApplicationAssignmentConfiguration", "sso:PutApplicationGrant", "sso:PutApplicationAccessScope"

建立具有信任身分傳播的應用程式、啟用 Apache Livy 端點,以及設定為中繼存放區的 AWS Glue Data Catalog:

aws emr-serverless create-application \ --release-label emr-7.8.0 \ --type "SPARK" \ --identity-center-configuration '{"identityCenterInstanceArn": "arn:aws:sso:::instance/ssoins-instance-id"}' \ --interactive-configuration '{"livyEndpointEnabled":true}' \ --runtime-configuration '[{"classification": "spark-defaults", "properties": {"spark.sql.catalogImplementation": "hive", "spark.hadoop.hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory"}}]'
重要

AWS CLI 版本需求:--identity-center-configuration、 --interactive-configuration和 --runtime-configuration旗標需要 v2 AWS CLI .15+。如果您收到「未知選項」錯誤,請升級您的 AWS CLI 或使用 Python boto3 (v1.35+) 做為替代方案:

import boto3 session = boto3.Session(profile_name='your-profile', region_name='your-region') client = session.client('emr-serverless') response = client.create_application( name='tip-tutorial-app', releaseLabel='emr-7.8.0', type='SPARK', identityCenterConfiguration={ 'identityCenterInstanceArn': 'arn:aws:sso:::instance/ssoins-instance-id' }, interactiveConfiguration={ 'livyEndpointEnabled': True }, runtimeConfiguration=[ { 'classification': 'spark-defaults', 'properties': { 'spark.sql.catalogImplementation': 'hive', 'spark.hadoop.hive.metastore.client.factory.class': 'com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory' } } ] ) print(response['applicationId'])
Glue Data Catalog 中繼存放區組態

spark.sql.catalogImplementation 和 AWS Glue 中繼存放區原廠類別runtimeConfiguration的設定可確保 Spark 工作階段可以存取 Glue Data Catalog 中的所有資料表類型 AWS (CSV、Parquet、ORC、Iceberg)。如果沒有此組態,則只會顯示 Iceberg 資料表。

TIP 需要 Apache Livy 端點

信任的身分傳播需要 --interactive-configuration '{"livyEndpointEnabled":true}'。僅針對 Apache Livy 端點啟用 TIP。

如果您沒有所需的 IAM Identity Center 許可,請在沒有 --identity-center-configuration 參數的情況下建立應用程式,並要求您的 IAM Identity Center 管理員稍後使用 update-application API 啟用 TIP:

aws emr-serverless update-application \ --application-id application-id \ --identity-center-configuration '{"identityCenterInstanceArn": "arn:aws:sso:::instance/ssoins-instance-id"}'

步驟 4:設定任務執行角色

任務執行角色的信任政策必須包含 sts:SetContext,以允許 EMR Serverless 使用使用者的身分增強登入資料:

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "emr-serverless.amazonaws.com" }, "Action": ["sts:AssumeRole", "sts:SetContext"] } ] }

建立角色:

aws iam create-role \ --role-name emr-serverless-tip-job-role \ --assume-role-policy-document file://trust-policy.json \ --description "EMR Serverless job execution role with TIP support"

連接任務將存取之下游服務的許可。此角色需要 Lake Formation、 AWS Glue、Amazon S3 和 Amazon CloudWatch Logs 許可:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "LakeFormationAndGlueAccess", "Effect": "Allow", "Action": [ "lakeformation:GetDataAccess", "glue:GetTable", "glue:GetTables", "glue:GetDatabase", "glue:GetDatabases", "glue:GetPartitions" ], "Resource": "*" }, { "Sid": "S3DataAccess", "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:ListBucket", "s3:GetBucketLocation" ], "Resource": [ "arn:aws:s3:::your-data-bucket", "arn:aws:s3:::your-data-bucket/*" ] }, { "Sid": "CloudWatchLogsAccess", "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents", "logs:DescribeLogGroups", "logs:DescribeLogStreams" ], "Resource": "arn:aws:logs:us-east-1:111122223333:*" } ] }
除了 Lake Formation 許可之外,還需要 S3 許可

除了 Lake Formation 和 Glue 許可之外s3:GetObject,還需要資料儲存貯體上的 Amazon S3 許可 AWS (s3:PutObject、、s3:ListBucket)。Spark 暫存和隨機播放資料需要 s3:PutObject許可。如果沒有 Amazon S3 許可,Spark 任務可能會在讀取基礎資料檔案或寫入臨時資料時失敗,並出現存取遭拒錯誤。

需要 CloudWatch Logs 許可

Spark 驅動程式和執行器日誌交付需要 Amazon CloudWatch Logs 許可。如果沒有這些工作階段,Spark 工作階段可能無法啟動。

將政策連接到角色:

aws iam put-role-policy \ --role-name emr-serverless-tip-job-role \ --policy-name tip-lakeformation-access \ --policy-document file://permissions-policy.json

將 Lake Formation 許可授予任務執行角色:

任務執行角色還需要資料庫和資料表上的 Lake Formation 許可。這是對 IAM Identity Center 使用者授予的授予的補充。

# Grant DESCRIBE on the database to the job role aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:iam::account-id:role/emr-serverless-tip-job-role"}' \ --permissions '["DESCRIBE"]' \ --resource '{"Database": {"DatabaseName": "tip_tutorial_db", "CatalogId": "account-id"}}' # Grant SELECT and DESCRIBE on the table to the job role aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:iam::account-id:role/emr-serverless-tip-job-role"}' \ --permissions '["SELECT","DESCRIBE"]' \ --resource '{"Table": {"DatabaseName": "tip_tutorial_db", "Name": "employees", "CatalogId": "account-id"}}'

如需其他下游服務組態,請參閱使用 Lake Formation 搭配 EMR Serverless 和使用 Amazon S3 Access Grants 搭配 EMR Serverless。

透過 Amazon SageMaker Unified Studio 使用受信任身分傳播查詢資料

完成設定後,您可以使用啟用 TIP 的 EMR Serverless 應用程式做為 Amazon SageMaker Unified Studio 專案中的運算來查詢資料。EMR Serverless 會使用自己的 IAM Identity Center 身分執行每個使用者的查詢,而 Lake Formation 會強制執行每個使用者的許可。

先決條件

  • 啟用 TIP 的 EMR Serverless 應用程式 (已於上述步驟 3 完成)。

  • 使用 IAM Identity Center 身分驗證設定的 Amazon SageMaker Unified Studio 網域。

  • 授予目標資料庫和資料表上 IAM Identity Center 使用者或群組的 Lake Formation 許可 (已於上述步驟 2 完成)。

步驟 5:建立已啟用 TIP 的 Amazon SageMaker Unified Studio 專案

  1. 使用您的 IAM Identity Center 憑證登入 Amazon SageMaker Unified Studio。

  2. 建立新的專案或開啟現有的專案。

  3. 在專案設定中,確保設定檔已在藍圖參數true中enableTrustedIdentityPropagationPermissions設定為 。

步驟 6:將 EMR Serverless 應用程式新增為運算

Amazon SageMaker Unified Studio 專案運算組態目前不支援透過主控台新增現有的 EMR Serverless 應用程式。使用 AWS CLI 建立連線:

aws datazone create-connection \ --domain-identifier domain-id \ --name "my-emr-serverless-connection" \ --environment-identifier environment-id \ --props '{ "sparkEmrProperties": { "computeArn": "arn:aws:emr-serverless:region:account-id:/applications/application-id", "runtimeRole": "arn:aws:iam::account-id:role/emr-serverless-tip-job-role" } }'

取代以下的值:

  • domain-id:您的 Amazon SageMaker Unified Studio 網域識別符。

  • environment-id:專案的環境識別符 (使用 工具環境)。

  • application-id:步驟 3 的 EMR Serverless 應用程式 ID。

  • emr-serverless-tip-job-role:步驟 4 的任務執行角色。

重要

create-connection API 要求發起人成為 DataZone 專案的成員。如果您收到 AccessDeniedException,請在建立連線之前,將您的 IAM 角色新增為專案參與者:

import boto3 session = boto3.Session(profile_name='your-profile', region_name='your-region') client = session.client('datazone') client.create_project_membership( domainIdentifier='domain-id', projectIdentifier='project-id', designation='PROJECT_CONTRIBUTOR', member={'userIdentifier': 'arn:aws:iam::account-id:role/your-role'} )

設定專案的 DataZone 使用者角色以進行 EMR Serverless 存取。

專案環境的 DataZone 使用者角色必須具有與 EMR Serverless 應用程式互動的許可。將下列內嵌政策新增至角色 (角色名稱遵循模式 datazone_usr_role_<project-id>_<environment-id>):

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "emr-serverless:GetApplication", "emr-serverless:StartApplication", "emr-serverless:StopApplication", "emr-serverless:ListApplications", "emr-serverless:AccessLivyEndpoints", "emr-serverless:StartJobRun", "emr-serverless:GetJobRun", "emr-serverless:ListJobRuns", "emr-serverless:CancelJobRun", "emr-serverless:GetDashboardForJobRun" ], "Resource": "*" }, { "Effect": "Allow", "Action": "iam:PassRole", "Resource": "arn:aws:iam::111122223333:role/emr-serverless-tip-job-role", "Condition": { "StringLike": { "iam:PassedToService": "emr-serverless.amazonaws.com" } } } ] }

將 Lake Formation 許可授予 DataZone 使用者角色:

# Grant DESCRIBE on the database aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:iam::account-id:role/datazone_usr_role_project-id_environment-id"}' \ --permissions '["DESCRIBE"]' \ --resource '{"Database": {"DatabaseName": "tip_tutorial_db", "CatalogId": "account-id"}}' # Grant SELECT and DESCRIBE on the table aws lakeformation grant-permissions \ --principal '{"DataLakePrincipalIdentifier": "arn:aws:iam::account-id:role/datazone_usr_role_project-id_environment-id"}' \ --permissions '["SELECT","DESCRIBE"]' \ --resource '{"Table": {"DatabaseName": "tip_tutorial_db", "Name": "employees", "CatalogId": "account-id"}}'
EMR Serverless 連線的主控台限制

Amazon SageMaker Unified Studio 目前支援透過主控台運算組態新增現有的 Amazon EMR on EC2 叢集。對於 EMR Serverless 應用程式,請使用上述 AWS CLI 方法。

步驟 7:查詢範例資料表

  1. 在 Amazon SageMaker Unified Studio 專案中開啟 JupyterLab IDE 空間。

  2. 選取 EMR Serverless 連線做為筆記本執行的運算。

  3. 在新的筆記本儲存格中,執行下列 PySpark 查詢:

    %%pyspark my-emr-serverless-connection spark.sql("SHOW DATABASES").show() %%pyspark my-emr-serverless-connection spark.sql("SELECT * FROM tip_tutorial_db.employees LIMIT 10").show()
  4. 確認查詢傳回資料。這確認您的 IAM Identity Center 身分已傳播到 Lake Formation,並根據您的身分授予存取權。

應用程式冷啟動

建立連線後第一次執行儲存格時,EMR Serverless 會啟動應用程式 (冷啟動)。這可能需要 2 到 3 分鐘。後續儲存格會在工作階段作用中的幾秒鐘內執行。

對存取遭拒錯誤進行故障診斷

如果您收到存取遭拒錯誤,請確認:

  • 您的 IAM Identity Center 使用者或群組已獲得資料庫上的 DESCRIBE 和 Lake Formation 中資料表上的 SELECT/DESCRIBE。

  • 任務執行角色具有 lakeformation:GetDataAccess和 AWS Glue Data Catalog 許可。

  • DataZone 使用者角色具有資料庫和資料表的 Lake Formation 許可。

  • Lake Formation Amazon S3 位置已向包含 sts:SetContext(非服務連結角色) 的自訂角色註冊。

疑難排解

錯誤 原因 修正
AccessDeniedException: emr-serverless:GetApplication DataZone 使用者角色缺少 EMR Serverless 許可。 將 EMR Serverless 內嵌政策新增至 DataZone 使用者角色 (請參閱步驟 6)。
等待應用程式達到 STARTED 狀態逾時 EMR Serverless 應用程式冷啟動超過預設逾時。 重新執行儲存格。應用程式會在背景繼續啟動,並在下次嘗試時準備就緒。
找不到工作階段。由於閒置逾時而清除。 由於任務執行角色的許可不足,Spark 工作階段無法啟動。 確保任務執行角色具有 Amazon S3、Amazon CloudWatch Logs、 AWS Glue 和 Lake Formation 許可 (請參閱步驟 4)。
Lake Formation (Lake Formation) 許可不足:資料庫上的必要描述 您的身分或角色沒有資料庫層級的 Lake Formation 許可。 將資料庫上的 DESCRIBE 授予 IAM Identity Center 使用者、任務執行角色和 DataZone 使用者角色。
TABLE_OR_VIEW_NOT_FOUND Spark 工作階段未設定為使用 AWS Glue Data Catalog。 確保 EMR Serverless 應用程式在其 中包含 spark.sql.catalogImplementation=hive和 AWS Glue 中繼存放區原廠類別 runtimeConfiguration(請參閱步驟 3)。
AccessDeniedException: CreateConnection 發起人不是 DataZone 專案的成員。 在呼叫 之前,將您的角色新增為專案參與者 create-connection(請參閱步驟 6)。

清除資源

為避免持續產生費用,您應刪除在本教學課程中建立的資源:

  1. 刪除 EMR Serverless 應用程式。

  2. 移除 Amazon SageMaker Unified Studio 連線,並刪除 Amazon SageMaker Unified Studio 專案 (和網域,如果您只為此教學課程建立)。

  3. 撤銷您授予的 Lake Formation 許可,並取消註冊 Amazon S3 資料位置。

  4. 刪除您在步驟 2 和步驟 4 中建立的 IAM 角色。

  5. 刪除任何測試 Amazon S3 儲存貯體和範例資料。

後續步驟

  • 若要在 CloudTrail 中驗證身分傳播,請從 EMR Serverless 服務尋找sts:SetContext具有 AssumeRole的事件。

  • 若要限制不同使用者的存取,請建立其他 IAM Identity Center 使用者,並在相同的資料表上授予他們不同的 Lake Formation 許可 (例如資料欄層級或資料列層級篩選)。

  • 對於生產部署,將任務執行角色的 Amazon S3 和 AWS Glue 許可範圍限定為特定資源,而不是使用萬用字元。

  • 如需搭配 Amazon SageMaker Unified Studio 使用 EMR Serverless 的詳細資訊,請參閱 Amazon SageMaker Unified Studio 中的 EMR Serverless。