

 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 Spectrum 的 IAM 政策
<a name="c-spectrum-iam-policies"></a>

本主題說明使用 Redshift Spectrum 所需的 IAM 許可。

根據預設，Amazon Redshift Spectrum 在 支援的 AWS Glue Data Catalog AWS 區域中使用 AWS Glue。在其他 AWS 區域中，Redshift Spectrum 會使用 Athena Data Catalog。您的叢集需要授權才能存取 AWS Glue 或 Athena 中的外部資料目錄，以及 Amazon S3 中的資料檔案。您可以透過參考連接到叢集的 AWS Identity and Access Management (IAM) 角色來提供該授權。如果您使用 Apache Hive 中繼存放區來管理資料目錄，則無需提供對 Athena 的存取權限。

您可以鏈結角色，以便您的叢集可以擔任未連接到叢集的其他角色。如需詳細資訊，請參閱[在 Amazon Redshift Spectrum 中鏈結 IAM 角色](#c-spectrum-chaining-roles)。

您存取的 AWS Glue 目錄可能會加密，以提高安全性。如果 AWS Glue 目錄已加密，您需要 AWS KMS 金鑰 AWS Glue 才能存取 AWS Glue Data Catalog。如需詳細資訊，請參閱《 開發人員指南》中的[加密 AWS Glue 您的資料目錄](https://docs.aws.amazon.com/glue/latest/dg/encrypt-glue-data-catalog.html)。 *[AWS Glue](https://docs.aws.amazon.com/glue/latest/dg/)*

**Topics**
+ [Amazon S3 許可](#spectrum-iam-policies-s3)
+ [跨帳戶 Amazon S3 許可](#spectrum-iam-policies-cross-account)
+ [授予或限制使用 Redshift Spectrum 存取的政策](#spectrum-iam-policies-spectrum-only)
+ [授予最低許可的政策](#spectrum-iam-policies-minimum-permissions)
+ [在 Amazon Redshift Spectrum 中鏈結 IAM 角色](#c-spectrum-chaining-roles)
+ [控制對 AWS Glue Data Catalog 的存取](#c-spectrum-glue-acess)

## Amazon S3 許可
<a name="spectrum-iam-policies-s3"></a>

您的叢集至少需要對您 Amazon S3 儲存貯體的 GET 和 LIST 存取權限。如果您的儲存貯體與叢集不在相同的 AWS 帳戶中，則您的儲存貯體也必須授權您的叢集存取資料。如需詳細資訊，請參閱[授權 Amazon Redshift 代表您存取 AWS 其他服務](https://docs.aws.amazon.com/redshift/latest/mgmt/authorizing-redshift-service.html)。

**注意**  
Amazon S3 儲存貯體不能使用僅從特定 VPC 端點限制存取的儲存貯體政策。

下列政策會授予對任何 Amazon S3 儲存貯體的 GET 和 LIST 存取權限。該政策允許 Redshift Spectrum 對 Amazon S3 儲存貯體的存取權以及 COPY 操作。

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

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [{
		"Effect": "Allow",
		"Action": ["s3:Get*", "s3:List*"],
		"Resource": "*"
	}]
}
```

------

下列政策會授予對您名為 `amzn-s3-demo-bucket` 之 Amazon S3 儲存貯體的 GET 和 LIST 存取權限。

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

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [{
		"Effect": "Allow",
		"Action": ["s3:Get*", "s3:List*"],
		"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
	}]
}
```

------

## 跨帳戶 Amazon S3 許可
<a name="spectrum-iam-policies-cross-account"></a>

若要授予 Redshift Spectrum 存取屬於另一個 AWS 帳戶的 Amazon S3 儲存貯體中資料的許可，請將下列政策新增至 Amazon S3 儲存貯體。如需詳細資訊，請參閱[授予跨帳戶儲存貯體許可](https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-walkthroughs-managing-access-example2.html)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "Example permissions",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::{{111122223333}}:role/{{spectrumrole}}"
            },
            "Action": [
                "s3:GetBucketLocation",
                "s3:GetObject",
                "s3:ListMultipartUploadParts",
                "s3:ListBucket",
                "s3:ListBucketMultipartUploads"
            ],
            "Resource": [
                "arn:aws:s3:::{{bucketname}}",
                "arn:aws:s3:::{{bucketname}}/*"
            ]
        }
    ]
}
```

------

## 授予或限制使用 Redshift Spectrum 存取的政策
<a name="spectrum-iam-policies-spectrum-only"></a>

以下範例政策允許 Amazon Redshift 存取 Amazon S3 儲存貯體。將此政策新增至 IAM 角色時，建議您使用僅用於 Amazon Redshift 的 IAM 角色。另外建議您，不要重複使用現有角色，或為此 IAM 角色新增額外的許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:ListBucketVersions",
                "s3:ListBucket"
            ],
            "Resource": [ 
                "arn:aws:s3:::amzn-s3-demo-bucket", 
                "arn:aws:s3:::amzn-s3-demo-bucket/*" 
            ]
        }
    ]
}
```

------

## 授予最低許可的政策
<a name="spectrum-iam-policies-minimum-permissions"></a>

下列政策會授予將 Redshift Spectrum 與 Amazon S3 AWS Glue和 Athena 搭配使用所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucketVersions",
                "s3:ListBucket",
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::bucketname", 
                "arn:aws:s3:::bucketname/folder1/folder2/*" 
             ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "glue:CreateDatabase",
                "glue:DeleteDatabase",
                "glue:GetDatabase",
                "glue:GetDatabases",
                "glue:UpdateDatabase",
                "glue:CreateTable",
                "glue:DeleteTable",
                "glue:BatchDeleteTable",
                "glue:UpdateTable",
                "glue:GetTable",
                "glue:GetTables",
                "glue:BatchCreatePartition",
                "glue:CreatePartition",
                "glue:DeletePartition",
                "glue:BatchDeletePartition",
                "glue:UpdatePartition",
                "glue:GetPartition",
                "glue:GetPartitions",
                "glue:BatchGetPartition"
            ],
            "Resource": "*"
        }
    ]
}
```

------

如果您將 Athena 用於資料目錄，而不是 AWS Glue，則政策需要完整的 Athena 存取。下列政策會授予對 Athena 資源的存取許可。如果外部資料庫位於 Hive 中繼存放區中，則您不需要 Athena 存取權限。

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

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [{
		"Effect": "Allow",
		"Action": ["athena:*"],
		"Resource": ["*"]
	}]
}
```

------

## 在 Amazon Redshift Spectrum 中鏈結 IAM 角色
<a name="c-spectrum-chaining-roles"></a>

當您將角色連接到叢集時，您的叢集可以擔任該角色來 AWS Glue 代表您存取 Amazon S3、Athena 和 。如果連接至叢集的角色無法存取必要資源，您可鏈結其他角色，該角色甚至可以來自其他帳戶。您的叢集接著會暫時擔任鏈結的角色，以存取資料。您也可以用鏈結角色的方式來授予跨帳戶存取。您最多可以鏈結 10 個角色。鏈結中的每個角色都會擔任鏈結中的下一個角色，直到叢集擔任鏈結尾端的角色為止。

若要鏈結角色，您應建立角色間的信任關係。擔任其他角色的角色，必須擁有允許其擔任指定角色的許可政策。另一方面，要傳送許可的角色必須擁有信任政策，允許其將許可傳送給其他鏈結的角色。如需詳細資訊，請參閱[在 Amazon Redshift 中鏈結 IAM 角色](https://docs.aws.amazon.com/redshift/latest/mgmt/authorizing-redshift-service.html#authorizing-redshift-service-chaining-roles)。

當您執行 CREATE EXTERNAL SCHEMA 命令時，您可包括逗號分隔的角色 ARN 清單來鏈結角色。

**注意**  
鏈結的角色清單不得包含空格。

在以下範例中，`MyRedshiftRole` 連接到叢集。`MyRedshiftRole` 會擔任角色 `AcmeData`，它屬於帳戶 `111122223333`。

```
create external schema acme from data catalog 
database 'acmedb' region 'us-west-2' 
iam_role 'arn:aws:iam::123456789012:role/MyRedshiftRole,arn:aws:iam::111122223333:role/AcmeData';
```

## 控制對 AWS Glue Data Catalog 的存取
<a name="c-spectrum-glue-acess"></a>

如果您將 AWS Glue 用於資料目錄，則可以使用 IAM 政策將精細存取控制套用至 AWS Glue 資料目錄。例如，您可能只想要對特定 IAM 角色公開一些資料庫和資料表。

下列各節說明 IAM 政策，用於各種層級的資料存取，儲存在 AWS Glue Data Catalog 中。

**Topics**
+ [資料庫操作的政策](#c-spectrum-glue-acess-database)
+ [資料表操作的政策](#c-spectrum-glue-acess-tables)
+ [分割區操作的政策](#c-spectrum-glue-acess-partitions)

### 資料庫操作的政策
<a name="c-spectrum-glue-acess-database"></a>

如果您想要授予使用者檢視和建立資料庫的許可，他們需要資料庫和 AWS Glue 資料目錄的存取權限。

下列範例查詢會建立資料庫。

```
CREATE EXTERNAL SCHEMA example_db
FROM DATA CATALOG DATABASE 'example_db' region 'us-west-2' 
IAM_ROLE 'arn:aws:iam::redshift-account:role/spectrumrole'
CREATE EXTERNAL DATABASE IF NOT EXISTS
```

以下 IAM 政策會提供用於建立資料庫所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetDatabase",
                "glue:CreateDatabase"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog"
            ]
        }
    ]
}
```

------

下列範例查詢會列出目前的資料庫。

```
SELECT * FROM SVV_EXTERNAL_DATABASES WHERE
databasename = 'example_db1' or databasename = 'example_db2';
```

以下 IAM 政策會提供用於列出目前的資料庫所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetDatabases"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db1",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db2",
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog"
            ]
        }
    ]
}
```

------

### 資料表操作的政策
<a name="c-spectrum-glue-acess-tables"></a>

如果要提供使用者檢視、建立、卸除、變更或對資料表採取其他動作的許可，使用者便需要數種類型的存取。使用者需要自行存取資料表、其所屬的資料庫，以及目錄。

以下範例查詢會建立外部資料表。

```
CREATE EXTERNAL TABLE example_db.example_tbl0(
    col0 INT,
    col1 VARCHAR(255)
) PARTITIONED BY (part INT) STORED AS TEXTFILE
LOCATION 's3://test/s3/location/';
```

以下 IAM 政策會提供用於建立外部資料表所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:CreateTable"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

以下每個範例查詢會列出目前的外部資料表。

```
SELECT * FROM svv_external_tables
WHERE tablename = 'example_tbl0' OR
tablename = 'example_tbl1';
```

```
SELECT * FROM svv_external_columns
WHERE tablename = 'example_tbl0' OR
tablename = 'example_tbl1';
```

```
SELECT parameters FROM svv_external_tables
WHERE tablename = 'example_tbl0' OR
tablename = 'example_tbl1';
```

以下 IAM 政策會提供用於列出目前的外部資料表所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetTables"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl1"
            ]
        }
    ]
}
```

------

以下範例查詢會修改現有資料表。

```
ALTER TABLE example_db.example_tbl0
SET TABLE PROPERTIES ('numRows' = '100');
```

以下 IAM 政策會提供用於修改現有資料表所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetTable",
                "glue:UpdateTable"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

以下範例查詢會捨棄現有資料表。

```
DROP TABLE example_db.example_tbl0;                       
```

以下 IAM 政策會提供用於捨棄現有資料表所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:DeleteTable"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

### 分割區操作的政策
<a name="c-spectrum-glue-acess-partitions"></a>

如果要提供使用者執行分割區層級操作 (檢視、建立、捨棄、變更等等) 的許可，使用者需要分割區所屬資料表的許可。他們也需要相關資料庫和 AWS Glue 資料目錄的許可。

下列範例查詢會建立分割區。

```
ALTER TABLE example_db.example_tbl0
ADD PARTITION (part=0) LOCATION 's3://test/s3/location/part=0/';
ALTER TABLE example_db.example_t
ADD PARTITION (part=1) LOCATION 's3://test/s3/location/part=1/';
```

以下 IAM 政策會提供用於建立分割區所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetTable",
                "glue:BatchCreatePartition"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

下列範例查詢會列出目前的分割區。

```
SELECT * FROM svv_external_partitions
WHERE schemname = 'example_db' AND
tablename = 'example_tbl0'
```

以下 IAM 政策會提供用於列出目前的分割區所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetPartitions",
                "glue:GetTables",
                "glue:GetTable"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

以下範例查詢會修改現有分割區。

```
ALTER TABLE example_db.example_tbl0 PARTITION(part='0')
SET LOCATION 's3://test/s3/new/location/part=0/';
```

以下 IAM 政策會提供用於修改現有分割區所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:GetPartition",
                "glue:UpdatePartition"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------

以下範例查詢會捨棄現有分割區。

```
ALTER TABLE example_db.example_tbl0 DROP PARTITION(part='0');               
```

以下 IAM 政策會提供刪除現有分割區所需的最低許可。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "glue:DeletePartition"
            ],
            "Resource": [
                "arn:aws:glue:us-west-2:{{111122223333}}:catalog",
                "arn:aws:glue:us-west-2:{{111122223333}}:database/example_db",
                "arn:aws:glue:us-west-2:{{111122223333}}:table/example_db/example_tbl0"
            ]
        }
    ]
}
```

------