Amazon Redshift Spectrum 的 IAM 政策 - Amazon Redshift

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

Amazon Redshift Spectrum 的 IAM 政策

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

您可以鏈結角色,以便您的叢集可以擔任未連接到叢集的其他角色。如需詳細資訊,請參閱 在 Amazon Redshift Spectrum 中鏈結 IAM 角色

您存取的 AWS Glue 目錄可能會加密以提高安全性。如果目 AWS Glue 錄已加密,則需要用於存 AWS Glue 取 AWS Glue 資料目錄的 AWS KMS 金鑰。如需詳細資訊,請參閱AWS Glue 開發人員指南的加密資 AWS Glue 料目錄

注意

如果您目前在 Athena 資料目錄中有 Redshift 頻譜外部表格,則可以將您的 Athena 資料目錄移轉至 AWS Glue 資料目錄。若要將資 AWS Glue 料目錄與 Redshift 頻譜搭配使用,您可能需要變更 IAM 政策。如需詳細資訊,請參閱 Athena 使用指南中的升級至資 AWS Glue 料目錄

Amazon S3 許可

您的叢集至少需要對您 Amazon S3 儲存貯體的 GET 和 LIST 存取權限。如果您的儲存貯體與叢集不在同一個 AWS 帳戶中,您的儲存貯體也必須授權叢集存取資料。如需詳細資訊,請參閱授權 Amazon Redshift 代表您存取其他 AWS 服務

注意

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

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

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

下列政策會授予對您名為 myBucket 之 Amazon S3 儲存貯體的 GET 和 LIST 存取權限。

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

跨帳戶 Amazon S3 許可

若要授與 Redshift 頻譜存取屬於其他 AWS 帳戶之 Amazon S3 儲存貯體中資料的權限,請將以下政策新增至 Amazon S3 儲存貯體。如需詳細資訊,請參閱授予跨帳戶儲存貯體許可

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

授予或限制使用 Redshift Spectrum 存取的政策

若要授予只使用 Redshift Spectrum 的 Amazon S3 儲存貯體存取權,請加入允許使用者代理程式 AWS Redshift/Spectrum 存取的條件。以下政策僅允許 Redshift Spectrum 存取 Amazon S3 儲存貯體。它排除了其他存取權限,例如 COPY 操作。

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": ["s3:Get*", "s3:List*"], "Resource": "arn:aws:s3:::myBucket/*", "Condition": {"StringEquals": {"aws:UserAgent": "AWS Redshift/Spectrum"}} }] }

同樣的,您可能希望建立一個允許存取 COPY 操作,但不包括 Redshift Spectrum 存取的 IAM 角色。若要這麼做,請加入拒絕使用者代理程式 AWS Redshift/Spectrum 存取的條件。以下政策允許存取除 Redshift Spectrum 之外的 Amazon S3 儲存貯體。

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": ["s3:Get*", "s3:List*"], "Resource": "arn:aws:s3:::myBucket/*", "Condition": {"StringNotEquals": {"aws:UserAgent": "AWS Redshift/Spectrum"}} }] }

授予最低許可的政策

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

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetBucketLocation", "s3:GetObject", "s3:ListMultipartUploadParts", "s3:ListBucket", "s3:ListBucketMultipartUploads" ], "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 存取權限。

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

在 Amazon Redshift Spectrum 中鏈結 IAM 角色

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

若要鏈結角色,您應建立角色間的信任關係。擔任其他角色的角色,必須擁有允許其擔任指定角色的許可政策。另一方面,要傳送許可的角色必須擁有信任政策,允許其將許可傳送給其他鏈結的角色。如需詳細資訊,請參閱在 Amazon Redshift 中鏈結 IAM 角色

當您執行 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 資料目錄的存取

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

以下各節說明存取資料目錄中儲存之資料的各種層級存取 AWS Glue 權管理政策。

資料庫操作的政策

如果您想要授與使用者檢視和建立資料庫的權限,他們需要資料庫和 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 政策會提供用於建立資料庫所需的最低許可。

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

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

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

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

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

資料表操作的政策

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

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

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 政策會提供用於建立外部資料表所需的最低許可。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "glue:CreateTable" ], "Resource": [ "arn:aws:glue:us-west-2:redshift-account:catalog", "arn:aws:glue:us-west-2:redshift-account:database/example_db", "arn:aws:glue:us-west-2:redshift-account: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 政策會提供用於列出目前的外部資料表所需的最低許可。

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

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

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

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

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

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

DROP TABLE example_db.example_tbl0;

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

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

分割區操作的政策

如果要提供使用者執行分割區層級操作 (檢視、建立、捨棄、變更等等) 的許可,使用者需要分割區所屬資料表的許可。他們也需要相關資料庫和 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 政策會提供用於建立分割區所需的最低許可。

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

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

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

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

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

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

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

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

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

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

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

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

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