

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

# 在 Amazon 中使用 IAM 政策条件 EventBridge
<a name="eb-use-conditions"></a>

要授予权限，可在策略语句中使用 IAM 策略语言指定条件，规定策略何时生效。例如，您可以指定仅在特定日期后应用的策略。

策略中的条件由键值对组成。条件键不区分大小写。

如果您在单个条件中指定多个条件或密钥，则必须满足所有条件和密钥 EventBridge 才能授予权限。如果您为一个密钥指定具有多个值的单个条件，则在满足其中一个值时 EventBridge 授予权限。

指定条件时，您也可以使用占位符或*策略变量*。有关更多信息，请参阅《IAM 用户指南》中的[策略变量](https://docs.aws.amazon.com/IAM/latest/UserGuide/policyvariables.html)。**有关使用 IAM 策略语言指定条件的更多信息，请参阅《IAM 用户指南》**中的[条件](https://docs.aws.amazon.com/IAM/latest/UserGuide/AccessPolicyLanguage_ElementDescriptions.html#Condition)。

默认情况下，IAM 用户和角色无法访问您账户中的[事件](eb-events.md)。要访问事件，用户必须获得 `PutRule` API 操作的授权。如果授权 IAM 用户或角色执行 `events:PutRule` 操作，他们可以创建匹配特定事件的[规则](eb-rules.md)。但是，要使规则发挥作用，用户还必须拥有该`events:PutTargets`操作的权限，因为如果您希望规则的作用不仅仅是发布 CloudWatch 指标，则还必须向规则中添加[目标](eb-targets.md)。

可以在 IAM 用户或角色的策略语句中提供条件，允许该用户或角色创建仅匹配一组特定源和事件类型的规则。要对特定源和事件类型授予访问权限，请使用 `events:source` 和 `events:detail-type` 条件键。

同样，您可以在 IAM 用户或角色的策略语句中提供条件，允许该用户或角色创建规则，仅匹配您账户中的特定资源。要对特定资源授予访问权限，请使用 `events:TargetArn` 条件键。

## EventBridge 条件键
<a name="conditions-table"></a>

下表显示了可在中的策略中使用的条件键以及密钥和值对 EventBridge。


| 条件键 | 键值对 | 评估类型 | 
| --- | --- | --- | 
|  aws：SourceAccount  |  `aws:SourceArn` 指定的规则所在的账户。  |  Account Id、Null  | 
|  aws：SourceArn  |  发送事件的规则的 ARN。  |  ARN，Null  | 
|  events:creatorAccount  |  `"events:creatorAccount":"creatorAccount"` 对于*creatorAccount*，请使用创建规则的账户的账户 ID。使用此条件授权 API 调用来自特定账户的规则。  |  creatorAccount、Null  | 
|  events:detail-type  |  `"events:detail-type":"detail-type "` 其中*detail-type*是事件的**详细类型**字段的文字字符串，例如和。`"AWS API Call via CloudTrail"` `"EC2 Instance State-change Notification"`  |  Detail Type，Null  | 
|  事件：详情。 eventTypeCode  |  `"events:detail.eventTypeCode":"eventTypeCode"` 对于*eventTypeCode*，请使用文字字符串作为**详细信息。 eventTypeCode**事件的字段，例如`"AWS_ABUSE_DOS_REPORT"`。  |  eventTypeCode，空  | 
|  events: detail.service  |  `"events:detail.service":"service"` 对于*service*，使用文字字符串作为事件的 **detail.service** 字段，例如。`"ABUSE"`  |  service，Null  | 
|  events: detail.userIdentity.principalId  |  `"events:detail.userIdentity.principalId":"principal-id"` 对于*principal-id*，使用事件的 **detail.userIdentity.principalID 字段的文字字符串，详细类型为，例如。** `"AWS API Call via CloudTrail"` `"AROAIDPPEZS35WEXAMPLE:AssumedRoleSessionName."`  |  Principal Id，Null  | 
|  事件:eventBusInvocation  |  `"events:eventBusInvocation":"boolean"` 对于*boolean*，当规则将事件发送到另一个账户中的事件总线的目标时，使用 true。使用 `PutEvents` API 调用时，使用 false。  |  eventBusInvocation，空  | 
|  事件:ManagedBy  |  由 AWS 服务内部使用。对于 AWS 服务代表您创建的规则，该值是创建该规则的服务的主体名称。  |  不要在客户策略中使用。  | 
|  events:source  |  `"events:source":"source "` *source*用于事件源字段的文字字符串，例如`"aws.ec2"`或`"aws.s3"`。有关更多可能的值*source*，请参阅中的示例事件[来自 AWS 服务的事件](eb-events.md#eb-service-event)。  |  Source，Null  | 
|  事件:TargetArn  |  `"events:TargetArn":"target-arn "` 例如*target-arn*，使用目标的 ARN 作为规则。`"arn:aws:lambda:*:*:function:*"`  |  ArrayOfARN，空值  | 

有关政策声明的示例 EventBridge，请参阅[管理对您的 Amazon EventBridge 资源的访问权限](eb-manage-iam-access.md)。

**Topics**
+ [EventBridge 条件键](#conditions-table)
+ [EventBridge 管道细节](#eb-pipes-condition-diff)
+ [示例：使用 `creatorAccount` 条件](#eb-events-creator-account)
+ [示例：使用 `eventBusInvocation` 条件](#eb-events-bus-invocation)
+ [示例：限制对特定源的访问](#eb-events-limit-access-control)
+ [示例：定义可在事件模式中分别使用的多个源](#eb-events-pattern-sources)
+ [示例：确保在事件模式中定义源](#eb-source-defined-events-pattern)
+ [示例：在包含多个源的事件模式中定义允许的源的列表](#eb-allowed-sources-events-pattern)
+ [示例：由 `detail.service` 限制 `PutRule` 访问权限](#eb-limit-rule-by-service)
+ [示例：由 `detail.eventTypeCode` 限制 `PutRule` 访问权限](#eb-limit-rule-by-type-code)
+ [示例：确保只允许来自特定`PrincipalId`用户的 API 调用 AWS CloudTrail 的事件](#eb-consume-specific-events)
+ [示例：限制对目标的访问](#eb-limiting-access-to-targets)

## EventBridge 管道细节
<a name="eb-pipes-condition-diff"></a>

EventBridge Pipes 不支持任何其他 IAM 策略条件密钥。

## 示例：使用 `creatorAccount` 条件
<a name="eb-events-creator-account"></a>

以下示例策略语句说明如何在策略中使用 `creatorAccount` 条件，仅当指定为 `creatorAccount` 的账户是创建规则的账户时，才允许创建规则。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleForOwnedRules",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "StringEqualsIfExists": {
                    "events:creatorAccount": "${aws:PrincipalAccount}"
                }
            }
        }
    ]
}
```

------

## 示例：使用 `eventBusInvocation` 条件
<a name="eb-events-bus-invocation"></a>

`eventBusInvocation` 指示调用是来自跨账户目标还是 `PutEvents` API 请求。如果调用源于包含跨账户目标的规则，例如目标为另一账户中的事件总线，则该值为 **true**。如果调用源于 `PutEvents` API 请求，该值为 **false**。以下示例表示来自跨账户目标的调用。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowCrossAccountInvocationEventsOnly",
      "Effect": "Allow",
      "Action": "events:PutEvents",
      "Resource": "*",
      "Condition": {
        "BoolIfExists": {
          "events:eventBusInvocation": "true"
        }
      }
    }
  ]
}
```

------

## 示例：限制对特定源的访问
<a name="eb-events-limit-access-control"></a>

以下示例策略可附加到一个 IAM 用户。策略 A 允许所有事件的 `PutRule` API 操作，而策略 B 仅在所创建规则的事件模式与 Amazon EC2 事件匹配时才允许 `PutRule` 操作。

**策略 A：允许所有事件**

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleForAllEvents",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*"
        }
    ]
    }
```

------

**策略 B：仅允许来自 Amazon EC2 的事件** 

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
    "Sid": "AllowPutRuleForAllEC2Events",
    "Effect": "Allow",
    "Action": "events:PutRule",
    "Resource": "*",
    "Condition": {
    "ForAllValues:StringEquals": {
    "events:source": "aws.ec2"
    }
    }
    }
    ]
    }
```

------

`EventPattern` 是 `PutRule` 的必需参数。因此，如果具有策略 B 的用户使用类似于下面的事件模式调用 `PutRule`：

```
{
    "source": [ "aws.ec2" ]
}
```

将创建规则，因为策略允许此特定源，即 `"aws.ec2"`。但是，如果具有策略 B 的用户使用类似下面的事件模式调用 `PutRule`，则将拒绝该规则创建操作，因为策略不允许此特定源：也就是 `"aws.s3"`。

```
{
    "source": [ "aws.s3" ]
}
```

实质上，仅允许具有策略 B 的用户创建与源自 Amazon EC2 的事件匹配的规则，因此他们只能访问 Amazon EC2 中的事件。

有关策略 A 和策略 B 的比较，请参阅下表。


| 事件模式 | 策略 A 允许的 | 策略 B 允许的 | 
| --- | --- | --- | 
|  <pre>{<br />    "source": [ "aws.ec2" ]<br />}</pre>  |  支持  |  是  | 
|  <pre>{<br />    "source": [ "aws.ec2", "aws.s3" ]<br />}</pre>  |  是  |  否（不允许源 aws.s3）  | 
|  <pre>{<br />    "source": [ "aws.ec2" ],<br />    "detail-type": [ "EC2 Instance State-change Notification" ]<br />}</pre>  |  支持  |  是  | 
|  <pre>{<br />    "detail-type": [ "EC2 Instance State-change Notification" ]<br />}</pre>  |  是  |  否 (必须指定源)  | 

## 示例：定义可在事件模式中分别使用的多个源
<a name="eb-events-pattern-sources"></a>

以下策略允许 IAM 用户或角色创建一条规则，`EventPattern` 中的源是 Amazon EC2 或 Amazon ECS。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
    "Sid": "AllowPutRuleIfSourceIsEC2OrECS",
    "Effect": "Allow",
    "Action": "events:PutRule",
    "Resource": "*",
    "Condition": {
    "ForAllValues:StringEquals": {
    "events:source": [
    "aws.ec2",
    "aws.ecs"
    ]
    }
    }
    }
    ]
    }
```

------

下表显示此策略允许或拒绝的一些事件模式的示例。


| 事件模式 | 策略允许 | 
| --- | --- | 
|  <pre>{<br />    "source": [ "aws.ec2" ]<br />}</pre>  |  支持  | 
|  <pre>{<br />    "source": [ "aws.ecs" ]<br />}</pre>  |  是  | 
|  <pre>{<br />    "source": [ "aws.s3" ]<br />}</pre>  |  否  | 
|  <pre>{<br />    "source": [ "aws.ec2", "aws.ecs" ]<br />}</pre>  |  否  | 
|  <pre>{<br />    "detail-type": [ "AWS API Call via CloudTrail" ]<br />}</pre>  |  否  | 

## 示例：确保在事件模式中定义源
<a name="eb-source-defined-events-pattern"></a>

以下策略仅允许用户创建 `EventPatterns` 具有源字段的规则。使用此策略，IAM 用户或角色不能创建 `EventPattern` 不提供特定源的规则。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleIfSourceIsSpecified",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "Null": {
                    "events:source": "false"
                }
            }
        }
    ]
}
```

------

下表显示此策略允许或拒绝的一些事件模式的示例。


| 事件模式 | 策略允许 | 
| --- | --- | 
|  <pre>{<br />    "source": [ "aws.ec2" ],<br />    "detail-type": [ "EC2 Instance State-change Notification" ]<br />}</pre>  |  支持  | 
|  <pre>{<br />    "source": [ "aws.ecs", "aws.ec2" ]<br />}</pre>  |  是  | 
|  <pre>{<br />    "detail-type": [ "EC2 Instance State-change Notification" ]<br />}</pre>  |  否  | 

## 示例：在包含多个源的事件模式中定义允许的源的列表
<a name="eb-allowed-sources-events-pattern"></a>

以下策略允许用户创建 `EventPatterns` 具有多个源的规则。事件模式中的每个源必须是条件中提供的列表的成员。在使用 `ForAllValues` 条件时，请确保定义条件列表中的至少一个项。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleIfSourceIsSpecifiedAndIsEitherS3OrEC2OrBoth",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "ForAllValues:StringEquals": {
                    "events:source": [ "aws.ec2", "aws.s3" ]
                },
                "Null": {
                    "events:source": "false"
                }
            }
        }
    ]
}
```

------

下表显示此策略允许或拒绝的一些事件模式的示例。


| 事件模式 | 策略允许 | 
| --- | --- | 
|  <pre>{<br />    "source": [ "aws.ec2" ]<br />}</pre>  |  支持  | 
|  <pre>{<br />    "source": [ "aws.ec2", "aws.s3" ]<br />}</pre>  |  是  | 
|  <pre>{<br />    "source": [ "aws.ec2", "aws.autoscaling" ]<br />}</pre>  |  否  | 
|  <pre>{<br />    "detail-type": [ "EC2 Instance State-change Notification" ]<br />}</pre>  |  否  | 

## 示例：由 `detail.service` 限制 `PutRule` 访问权限
<a name="eb-limit-rule-by-service"></a>

您可以限制 IAM 用户或角色仅为 `events:details.service` 字段中具有特定值的事件创建规则。的值`events:details.service`不一定是 AWS 服务的名称。

当您处理与安全或滥用行为有关的事件时 AWS Health ，此政策条件非常有用。通过使用此策略条件，您可以限制只有需要查看敏感警报的那些用户才能访问它们。

例如，以下策略允许仅为 `events:details.service` 的值为 `ABUSE` 的事件创建规则。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleEventsWithDetailServiceEC2",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "events:detail.service": "ABUSE"
                }
            }
        }
    ]
}
```

------

## 示例：由 `detail.eventTypeCode` 限制 `PutRule` 访问权限
<a name="eb-limit-rule-by-type-code"></a>

您可以限制 IAM 用户或角色仅为 `events:details.eventTypeCode` 字段中具有特定值的事件创建规则。当您处理与安全或滥用行为有关的事件时 AWS Health ，此政策条件非常有用。通过使用此策略条件，您可以限制只有需要查看敏感警报的那些用户才能访问它们。

 例如，以下策略允许仅为 `events:details.eventTypeCode` 的值为 `AWS_ABUSE_DOS_REPORT` 的事件创建规则。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleEventsWithDetailServiceEC2",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "events:detail.eventTypeCode": "AWS_ABUSE_DOS_REPORT"
                }
            }
        }
    ]
}
```

------

## 示例：确保只允许来自特定`PrincipalId`用户的 API 调用 AWS CloudTrail 的事件
<a name="eb-consume-specific-events"></a>

所有 AWS CloudTrail 事件在事件 PrincipalId 的`detail.userIdentity.principalId`路径中都有发出 API 调用的用户。使用`events:detail.userIdentity.principalId`条件密钥，您可以限制 IAM 用户或角色只能访问来自特定账户 CloudTrail 的事件。

```
    "Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutRuleOnlyForCloudTrailEventsWhereUserIsASpecificIAMUser",
            "Effect": "Allow",
            "Action": "events:PutRule",
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "events:detail-type": [ "AWS API Call via CloudTrail" ],
                    "events:detail.userIdentity.principalId": [ "AIDAJ45Q7YFFAREXAMPLE" ]
                }
            }
        }
    ]
}
```

下表显示此策略允许或拒绝的一些事件模式的示例。


| 事件模式 | 策略允许 | 
| --- | --- | 
|  <pre>{<br />    "detail-type": [ "AWS API Call via CloudTrail" ]<br />}</pre>  |  否  | 
|  <pre>{<br />    "detail-type": [ "AWS API Call via CloudTrail" ],<br />    "detail.userIdentity.principalId": [ "AIDAJ45Q7YFFAREXAMPLE" ]<br />}</pre>  |  是  | 
|  <pre>{<br />    "detail-type": [ "AWS API Call via CloudTrail" ],<br />    "detail.userIdentity.principalId": [ "AROAIDPPEZS35WEXAMPLE:AssumedRoleSessionName" ]<br />}</pre>  |  否  | 

## 示例：限制对目标的访问
<a name="eb-limiting-access-to-targets"></a>

如果 IAM 用户或角色具有 `events:PutTargets` 权限，他们就可以在相同账户下将任何目标添加到他们有权访问的规则。以下策略仅限用户将目标添加到特定规则：账户 `123456789012` 下的 `MyRule`。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutTargetsOnASpecificRule",
            "Effect": "Allow",
            "Action": "events:PutTargets",
            "Resource": "arn:aws:events:us-east-1:123456789012:rule/MyRule"
        }
    ]
}
```

------

为了限制可以添加到规则的目标，请使用 `events:TargetArn` 条件密钥。您可以将目标限制为 Lambda 函数，如以下示例中所示。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowPutTargetsOnASpecificRuleAndOnlyLambdaFunctions",
            "Effect": "Allow",
            "Action": "events:PutTargets",
            "Resource": "arn:aws:events:us-east-1:123456789012:rule/rule-name",
            "Condition": {
            "ForAnyValue:ArnLike": {
                    "events:TargetArn": "arn:aws:lambda:*:*:function:*"
                }
            }
        }
    ]
}
```

------