

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

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

# 使用資料 API 搭配可信身分傳播
<a name="data-api-trusted-identity-propagation"></a>

身為 Amazon Redshift 帳戶管理員，您可以將 Amazon Redshift 叢集或工作群組與 整合 AWS IAM Identity Center，這有助於透過單一登入來管理對 Amazon Redshift 的人力資源存取。如需詳細資訊，請參閱[設定與 AWS Amazon Redshift 的 IAM Identity Center 整合](redshift-iam-access-control-idp-connect-console.md)。Amazon Redshift Data API 支援將 IAM Identity Center 使用者身分傳播至 Amazon Redshift 叢集或工作群組，以及傳播至其他 服務 AWS Lake Formation，例如鏈結下。您可以依照[使用受信任身分傳播以程式設計方式存取 AWS 服務](https://aws.amazon.com/blogs//security/access-aws-services-programmatically-using-trusted-identity-propagation/)中的步驟，使用資料 API 來設定和查詢。

當您使用來自身分增強型 IAM 角色工作階段的 IAM Identity Center 使用者身分呼叫資料 API 時，您只能存取使用相同 IAM Identity Center 使用者產生的陳述式和陳述式結果。例如，下列 AWS CLI 命令會呼叫 `execute-statement`操作，以執行具有受信任身分傳播的 SQL 命令。

```
aws redshift-data execute-statement 
--sql "select current_user;" 
--cluster-id mycluster
--database dev
```

下列 AWS CLI 命令會呼叫 `batch-execute-statement`操作來執行兩個 SQL 命令。

```
aws redshift-data batch-execute-statement 
--sqls  "select current_user;"  "select current_date;"
--cluster-id mycluster
--database dev
```

若要存取由身分增強型 IAM 角色工作階段所提交具有 `cancel-statement`、`describe-statement`、`get-statement-result` 和 `get-statement-result-v2` 的陳述式，IAM Identity Center 使用者和 IAM 角色必須符合用於執行 `execute-statment` 或 `batch-execute-statement` 的憑證。例如，下列 AWS CLI 命令會取得 SQL 陳述式的結果。

```
aws redshift-data get-statement-result 
--id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111
```

若要列出陳述式，則必須提供 `cluster-identifier` 或 `workgroup-name` 參數，以確保 IAM Identity Center 使用者只能存取指派給他們的 Amazon Redshift IAM Identity Center 應用程式。例如，下列 AWS CLI 命令會列出特定叢集的陳述式。

```
aws redshift-data list-statements
--cluster-identifier mycluster
```

您也可以調用資料 API 操作，以使用可信身分傳播來存取叢集或工作群組中的資料庫物件。這包括 `list-databases`、`list-schemas`、`list-tables` 和 `describe-table` 操作。

IAM Identity Center 使用者發出的 API 呼叫可以在 AWS CloudTrail中追蹤。CloudTrail 事件的 `onBehalfOf` 區段會顯示 IAM Identity Center 使用者 ID 和身分存放區 ARN。下列範例示範 CloudTrail 事件的程式碼片段，顯示 `onBehalfOf` 區段中包含 IAM Identity Center 使用者 ID `a1b2c3d4-5678-90ab-cdef-EXAMPLE11111` 和身分存放區 ARN `arn:aws:identitystore::123456789012:identitystore/d-9067bc44d2`。

```
{
            "eventVersion":"1.10",
            "userIdentity":{
            "type":"AssumedRole",
            ...
            },
            "onBehalfOf":{
            "userId":"a1b2c3d4-5678-90ab-cdef-EXAMPLE11111",
            "identityStoreArn":"arn:aws:identitystore::123456789012:identitystore/d-9067bc44d2"
            }
            },
            "eventTime":"2025-01-13T04:46:27Z",
            "eventSource":"redshift-data.amazonaws.com",
            "eventName":"ExecuteStatement",
            "awsRegion":"us-east-1"
            }
```

您可以執行下列 SQL 命令來檢查 IAM Identity Center 使用者提交的查詢。在此範例中，Identity Center 中註冊的電子郵件為 `username@example.com`。

```
SELECT
    h.query_id,
    h.database_name,
    h.status,
    h.query_text,
    u.usename,
    h.start_time,
    h.end_time
FROM
    sys_query_history h
LEFT JOIN
    pg_user u
ON
    h.user_id = u.usesysid
where u.usename='awsidc:username@example.com'    
ORDER BY
    h.start_time DESC;
```