

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

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

若要授與許可，請使用 IAM 政策語言指定政策生效時間的條件。例如，您可以在特定日期之後套用政策。

政策中的條件由鍵值對所組成。條件索引鍵名稱不區分大小寫。

若您在單一條件中指定多個條件和索引鍵，則 EventBridge 必須符合所有條件和索引鍵，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* 是事件的 **detail-type** 欄位的文字字串，例如 `"AWS API Call via CloudTrail"` 和 `"EC2 Instance State-change Notification"`。  |  Detail Type, Null  | 
|  events: detail.eventTypeCode  |  `"events:detail.eventTypeCode":"eventTypeCode"` 針對 *eventTypeCode*，使用 **detail.eventTypeCode **欄位的文字字串，例如 `"AWS_ABUSE_DOS_REPORT"`。  |  eventTypeCode，Null  | 
|  events: detail.service  |  `"events:detail.service":"service"` 針對*服務*，使用事件的 **detail.service** 欄位的文字字串，例如 `"ABUSE"`。  |  服務，Null  | 
|  events: detail.userIdentity.principalId  |  `"events:detail.userIdentity.principalId":"principal-id"` 針對 *principal-id*，使用 detail-type `"AWS API Call via CloudTrail"` 事件的 **detail.userIdentity.principalId** 欄位的文字字串，例如 `"AROAIDPPEZS35WEXAMPLE:AssumedRoleSessionName."`。  |  Principal Id，Null  | 
|  events：eventBusInvocation  |  `"events:eventBusInvocation":"boolean"` 針對*布林值*，當規則將事件傳送至另一個帳戶中事件匯流排的目標時，請使用 true。使用 `PutEvents` API 呼叫時使用 false。  |  eventBusInvocation, Null  | 
|  events:ManagedBy  |   AWS 服務內部使用。對於 AWS 服務代表您建立的規則，值是建立規則之服務的主體名稱。  |  不適用於客戶政策。  | 
|  events:source  |  `"events:source":"source "` 使用*來源*作為事件的來源欄位的文字字串，例如 `"aws.ec2"` 和 `"aws.s3"`。若要查看更多可能的*來源*值，請參閱 [來自 AWS 服務的事件](eb-events.md#eb-service-event) 中的範例事件。  |  Source，Null  | 
|  events:TargetArn  |  `"events:TargetArn":"target-arn "` 針對 *target-arn*，請使用規則的目標的 ARN，例如 `"arn:aws:lambda:*:*:function:*"`。  |  ArrayOfARN, Null  | 

如需 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)
+ [範例：限制 `PutRule` 存取 `detail.service`](#eb-limit-rule-by-service)
+ [範例：限制 `PutRule` 存取 `detail.eventTypeCode`](#eb-limit-rule-by-type-code)
+ [範例：確保僅允許來自特定 的 API `PrincipalId` 呼叫 AWS CloudTrail 事件](#eb-consume-specific-events)
+ [範例：限制存取目標](#eb-limiting-access-to-targets)

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

EventBridge 管道不支援任何其他 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 只有在所建立規則的事件模式符合 EC2 事件時，才允許 `PutRule`。

**政策 A：允許所有事件**

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

****  

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

------

**政策 B：允許來自 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 Source)  | 
|  <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>  |  否  | 

## 範例：限制 `PutRule` 存取 `detail.service`
<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"
                }
            }
        }
    ]
}
```

------

## 範例：限制 `PutRule` 存取 `detail.eventTypeCode`
<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"
                }
            }
        }
    ]
}
```

------

## 範例：確保僅允許來自特定 的 API `PrincipalId` 呼叫 AWS CloudTrail 事件
<a name="eb-consume-specific-events"></a>

所有 AWS CloudTrail 事件都有在事件`detail.userIdentity.principalId`路徑中發出 API 呼叫之使用者的 PrincipalId。利用 `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:*"
                }
            }
        }
    ]
}
```

------