【问题标题】:How do I specify another AWS account's Event Bus as a target of an EventBridge rule using CloudFormation or CDK?如何使用 CloudFormation 或 CDK 将另一个 AWS 账户的事件总线指定为 EventBridge 规则的目标?
【发布时间】:2021-01-12 02:56:49
【问题描述】:

如何使用 CloudFormation 或 CDK 将另一个 AWS 账户的事件总线指定为 CloudWatch 规则的目标?

这是一个使用 CDK 的示例规则,我尝试将 CodeDeploy 事件发送到另一个帐户:

Rule codedeployCreateDeploymentEventRule = Rule.Builder.create(this, "CodedeployCreateDeploymentEventRule")
                                                   .description("CloudWatch event rule covering CodeDeploy CreateDeployment notifications.")
                                                   .ruleName("MyRule")
                                                   .enabled(true)
                                                   .targets(List.of(...something here...))
                                                   .eventPattern(EventPattern.builder()
                                                                             .source(List.of("aws.codedeploy"))
                                                                             .detail(Map.of("eventName", List.of("CreateDeployment")))
                                                                             .build())
                                                   .build();

如何将另一个帐户的 EventBus 指定为目标?语法是什么 - 它是 ARN 还是什么?

【问题讨论】:

  • 这个问题是怎么解决的?仍然存在?

标签: amazon-web-services amazon-cloudformation amazon-cloudwatch aws-cdk


【解决方案1】:

要在 CloudFormation 中将 CW 事件从 Acc1 中继到 Acc2,需要三件事:

1。 Acc2 - EventBusPolicy

AWS::Events::EventBusPolicy 允许 Acc1 提交事件。例如:

  MyEventBusPolicy:
    Type: AWS::Events::EventBusPolicy
    Properties: 
      Action: events:PutEvents
      EventBusName: default
      Principal: 2234322123 # Account1 Id
      StatementId: AcceptEventsFromAcc1

2。 Acc1 - CW 的 Iam 角色

允许 Acc1 中的 CW Events 将事件发布到 Acc 2 的 IAM 角色。示例:

  MyCWEventsRole:
    Type: AWS::IAM::Role
    Properties: 
      AssumeRolePolicyDocument:                   
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service: [events.amazonaws.com]
            Action: ["sts:AssumeRole"]
      Description: Role for CW event to be able to publish events to acc2
      Policies: 
        - PolicyName: MyEventPolicy
          PolicyDocument: !Sub |
            {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Action": [
                            "events:PutEvents"
                        ],
                        "Resource": [
                            "arn:aws:events:${RegionId}:${AccountId}:event-bus/default"
                         ]
                    }
                ]
            }

其中AccountIdRegionId 是Acc2 值,而不是Acc1。

3。 Acc1 - CW 事件规则将事件依赖于 Acc2 的总线

它将使用步骤 2 中的 IAM 角色。例如,依赖 CodeCommits 事件(我之前设置过,所以我知道它有效):

  MyRule:
    Type: AWS::Events::Rule
    Properties: 
      Description: Monitor master branch of our repo and rely to Acc2
      EventPattern: !Sub |
            {
              "source": [
                "aws.codecommit"
              ],
              "detail-type": [
                "CodeCommit Repository State Change"
              ],
              "resources": [
                "${GitRepoArn}"
              ],
              "detail": {
                "event": [
                  "referenceCreated",
                  "referenceUpdated"
                ],
                "referenceType": [
                  "branch"
                ],
                "referenceName": [
                  "master"
                ]
               }
             }  
      State: ENABLED
      Targets: 
        - Arn: !Sub "arn:aws:events:${RegionId}:${AccountId}:event-bus/default"
          Id: MyEventToAcc2
          RoleArn: !GetAtt MyCWEventsRole.Arn

其中AccountIdRegionId 是Acc2 值,而不是Acc1。

【讨论】:

  • 很好的答案,网上没有太多关于如何做到这一点的例子,我有一个问题.. 您的规则中提供的事件模式,这与您在 Acc2 中部署的规则模式相同吗?有没有办法将 Acc1 上的 eventbridge 转发到 Acc2 上的 eventbridge 并查看它是否与那里部署的任何规则匹配?
  • @AnonymousAlias 谢谢。我认为你可以做到这一点,但我不知道现在如何。如果可以的话,你可以为此提出新问题吗?
  • 我已经弄明白了,对于其他想知道事件模式的人来说,这只是 {"account": ["account1-id"]} 奇怪的是,您需要输入帐户 ID,但确实如此目前不接受通配符,我们需要在这里放一些东西。
猜你喜欢
  • 2021-11-25
  • 2021-04-18
  • 2018-08-09
  • 2020-11-24
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
相关资源
最近更新 更多