

 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/)。

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

# 授權 Amazon Redshift 資料 API 的存取
<a name="data-api-access"></a>

若要存取資料 API，使用者必須獲得授權。您可以透過將受管政策 (即預先定義的 AWS Identity and Access Management (IAM) 政策) 新增至該使用者，來授權其存取資料 API。我們建議的最佳實務是，將許可政策附加到 IAM 角色，然後根據需要將其指派給使用者和群組。如需詳細資訊，請參閱 [Amazon Redshift 中的身分和存取管理](https://docs.aws.amazon.com/redshift/latest/mgmt/redshift-iam-authentication-access-control.html)。若要查看受管政策允許和拒絕的許可，請參閱 IAM 主控台 ([https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/))。

# 設定 IAM 許可權限
<a name="data-api-iam"></a>

Amazon Redshift 會提供 `AmazonRedshiftDataFullAccess` 受管政策。此政策可提供 Amazon Redshift 資料 API 操作的完整存取權。此政策也允許對特定 Amazon Redshift 的存取範圍 AWS Secrets Manager，以及驗證和存取 Amazon Redshift 叢集或 Redshift Serverless 工作群組所需的 IAM API 操作。

您也可以建立自己的 IAM 政策，以允許存取特定資源。若要建立您的政策，請使用 `AmazonRedshiftDataFullAccess` 政策作為起始範本。建立政策後，將它新增給每一位需要資料 API 存取的使用者。

請考量與使用者相關聯之 IAM 政策的下列要求：
+ 如果您使用 AWS Secrets Manager 進行身分驗證，請確認政策允許使用 `secretsmanager:GetSecretValue`動作來擷取以金鑰 標記的秘密`RedshiftDataFullAccess`。
+ 如果您使用暫時憑證對叢集進行身分驗證，請確認政策允許對叢集中任何資料庫的資料庫使用者名稱 `redshift_data_api_user` 使用 `redshift:GetClusterCredentials` 動作。此使用者名稱必須已建立在資料庫中。
+ 如果您使用暫時憑證向無伺服器工作群組進行身分驗證，請確認政策允許使用 `redshift-serverless:GetCredentials` 動作來擷取以索引鍵 `RedshiftDataFullAccess` 標記的工作群組。資料庫使用者會以 1：1 映射至 source AWS Identity and Access Management (IAM) 身分。例如，使用者 sample\$1user 會映射至資料庫使用者 `IAM:sample_user`，而 IAM 角色 sample\$1role 會映射至 `IAMR:sample_role`。如需 IAM 身分的相關資訊，請參閱《IAM 使用者指南》中的 [IAM 身分 (使用者、使用者群組和角色)](https://docs.aws.amazon.com/IAM/latest/UserGuide/id.html)。
+ IAM 動作 `redshift-data:GetStatementResult` 允許存取 `GetStatementResult` 和 `GetStatementResultV2` API 操作。

下列連結提供 *IAM 使用者指南* AWS Identity and Access Management 中有關 的詳細資訊。
+ 如需有關建立 IAM 角色的資訊，請參閱[建立 IAM 角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html)。
+ 如需有關建立 IAM 政策的資訊，請參閱[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。
+ 如需有關將 IAM 政策新增給使用者的資訊，請參閱[新增和移除 IAM 身分許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html)。

## 在另一個帳戶擁有的叢集上執行查詢
<a name="data-api-run-query-on-others-cluster"></a>

若要在另一個帳戶擁有的叢集上執行查詢，擁有端帳戶必須提供資料 API 可在呼叫端帳戶中擔任的 IAM 角色。例如，假設帳戶 B 擁有帳戶 A 需要存取的叢集。帳戶 B 可將 AWS 受管政策連接至`AmazonRedshiftDataFullAccess`帳戶 B 的 IAM 角色。然後，帳戶 B 會使用信任政策來信任帳戶 A，如下所示：``

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:role/someRoleA"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}
```

------

最後，帳戶 A 的 IAM 角色必須能夠擔任帳戶 B 的 IAM 角色。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": {
        "Effect": "Allow",
        "Action": "sts:AssumeRole",
        "Resource": "arn:aws:iam::111122223333:role/someRoleB"
    }
}
```

------

## 指定 IAM 角色，將資源限制在 中的 Redshift Serverless 工作群組和 Amazon Redshift 叢集 AWS 帳戶
<a name="data-api-restrict-to-account"></a>

您可以在身分型政策中指定資源 ARN，以控制對 AWS 帳戶中 Redshift Serverless 工作群組和 Amazon Redshift 叢集的存取。此範例示範如何建立政策，以便僅允許所指定 AWS 帳戶中的工作群組和叢集存取資料 API。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "redshift-data:CancelStatement",
                "redshift-data:DescribeStatement",
                "redshift-data:GetStatementResult",
                "redshift-data:ListStatements"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": "redshift-data:*",
            "Resource": [
                "arn:aws:redshift:us-east-1:111122223333:workgroup/*",
                "arn:aws:redshift:us-east-1:111122223333:cluster:*"
            ]
        }
    ]
}
```

------

## 設定 IAM 政策，以限制只有陳述式擁有者能夠存取 SQL 陳述式資訊
<a name="data-api-restrict-to-statement-owner"></a>

根據預設，Amazon Redshift 資料 API 會將呼叫 `ExecuteStatement` 和 `BatchExecuteStatement` 時使用的 IAM 角色視為 SQL 陳述式的擁有者。允許擔任該角色的任何人都可以存取 SQL 陳述式的相關資訊，包括其結果。若要限制只有特定擁有者的 IAM 角色工作階段才能存取 SQL 陳述式資訊，請新增條件 `redshift-data:statement-owner-iam-userid: "${aws:userid}"`。下列 IAM 政策會限制存取權。

------
#### [ JSON ]

****  

```
{
"Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "redshift-data:CancelStatement",
                "redshift-data:DescribeStatement",
                "redshift-data:GetStatementResult",
                "redshift-data:ListStatements"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "redshift-data:statement-owner-iam-userid": "${aws:userid}"
                }
            }
        }
    ]
}
```

------

您可以使用條件 `statement-owner-iam-userid` 搭配 `CancelStatement`、`DescribeStatement`、`GetStatementResult` 和 `ListStatements`。如需詳細資訊，請參閱 [Amazon Redshift 資料 API 定義的動作](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonredshiftdataapi.html#amazonredshiftdataapi-redshift-data_statement-owner-iam-userid)。

## 設定 IAM 政策以限制只有工作階段擁有者才能存取 SQL 結果
<a name="data-api-restrict-session-owner"></a>

根據預設，Amazon Redshift 資料 API 會將呼叫 `ExecuteStatement` 和 `BatchExecuteStatement` 時使用的 IAM 角色，視為執行 SQL 陳述式的資料庫工作階段的擁有者。允許擔任該角色的任何人都可以將查詢提交至資料庫工作階段。若要限制只有特定擁有者的 IAM 角色工作階段才能存取，請新增條件 ` redshift-data:session-owner-iam-userid: "${aws:userid}"`。下列 IAM 政策會限制存取權。

下列 IAM 政策僅允許工作階段擁有者取得陳述式結果。條件 `session-owner-iam-userid` 可用來限制只有指定的 `userid` 可存取資源。

------
#### [ JSON ]

****  

```
{
"Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [ 
                "redshift-data:ExecuteStatement",
                "redshift-data:BatchExecuteStatement"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "redshift-data:session-owner-iam-userid": "${aws:userid}"
                }
            }
        }
    ]
}
```

------

您可以使用條件 `session-owner-iam-userid` 搭配 `ExecuteStatement` 和 `BatchExecuteStatement`。如需詳細資訊，請參閱 [Amazon Redshift 資料 API 定義的動作](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonredshiftdataapi.html#amazonredshiftdataapi-redshift-data_statement-owner-iam-userid)。

# 在 中存放資料庫登入資料 AWS Secrets Manager
<a name="data-api-secrets"></a>

當您呼叫資料 API 時，您可以透過在 AWS Secrets Manager中使用機密來傳遞叢集或無伺服器工作群組的憑證。若要這樣傳遞登入資料，請指定秘密的名稱或秘密的 Amazon Resource Name (ARN)。

若要使用 Secrets Manager 來儲存憑證，您需要 `SecretManagerReadWrite` 受管政策許可。如需最低許可的詳細資訊，請參閱*AWS Secrets Manager 《 使用者指南*》中的[使用 Secrets Manager 建立和管理 AWS 秘密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/managing-secrets.html)。

**將憑證儲存在 Amazon Redshift 叢集的機密中**

1. 使用 AWS Secrets Manager 主控台建立包含叢集登入資料的秘密：
   + 當您選擇**儲存新的機密**時，請選擇 **Redshift 叢集的憑證**。
   + 將**使用者名稱** (資料庫使用者)、**密碼**和**資料庫叢集** (叢集識別碼) 的值儲存在機密中。
   + 使用索引鍵 `RedshiftDataFullAccess` 標記機密。 AWS 受管政策`AmazonRedshiftDataFullAccess`僅允許`secretsmanager:GetSecretValue`以金鑰 標記的秘密執行 動作`RedshiftDataFullAccess`。

   如需說明，請參閱《AWS Secrets Manager 使用者指南》**中的[建立基本秘密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/manage_create-basic-secret.html)。

1. 使用 AWS Secrets Manager 主控台來檢視您建立之秘密的詳細資訊，或執行 `aws secretsmanager describe-secret` AWS CLI 命令。

   記下秘密的名稱和 ARN。您可以在呼叫資料 API 時使用這些資料。

**將憑證儲存在無伺服器工作群組的機密中**

1. 使用 AWS Secrets Manager AWS CLI 命令來存放包含無伺服器工作群組登入資料的秘密：
   + 在檔案中建立您的秘密，例如名為 `mycreds.json` 的 JSON 檔案。在檔案中提供**使用者名稱** (資料庫使用者) 和**密碼**的值。

     ```
     {
           "username": "myusername",
           "password": "mypassword"
     }
     ```
   + 將值儲存在機密中，並使用索引鍵 `RedshiftDataFullAccess` 標記機密。

     ```
     aws secretsmanager create-secret --name MyRedshiftSecret  --tags Key="RedshiftDataFullAccess",Value="serverless" --secret-string file://mycreds.json
     ```

     以下將顯示輸出。

     ```
     {
         "ARN": "arn:aws:secretsmanager:region:accountId:secret:MyRedshiftSecret-mvLHxf",
         "Name": "MyRedshiftSecret",
         "VersionId": "a1603925-e8ea-4739-9ae9-e509eEXAMPLE"
     }
     ```

   如需詳細資訊，請參閱《AWS Secrets Manager 使用者指南》**中的[使用 AWS CLI建立基本機密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/manage_create-basic-secret.html#proc-create-api)。

1. 使用 AWS Secrets Manager 主控台來檢視您建立之秘密的詳細資訊，或執行 `aws secretsmanager describe-secret` AWS CLI 命令。

   記下秘密的名稱和 ARN。您可以在呼叫資料 API 時使用這些資料。

# 建立資料 API 的 Amazon VPC 端點 (AWS PrivateLink)
<a name="data-api-vpc-endpoint"></a>

Amazon Virtual Private Cloud (Amazon VPC) 可讓您在虛擬私有雲端 (VPC) 中安全地啟動 AWS 資源，例如 AWS Amazon Redshift 叢集和應用程式。 在虛擬私有雲端 (VPCs) 和服務之間 AWS PrivateLink 提供私有連線。您可以使用 AWS PrivateLink建立 VPC 端點，以根據 Amazon VPC 連接到不同帳戶和 VPCs 的服務。如需詳細資訊 AWS PrivateLink，請參閱《*Amazon Virtual Private Cloud 使用者指南*》中的 [VPC 端點服務 (AWS PrivateLink)](https://docs.aws.amazon.com/vpc/latest/userguide/endpoint-service.html)。

您可以使用 Amazon VPC 端點呼叫資料 API。使用 Amazon VPC 端點可使 Amazon VPC 中的應用程式與資料 API 中的流量保持在 AWS 網路，而不使用公有 IP 地址。Amazon VPC 端點能協助您符合與限制公有網際網路連線相關的合規和法律需求。舉例來說，若使用 Amazon VPC 端點，就能讓在 Amazon EC2 執行個體上執行之應用程式和資料 API 的流量，只在包含兩者的 VPC 中傳送。

建立 Amazon VPC 端點之後就能開始使用，而不需要在應用程式中進行任何程式碼或組態變更。

**建立資料 API 的 Amazon VPC 端點**

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/)：// 開啟 Amazon VPC 主控台。

1. 選擇 **Endpoints (端點)**，然後選擇 **Create Endpoint (建立端點)**。

1. 在 **Create Endpoint** (建立端點) 頁面上，針對 **Service category** (服務類別) 選擇 **AWS services** ( 服務)。針對**服務名稱**，選擇 **redshift-data** (`com.amazonaws.region.redshift-data`)。

1. 針對 **VPC**，選擇要在其中建立端點的 VPC。

   選擇包含進行資料 API 呼叫之應用程式的 VPC。

1. 針對**子網路**，選擇執行您應用程式之 AWS 服務所使用的每個可用區域 (AZ) 的子網路。

   若要建立 Amazon VPC 端點，請指定可存取端點的私有 IP 地址範圍。若要執行此作業，請選擇每個可用區域的子網路。這麼做會將 VPC 端點限制為每個可用區域專屬的私有 IP 地址範圍，並且也會在每個可用區域中建立 Amazon VPC 端點。

1. 針對 **Enable DNS name (啟用 DNS 名稱)**，選取 **Enable for this endpoint (為此端點啟用)**。

   私有 DNS 會將標準資料 API DNS 主機名稱 (`https://redshift-data.region.amazonaws.com`) 解析為與您 Amazon VPC 端點專用 DNS 主機名稱相關的私有 IP 地址。因此，您可以使用 AWS CLI AWS SDKs存取資料 API VPC 端點，而不需要進行任何程式碼或組態變更來更新資料 API 端點 URL。

1. 針對 **Security group (安全群組)**，選擇要與 Amazon VPC 端點建立關聯的安全群組。

   選擇允許存取執行您應用程式之 AWS 服務的安全群組。舉例來說，若有 Amazon EC2 執行個體在執行您的應用程式，請選擇要允許存取 Amazon EC2 執行個體的安全群組。安全群組能讓您控制 VPC 中，資源流向 Amazon VPC 端點的流量。

1. 選擇**建立端點**。

建立端點之後，請選擇 中的連結 AWS 管理主控台 以檢視端點詳細資訊。

端點 **Details (詳細資訊)** 標籤會顯示建立 Amazon VPC 端點時產生的 DNS 主機名稱。

您可以使用標準端點 (`redshift-data.region.amazonaws.com`) 或其中一個 VPC 專用端點，來在 Amazon VPC 中呼叫資料 API。標準資料 API 端點會自動路由至 Amazon VPC 端點。因為私有 DNS 主機名稱在 Amazon VPC 端點建立時已啟用，所以會發生此路由。

當您在資料 API 呼叫中使用 Amazon VPC 端點時，應用程式與資料 API 間的所有流量都會維持在包含兩者的 Amazon VPC 中。您可以使用 Amazon VPC 端點進行任何類型的資料 API 呼叫。如需呼叫資料 API 的詳細資訊，請參閱[呼叫 Amazon Redshift 資料 API 時的考量](data-api.md#data-api-calling-considerations)。

# 連線到叢集時加入資料庫群組
<a name="data-api-dbgroups"></a>

資料庫群組是資料庫使用者的集合。您可以向群組授予資料庫權限。管理員可以設定 IAM 角色，以便在使用資料 API 執行 SQL 時將這些資料庫群組納入考量。如需資料庫群組的相關資訊，請參閱《Amazon Redshift 資料庫開發人員指南》**中的[群組](https://docs.aws.amazon.com/redshift/latest/dg/r_Groups.html)。

您可以設定資料 API 呼叫者的 IAM 角色，以便在資料 API 連線到叢集時，呼叫中指定的資料庫使用者會加入資料庫群組。只有在連線至已佈建的叢集時才支援此功能。連線至 Redshift Serverless 工作群組時則不支援。資料 API 呼叫者的 IAM 角色也必須允許 `redshift:JoinGroup` 動作。

請透過向 IAM 角色新增標籤來進行此設定。呼叫者 IAM 角色的管理員會新增標籤，標籤中包含資料庫群組清單的索引鍵 `RedshiftDbGroups` 和索引鍵值。此值是以冒號 (:) 分隔的資料庫群組名稱清單，總長度最多可有 256 個字元。您必須之前就已在連線的資料庫中定義好資料庫群組。如果在資料庫中找不到任何指定的群組，則會忽略該群組。例如，對於資料庫群組 `accounting` 和 `retail`，索引鍵-值為 `accounting:retail`。資料 API 會使用標籤索引鍵-值組 `{"Key":"RedshiftDbGroups","Value":"accounting:retail"}` 來判斷在呼叫資料 API 時，哪些資料庫群組會與所提供的資料庫使用者相關聯。

**聯結資料庫群組**

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 的 IAM 主控台。

1. 在主控台導覽窗格中，選擇 **Roles** (角色)，然後選擇您要編輯的角色名稱。

1. 選擇**標籤**索引標籤，然後選擇**管理標籤**。

1. 選擇**新增標籤**，然後新增 **RedshiftDbGroups** 索引鍵和值 (此為 *database-groups-colon-separated* 清單)。

1. 選擇**儲存變更**。

   現在，當 IAM 主體 (附加了此 IAM 角色) 呼叫資料 API 時，指定的資料庫使用者便會加入 IAM 角色中指定的資料庫群組。

如需如何將標籤附加到主體 (包括 IAM 角色和 IAM 使用者) 的相關資訊，請參閱《IAM 使用者指南》**中的[標記 IAM 資源](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_tags.html)。