

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 为 AWS CodeStar 通知使用服务相关角色
<a name="using-service-linked-roles"></a>

AWS CodeStar 通知使用 AWS Identity and Access Management (IAM) [服务相关角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_terms-and-concepts.html#iam-term-service-linked-role)。服务相关角色是一种独特的 IAM 角色，直接链接到 AWS CodeStar 通知。服务相关角色由 AWS CodeStar 通知预定义，包括该服务代表您调用其他 AWS 服务所需的所有权限。您首次创建通知规则时为您创建此角色。您不必创建角色。

服务相关角色可以更轻松地设置 AWS CodeStar 通知，因为您不必手动添加权限。 AWS CodeStar 通知定义了其服务相关角色的权限，除非另有定义，否则只有 AWS CodeStar 通知才能扮演其角色。定义的权限包括信任策略和权限策略，而且权限策略不能附加到任何其他 IAM 实体。

要删除服务相关角色，您必须先删除其相关资源。这可以保护您的 AWS CodeStar 通知资源，因为您不能无意中删除访问这些资源的权限。

有关支持服务相关角色的其他服务的信息，请参阅[与 IAM 配合使用的AWS 服务](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-services-that-work-with-iam.html)。

## 通知的服务相关角色权限 AWS CodeStar
<a name="slr-permissions"></a>

AWS CodeStar 通知使用 AWSServiceRoleForCodeStarNotifications 服务相关角色来检索工具链中发生的事件的相关信息，并向您指定的目标发送通知。

 AWSServiceRoleForCodeStarNotifications 服务相关角色信任以下服务来代入该角色：
+ `codestar-notifications.amazonaws.com`

角色权限策略允许 AWS CodeStar 通知在指定资源上完成以下操作：
+ 操作：`CloudWatch Event rules that are named awscodestar-notifications-*` 上的 `PutRule`
+ 操作：`DescribeRule` 上的 `CloudWatch Event rules that are named awscodestar-notifications-*`
+ 操作：`PutTargets` 上的 `CloudWatch Event rules that are named awscodestar-notifications-*`
+ 操作：`CreateTopic` 到 `create Amazon SNS topics for use with AWS CodeStar Notifications with the prefix CodeStarNotifications-`
+ 操作：`all comments on all pull requests in all CodeCommit repositories in the AWS account` 上的 `GetCommentsForPullRequests`
+ 操作：`GetCommentsForComparedCommit` 上的 `all comments on all commits in all CodeCommit repositories in the AWS account`
+ 操作：`GetDifferences` 上的 `all commits in all CodeCommit repositories in the AWS account`
+ 操作：`GetCommentsForComparedCommit` 上的 `all comments on all commits in all CodeCommit repositories in the AWS account`
+ 操作：`GetDifferences` 上的 `all commits in all CodeCommit repositories in the AWS account`
+ 操作：`DescribeSlackChannelConfigurations` 上的 `all AWS Chatbot clients in the AWS account`
+ 操作：`UpdateSlackChannelConfiguration` 上的 `all AWS Chatbot clients in the AWS account`
+ 操作：`ListActionExecutions` 上的 `all actions in all pipelines in the AWS account`
+ 操作：`GetFile` 上的 `all files in all CodeCommit repositories in the AWS account unless otherwise tagged`

您可以在 AWSServiceRoleForCodeStarNotifications 服务相关角色的策略声明中看到这些操作。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": "arn:aws:events:*:*:rule/awscodestarnotifications-*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "sns:CreateTopic"
            ],
            "Resource": "arn:aws:sns:*:*:CodeStarNotifications-*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "codecommit:GetCommentsForPullRequest",
                "codecommit:GetCommentsForComparedCommit",
                "codecommit:GetDifferences",
                "chatbot:DescribeSlackChannelConfigurations",
                "chatbot:UpdateSlackChannelConfiguration",
                "codepipeline:ListActionExecutions"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "codecommit:GetFile"
            ],
            "Resource": "*",
            "Condition": {
                "StringNotEquals": {
                    "aws:ResourceTag/ExcludeFileContentFromNotifications": "true"
                }
            },
            "Effect": "Allow"
        }
    ]
}
```

------

您必须配置权限，允许 IAM 实体（如用户、组或角色）创建、编辑或删除服务关联角色。有关更多信息，请参阅《IAM 用户指南》**中的[服务关联角色权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/using-service-linked-roles.html#service-linked-role-permissions)。

## 为通知创建服务相关角色 AWS CodeStar
<a name="create-slr"></a>

您无需手动创建服务关联角色。您可以使用开发者工具控制台或 AWS CLI 或中的 CreateNotificationRule API SDKs 来创建通知规则。您也可以直接调用 API。无论您使用哪种方法，都会为您创建服务相关角色。

如果您删除该服务关联角色，然后需要再次创建，您可以使用相同流程在账户中重新创建此角色。您可以使用开发者工具控制台或 AWS CLI 或中的 CreateNotificationRule API SDKs 来创建通知规则。您也可以直接调用 API。无论您使用哪种方法，都会为您创建服务相关角色。

## 编辑通知的服务相关角色 AWS CodeStar
<a name="edit-slr"></a>

创建服务相关角色后，您将无法更改其名称，因为可能有多种实体引用该角色。但是，您可以使用 IAM 编辑角色描述。有关更多信息，请参见 *IAM 用户指南*中的[编辑服务相关角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/using-service-linked-roles.html#edit-service-linked-role)。

## 删除通知的服务相关角色 AWS CodeStar
<a name="delete-slr"></a>

如果您不再需要使用某个需要服务相关角色的功能或服务，我们建议您删除该角色。这样您就没有未被主动监控或维护的未使用实体。您必须先清除服务相关角色的资源，然后才能将其删除。对于 AWS CodeStar 通知，这意味着删除您 AWS 账户中使用服务角色的所有通知规则。

**注意**  
如果您尝试删除资源时 AWS CodeStar 通知服务正在使用该角色，则删除可能会失败。如果发生这种情况，请等待几分钟后重试。

**删除使用的 AWS CodeStar 通知资源 AWSService RoleForCodeStarNotifications**

1. 在[https://console.aws.amazon.com/codesuite/设置/ AWS](https://console.aws.amazon.com/codesuite/settings/notifications/) 通知中打开开发者工具控制台。
**注意**  
通知规则适用于创建通知规则的 AWS 区域。如果您在多个 AWS 区域有通知规则，请使用区域选择器来更改 AWS 区域。

1. 选择列表中出现的所有通知规则，然后选择**删除**。

1. 在您创建通知规则的所有 AWS 区域中重复这些步骤。

**要**使用 IAM** 删除服务相关角色**

使用 IAM 控制台或 AWS Identity and Access Management API 删除 AWSServiceRoleForCodeStarNotifications 服务相关角色。 AWS CLI有关更多信息，请参见《IAM 用户指南》**中的[删除服务相关角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/using-service-linked-roles.html#delete-service-linked-role)。

## AWS CodeStar 通知服务相关角色支持的区域
<a name="slr-regions"></a>

AWS CodeStar 通知支持在提供服务的所有 AWS 区域中使用服务相关角色。有关更多信息，请参阅[AWS 区域和终端节点以及AWS](https://docs.aws.amazon.com/general/latest/gr/rande.html)[CodeStar 通知](https://docs.aws.amazon.com/general/latest/gr/codestar_notifications.html)。