

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/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 在支持 Amazon Glue 的 Amazon 区域中使用 Amazon Glue Data Catalog。在其他 Amazon 区域中，Redshift Spectrum 使用 Athena Data Catalog。您的集群需要授权才能访问您在 Amazon Glue 或 Athena 中的外部数据目录以及您在 Amazon S3 中的数据文件。您通过引用附加到集群的 Amazon Identity and Access Management (IAM) 角色来提供授权。如果您使用 Apache Hive 元存储管理您的数据目录，则无需提供对 Athena 的访问权限。

您可以串联角色，以便集群可以承担其他未附加到集群的角色。有关更多信息，请参阅 [在 Amazon Redshift Spectrum 中链接 IAM 角色](#c-spectrum-chaining-roles)。

您访问的 Amazon Glue 目录可能已加密以提高安全性。如果 Amazon Glue 目录已加密，则您需要使用 Amazon KMS 的 Amazon Glue 密钥来访问 Amazon Glue Data Catalog。有关更多信息，请参阅 *[Amazon Glue 开发人员指南](https://docs.amazonaws.cn/glue/latest/dg/)中的[加密您的 Amazon Glue Data Catalog](https://docs.amazonaws.cn/glue/latest/dg/encrypt-glue-data-catalog.html)。*

**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)
+ [控制对 Amazon Glue 数据目录的访问](#c-spectrum-glue-acess)

## Amazon S3 权限
<a name="spectrum-iam-policies-s3"></a>

您的集群至少需要对 Amazon S3 桶的 GET 和 LIST 访问权限。如果您的桶与您的集群没有位于同一个Amazon账户中，则您的桶还必须授权您的集群访问数据。有关更多信息，请参阅[代表您授权 Amazon Redshift 访问其他 Amazon 服务](https://docs.amazonaws.cn/redshift/latest/mgmt/authorizing-redshift-service.html)。

**注意**  
Amazon S3 桶不能使用将访问限制为仅通过特定 VPC 端点的桶策略。

以下策略将授予对任何 Amazon S3 桶的 GET 和 LIST 访问权限。该策略允许访问 Amazon S3 桶以进行 Redshift Spectrum 和 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 授权访问属于其他 Amazon 账户的 Amazon S3 桶中的数据，请将以下策略添加到 Amazon S3 桶中。有关更多信息，请参阅[授予跨账户桶权限](https://docs.amazonaws.cn/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 、Amazon 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 用于数据目录，而不是 Amazon 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>

当您将角色附加到集群时，集群可以代入该角色以您的名义访问 Amazon S3、Athena 和 Amazon Glue。如果附加到集群的角色无法访问必要的资源，则可以串联到另一个角色 (可能属于其他账户)。然后，您的集群临时代入串联的角色来访问数据。您还可以通过串联角色来授予跨账户访问权限。您可以串联最多 10 个角色。链中的每个角色都会代入链中的下一个角色，直到集群承担位于链尾的角色。

要串联角色，您可以在角色之间建立信任关系。代入另一个角色的角色必须具有允许其代入指定角色的权限策略。反过来，传递权限的角色必须具有允许其将权限传递给另一个角色的信任策略。有关更多信息，请参阅[在 Amazon Redshift 中链接 IAM 角色](https://docs.amazonaws.cn/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';
```

## 控制对 Amazon Glue 数据目录的访问
<a name="c-spectrum-glue-acess"></a>

如果将 Amazon Glue 用于数据目录，则可以使用 IAM 策略将细粒度访问控制应用于 Amazon Glue Data Catalog。例如，您可能希望仅将少数数据库和表公开给特定的 IAM 角色。

以下各部分描述了一些 IAM 策略，它们针对存储在 Amazon Glue 数据目录中的数据进行各种级别的访问。

**Topics**
+ [数据库操作的策略](#c-spectrum-glue-acess-database)
+ [表操作的策略](#c-spectrum-glue-acess-tables)
+ [分区操作的策略](#c-spectrum-glue-acess-partitions)

### 数据库操作的策略
<a name="c-spectrum-glue-acess-database"></a>

如果要授予用户查看和创建数据库的权限，则同时需要数据库和 Amazon 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>

如果要授予用户执行分区级操作（查看、创建、删除、更改等）的权限，则他们需要对分区所属的表具有权限。他们还需要相关数据库和 Amazon Glue Data Catalog 的权限。

以下示例查询创建一个分区。

```
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"
            ]
        }
    ]
}
```

------