Joh*_*ohn 6 amazon-web-services aws-cloudformation amazon-cloudwatch aws-cdk
如何使用 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();
Run Code Online (Sandbox Code Playgroud)
如何指定另一个账户的EventBus作为目标?语法是什么 - 是 ARN 还是什么?
要在 CloudFormation 中将 CW 事件从 Acc1 中继到 Acc2,需要三件事:
AWS::Events::EventBusPolicy允许 Acc1 提交事件。例如:
MyEventBusPolicy:
Type: AWS::Events::EventBusPolicy
Properties:
Action: events:PutEvents
EventBusName: default
Principal: 2234322123 # Account1 Id
StatementId: AcceptEventsFromAcc1
Run Code Online (Sandbox Code Playgroud)
允许 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"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
其中AccountId和RegionId是 Acc2 值,而不是 Acc1。
它将使用步骤 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
Run Code Online (Sandbox Code Playgroud)
其中AccountId和RegionId是 Acc2 值,而不是 Acc1。
| 归档时间: |
|
| 查看次数: |
4031 次 |
| 最近记录: |