基于身份的策略 (IAM) 示例 - AWS CodePipeline

基于身份的策略 (IAM) 示例

您可以向 IAM 身份附加策略。例如,您可以执行以下操作:

  • 将权限策略附加到您的账户中的用户或组:要向用户授予查看 CodePipeline 控制台中管道的权限,您可以将权限策略附加到用户或用户所属的组。

  • 向角色附加权限策略(授予跨账户权限) – 您可以向 IAM 角色附加基于身份的权限策略,以授予跨账户的权限。例如,账户 A 中的管理员可以创建一个角色,以向其他 AWS 账户(如账户 B)或 AWS 服务 授予跨账户权限,如下所述:

    1. 账户 A 管理员可以创建一个 IAM 角色,然后向该角色附加授予其访问账户 A 中资源的权限策略。

    2. 账户 A 管理员可以把信任策略附加至用来标识账户 B 的角色,账户 B 由此可以作为主体代入该角色。

    3. 之后,账户 B 管理员可以将担任该角色的权限委派给账户 B 中的任何用户。这样一来,账户 B 中的用户就可以在账户 A 中创建或访问资源。如果您需要向 AWS 服务 授予担任该角色的权限,则信任策略中的主体也可以是 AWS 服务 主体。

    有关使用 IAM 委托权限的更多信息,请参阅 IAM 用户指南中的访问权限管理

下面显示了一个权限策略的示例,该策略可以授权启用和禁用 us-west-2 region 区域名为 MyFirstPipeline 的管道中所有阶段之间的转换:

JSON
{ "Version":"2012-10-17", "Statement" : [ { "Effect" : "Allow", "Action" : [ "codepipeline:EnableStageTransition", "codepipeline:DisableStageTransition" ], "Resource" : [ "arn:aws:codepipeline:us-west-2:111222333444:MyFirstPipeline/*" ] } ] }

以下示例显示了 111222333444 账户中的一项策略,该策略允许用户在 CodePipeline 控制台中查看,但不能更改名为 MyFirstPipeline 的管道。该策略基于 AWSCodePipeline_ReadOnlyAccess 托管策略,但由于它特定于 MyFirstPipeline 管道,因此无法直接使用托管策略。如果您不想将策略限制于某特定管道,则应该考虑使用由 CodePipeline 创建和维护的托管式策略之一。有关更多信息,请参阅使用管理的策略。您必须将该策略附加到您为进行访问而创建的 IAM 角色,例如名为 CrossAccountPipelineViewers 的角色:

JSON
{ "Version":"2012-10-17", "Statement": [ { "Action": [ "codepipeline:GetPipeline", "codepipeline:GetPipelineState", "codepipeline:GetPipelineExecution", "codepipeline:ListPipelineExecutions", "codepipeline:ListActionExecutions", "codepipeline:ListActionTypes", "codepipeline:ListPipelines", "codepipeline:ListTagsForResource", "iam:ListRoles", "s3:ListAllMyBuckets", "codecommit:ListRepositories", "codedeploy:ListApplications", "lambda:ListFunctions", "codestar-notifications:ListNotificationRules", "codestar-notifications:ListEventTypes", "codestar-notifications:ListTargets" ], "Effect": "Allow", "Resource": "arn:aws:codepipeline:us-west-2:111222333444:MyFirstPipeline" }, { "Action": [ "codepipeline:GetPipeline", "codepipeline:GetPipelineState", "codepipeline:GetPipelineExecution", "codepipeline:ListPipelineExecutions", "codepipeline:ListActionExecutions", "codepipeline:ListActionTypes", "codepipeline:ListPipelines", "codepipeline:ListTagsForResource", "iam:ListRoles", "s3:GetBucketPolicy", "s3:GetObject", "s3:ListBucket", "codecommit:ListBranches", "codedeploy:GetApplication", "codedeploy:GetDeploymentGroup", "codedeploy:ListDeploymentGroups", "elasticbeanstalk:DescribeApplications", "elasticbeanstalk:DescribeEnvironments", "lambda:GetFunctionConfiguration", "opsworks:DescribeApps", "opsworks:DescribeLayers", "opsworks:DescribeStacks" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "CodeStarNotificationsReadOnlyAccess", "Effect": "Allow", "Action": [ "codestar-notifications:DescribeNotificationRule" ], "Resource": "*", "Condition": { "ArnLike": { "codestar-notifications:NotificationsForResource": "arn:aws:iam::*:role/Service*" } } } ] }

创建此策略后,在 111222333444 账户中创建 IAM 角色,并将策略附加到该角色。在角色的信任关系中,您必须添加将担任该角色的 AWS 账户。以下示例显示了允许 111111111111 AWS 账户中的用户担任 111222333444 账户中定义的角色的策略:

JSON
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::111111111111:root" }, "Action": "sts:AssumeRole" } ] }

以下示例显示了在 111111111111 AWS 账户中创建的策略,该策略允许用户担任 111222333444 账户中名为 CrossAccountPipelineViewers 的角色:

JSON
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRole", "Resource": "arn:aws:iam::111222333444:role/CrossAccountPipelineViewers" } ] }

您可以创建 IAM 策略来限制您账户中的用户有权访问的调用和资源,然后将这些策略与管理用户相关联。有关创建 IAM 角色和探索适用于 CodePipeline 的 IAM 策略语句的更多信息,请参阅客户管理型策略示例