選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

許可和 AWS CDK

焦點模式
許可和 AWS CDK - AWS Cloud Development Kit (AWS CDK) v2

這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。

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

這是 AWS CDK v2 開發人員指南。較舊的 CDK v1 已於 2022 年 6 月 1 日進入維護,並於 2023 年 6 月 1 日結束支援。

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

AWS 建構程式庫使用一些常見、廣泛實作的慣用語來管理存取和許可。IAM 模組為您提供使用這些慣用詞所需的工具。

AWS CDK 使用 AWS CloudFormation 來部署變更。每個部署都涉及開始 AWS CloudFormation 部署的演員 (開發人員或自動化系統)。在此過程中,演員將擔任一或多個 IAM 身分 (使用者或角色),並選擇性地將角色傳遞給 AWS CloudFormation。

如果您使用 AWS IAM Identity Center 來驗證使用者身分,則單一登入提供者會提供短期工作階段登入資料,授權您擔任預先定義的 IAM 角色。若要了解 如何從 IAM Identity Center 身分驗證 AWS CDK 取得 AWS 憑證,請參閱 AWS SDKs和工具參考指南中的了解 IAM Identity Center 身分驗證

主體

IAM 主體是經過驗證的 AWS 實體,代表可呼叫 AWS APIs的使用者、服務或應用程式。 AWS Construct Library 支援以多種彈性方式指定委託人,以授予委託人存取您的 AWS 資源。

在安全內容中,「主體」一詞特別是指已驗證的實體,例如使用者。群組和角色等物件不代表使用者 (和其他已驗證的實體),而是為了授予許可而間接識別

例如,如果您建立 IAM 群組,您可以授予群組 (及其成員) 對 Amazon RDS 資料表的寫入存取權。不過,群組本身不是委託人,因為它不代表單一實體 (也無法登入群組)。

在 CDK 的 IAM 程式庫中,直接或間接識別主體的類別會實作IPrincipal界面,允許這些物件在存取政策中可互換使用。不過,並非所有這些都是安全意識的主體。這些物件包括:

  1. IAM 資源,例如 RoleUserGroup

  2. 服務主體 (new iam.ServicePrincipal('service.amazonaws.com'))

  3. 聯合委託人 (new iam.FederatedPrincipal('cognito-identity.amazonaws.com'))

  4. 帳戶主體 (new iam.AccountPrincipal('0123456789012'))

  5. 正式使用者主體 (new iam.CanonicalUserPrincipal('79a59d[...]7ef2be'))

  6. AWS Organizations 主體 (new iam.OrganizationPrincipal('org-id'))

  7. 任意 ARN 主體 (new iam.ArnPrincipal(res.arn))

  8. iam.CompositePrincipal(principal1, principal2, ...) 信任多個主體的

授權

每個代表可存取之資源的建構,例如 Amazon S3 儲存貯體或 Amazon DynamoDB 資料表,都有將存取權授予另一個實體的方法。所有這些方法的名稱都以授予開頭。

例如,Amazon S3 儲存貯體具有方法 grantReadgrantReadWrite(Python:grant_readgrant_read_write),分別啟用從實體到儲存貯體的讀取和寫入存取權。實體不必確切知道執行這些操作需要哪些 Amazon S3 IAM 許可。

授予方法的第一個引數一律為類型 IGrantable。此界面代表可授予許可的實體。也就是說,它代表具有 角色的資源,例如 IAM 物件 RoleUserGroup

也可以授予其他實體許可。例如,本主題稍後會示範如何授予 CodeBuild 專案對 Amazon S3 儲存貯體的存取權。一般而言,關聯角色是透過被授予存取權之實體的 role 屬性取得。

使用執行角色的資源,例如 lambda.Function,也會實作 IGrantable,因此您可以直接授予他們存取權,而不是授予對其角色的存取權。例如,如果 bucket 是 Amazon S3 儲存貯體,而 function 是 Lambda 函數,則下列程式碼會授予該函數對儲存貯體的讀取存取權。

TypeScript
bucket.grantRead(function);
JavaScript
bucket.grantRead(function);
Python
bucket.grant_read(function)
Java
bucket.grantRead(function);
C#
bucket.GrantRead(function);
bucket.grantRead(function);

有時候,必須在部署堆疊時套用許可。其中一種情況是,當您將 AWS CloudFormation 自訂資源存取權授予其他一些資源時。自訂資源將在部署期間調用,因此在部署時間必須具有指定的許可。

另一個情況是,當服務驗證您傳遞給該服務的角色已套用正確的政策時。(許多 AWS 服務會執行此操作,以確保您不會忘記設定政策。) 在這些情況下,如果套用許可太晚,部署可能會失敗。

若要強制在建立其他資源之前套用授予的許可,您可以新增授予本身的相依性,如下所示。雖然授予方法的傳回值通常會遭到捨棄,但每個授予方法實際上都會傳回iam.Grant物件。

TypeScript
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);
JavaScript
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);
Python
grant = bucket.grant_read(function) custom = CustomResource(...) custom.node.add_dependency(grant)
Java
Grant grant = bucket.grantRead(function); CustomResource custom = new CustomResource(...); custom.node.addDependency(grant);
C#
var grant = bucket.GrantRead(function); var custom = new CustomResource(...); custom.node.AddDependency(grant);
const grant = bucket.grantRead(lambda); const custom = new CustomResource(...); custom.node.addDependency(grant);

角色

IAM 套件包含代表 Role IAM 角色的建構。下列程式碼會建立新的角色,信任 Amazon EC2 服務。

TypeScript
import * as iam from 'aws-cdk-lib/aws-iam'; const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), // required });
JavaScript
const iam = require('aws-cdk-lib/aws-iam'); const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com') // required });
Python
import aws_cdk.aws_iam as iam role = iam.Role(self, "Role", assumed_by=iam.ServicePrincipal("ec2.amazonaws.com")) # required
Java
import software.amazon.awscdk.services.iam.Role; import software.amazon.awscdk.services.iam.ServicePrincipal; Role role = Role.Builder.create(this, "Role") .assumedBy(new ServicePrincipal("ec2.amazonaws.com")).build();
C#
using Amazon.CDK.AWS.IAM; var role = new Role(this, "Role", new RoleProps { AssumedBy = new ServicePrincipal("ec2.amazonaws.com"), // required });
import * as iam from 'aws-cdk-lib/aws-iam'; const role = new iam.Role(this, 'Role', { assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'), // required });

您可以透過呼叫角色的 addToPolicy方法 (Python:add_to_policy),將許可新增至角色,傳入定義要新增之規則PolicyStatement的 。陳述式會新增至角色的預設政策;如果沒有,則會建立一個。

下列範例會將Deny政策陳述式新增至 動作的 角色,ec2:SomeAction並在 資源 bucketotherRole(Python:other_role) s3:AnotherAction上新增,條件為授權的服務為 AWS CodeBuild。

TypeScript
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com', }}}));
JavaScript
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com' }}}));
Python
role.add_to_policy(iam.PolicyStatement( effect=iam.Effect.DENY, resources=[bucket.bucket_arn, other_role.role_arn], actions=["ec2:SomeAction", "s3:AnotherAction"], conditions={"StringEquals": { "ec2:AuthorizedService": "codebuild.amazonaws.com"}} ))
Java
role.addToPolicy(PolicyStatement.Builder.create() .effect(Effect.DENY) .resources(Arrays.asList(bucket.getBucketArn(), otherRole.getRoleArn())) .actions(Arrays.asList("ec2:SomeAction", "s3:AnotherAction")) .conditions(java.util.Map.of( // Map.of requires Java 9 or later "StringEquals", java.util.Map.of( "ec2:AuthorizedService", "codebuild.amazonaws.com"))) .build());
C#
role.AddToPolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.DENY, Resources = new string[] { bucket.BucketArn, otherRole.RoleArn }, Actions = new string[] { "ec2:SomeAction", "s3:AnotherAction" }, Conditions = new Dictionary<string, object> { ["StringEquals"] = new Dictionary<string, string> { ["ec2:AuthorizedService"] = "codebuild.amazonaws.com" } } }));
role.addToPolicy(new iam.PolicyStatement({ effect: iam.Effect.DENY, resources: [bucket.bucketArn, otherRole.roleArn], actions: ['ec2:SomeAction', 's3:AnotherAction'], conditions: {StringEquals: { 'ec2:AuthorizedService': 'codebuild.amazonaws.com', }}}));

在上述範例中,我們已使用 addToPolicy(Python:add_to_policy) 呼叫建立新的PolicyStatement內嵌。您也可以傳入現有的政策陳述式或您已修改的政策陳述式。PolicyStatement 物件有多種新增主體、資源、條件和動作的方法

如果您使用的建構需要角色才能正常運作,您可以執行下列其中一項:

  • 在執行個體化建構物件時,傳遞現有角色。

  • 讓建構為您建立新的角色,信任適當的服務主體。下列範例使用這類建構:CodeBuild 專案。

TypeScript
import * as codebuild from 'aws-cdk-lib/aws-codebuild'; // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole: iam.IRole | undefined = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole, });
JavaScript
const codebuild = require('aws-cdk-lib/aws-codebuild'); // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole });
Python
import aws_cdk.aws_codebuild as codebuild # imagine role_or_none is a function that might return a Role object # under some conditions, and None under other conditions some_role = role_or_none(); project = codebuild.Project(self, "Project", # if role is None, the Project creates a new default role, # trusting the codebuild.amazonaws.com service principal role=some_role)
Java
import software.amazon.awscdk.services.iam.Role; import software.amazon.awscdk.services.codebuild.Project; // imagine roleOrNull is a function that might return a Role object // under some conditions, and null under other conditions Role someRole = roleOrNull(); // if someRole is null, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal Project project = Project.Builder.create(this, "Project") .role(someRole).build();
C#
using Amazon.CDK.AWS.CodeBuild; // imagine roleOrNull is a function that might return a Role object // under some conditions, and null under other conditions var someRole = roleOrNull(); // if someRole is null, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal var project = new Project(this, "Project", new ProjectProps { Role = someRole });
import * as codebuild from 'aws-cdk-lib/aws-codebuild'; // imagine roleOrUndefined is a function that might return a Role object // under some conditions, and undefined under other conditions const someRole: iam.IRole | undefined = roleOrUndefined(); const project = new codebuild.Project(this, 'Project', { // if someRole is undefined, the Project creates a new default role, // trusting the codebuild.amazonaws.com service principal role: someRole, });

建立物件後,角色 (無論角色是傳入,還是由建構建立的預設角色) 可做為 屬性 使用role。不過,此屬性不適用於外部資源。因此,這些建構具有 addToRolePolicy(Python:add_to_role_policy) 方法。

如果建構是外部資源,而且它呼叫 role 屬性的 addToPolicy(Python:add_to_policy) 方法,則 方法不會採取任何動作。這可為您節省明確處理未定義案例的麻煩。

下列範例示範:

TypeScript
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, // ... and so on defining the policy }));
JavaScript
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW // ... and so on defining the policy }));
Python
# project is imported into the CDK application project = codebuild.Project.from_project_name(self, 'Project', 'ProjectName') # project is imported, so project.role is undefined, and this call has no effect project.add_to_role_policy(iam.PolicyStatement( effect=iam.Effect.ALLOW, # ... and so on defining the policy )
Java
// project is imported into the CDK application Project project = Project.fromProjectName(this, "Project", "ProjectName"); // project is imported, so project.getRole() is null, and this call has no effect project.addToRolePolicy(PolicyStatement.Builder.create() .effect(Effect.ALLOW) // .. and so on defining the policy .build();
C#
// project is imported into the CDK application var project = Project.FromProjectName(this, "Project", "ProjectName"); // project is imported, so project.role is null, and this call has no effect project.AddToRolePolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, // ... and so on defining the policy }));
// project is imported into the CDK application const project = codebuild.Project.fromProjectName(this, 'Project', 'ProjectName'); // project is imported, so project.role is undefined, and this call has no effect project.addToRolePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, // ... and so on defining the policy }));

資源政策

中的一些資源 AWS,例如 Amazon S3 儲存貯體和 IAM 角色,也具有資源政策。這些建構有 addToResourcePolicy方法 (Python:add_to_resource_policy),以 PolicyStatement做為其引數。新增至資源政策的每個政策陳述式必須至少指定一個主體。

在下列範例中,Amazon S3 儲存貯體會將具有 本身s3:SomeAction許可的角色bucket授予 。

TypeScript
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));
JavaScript
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));
Python
bucket.add_to_resource_policy(iam.PolicyStatement( effect=iam.Effect.ALLOW, actions=["s3:SomeAction"], resources=[bucket.bucket_arn], principals=role))
Java
bucket.addToResourcePolicy(PolicyStatement.Builder.create() .effect(Effect.ALLOW) .actions(Arrays.asList("s3:SomeAction")) .resources(Arrays.asList(bucket.getBucketArn())) .principals(Arrays.asList(role)) .build());
C#
bucket.AddToResourcePolicy(new PolicyStatement(new PolicyStatementProps { Effect = Effect.ALLOW, Actions = new string[] { "s3:SomeAction" }, Resources = new string[] { bucket.BucketArn }, Principals = new IPrincipal[] { role } }));
bucket.addToResourcePolicy(new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['s3:SomeAction'], resources: [bucket.bucketArn], principals: [role] }));

使用外部 IAM 物件

如果您已在 AWS CDK 應用程式外部定義 IAM 使用者、主體、群組或角色,您可以在 AWS CDK 應用程式中使用該 IAM 物件。若要這樣做,請使用其 ARN 或其名稱建立參考。(使用者、群組和角色請使用 名稱。) 然後,傳回的參考可用來授予許可或建構前述的政策陳述式。

政策 (包括受管政策) 可以使用下列方法,以類似方式使用。您可以在任何需要 IAM 政策的地方使用這些物件的參考。

注意

如同所有外部 AWS 資源的參考,您無法修改 CDK 應用程式中的外部 IAM 物件。

下一個主題:

內容值

上一個主題:

資產
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。