创建云监视事件规则后,我尝试向其添加目标,但无法添加输入转换。以前 add 目标允许输入转换的道具,但现在不再允许了。
codeBuildRule.addTarget(new SnsTopic(props.topic));
Run Code Online (Sandbox Code Playgroud)
aws cdk 页面提供了这个解决方案,但我不完全明白它说的是什么
您可以使用 eventRule.addTarget(target[, input]) 添加额外的目标,以及可选的输入转换器。例如,我们可以添加一个 SNS 主题目标,它为提交设置了人类可读的消息格式。
您应该指定message道具并使用RuleTargetInput 静态方法。其中一些方法可以使用由 返回的字符串EventField.fromPath():
// From a path
codeBuildRule.addTarget(new SnsTopic(props.topic, {
message: events.RuleTargetInput.fromEventPath('$.detail')
}));
// Custom object
codeBuildRule.addTarget(new SnsTopic(props.topic, {
message: RuleTargetInput.fromObject({
foo: EventField.fromPath('$.detail.bar')
})
}));
Run Code Online (Sandbox Code Playgroud)
我在尝试在 CDK 中实现本教程时遇到了同样的问题:教程:设置 CloudWatch Events 规则以接收管道状态更改的电子邮件通知
我发现这也很有帮助:Detect and React tochanges in pipeline state with Amazon CloudWatch Events
注意:我无法使用 Pipeline 的类方法 onStateChange() 使其工作。
我最终写了一条规则:
const topic = new Topic(this, 'topic', {topicName: 'codepipeline-notes-failure',
});
const description = `Generated by the CDK for stack: ${this.stackName}`;
new Rule(this, 'failed', {
description: description,
eventPattern: {
detail: {state: ['FAILED'], pipeline: ['notes']},
detailType: ['CodePipeline Pipeline Execution State Change'],
source: ['aws.codepipeline'],
},
targets: [
new SnsTopic(topic, {
message: RuleTargetInput.fromText(
`The Pipeline '${EventField.fromPath('$.detail.pipeline')}' has ${EventField.fromPath(
'$.detail.state',
)}`,
),
}),
],
});
Run Code Online (Sandbox Code Playgroud)
实施后,如果您导航到 Amazon EventBridge -> 规则,然后选择规则,然后选择目标,然后单击查看详细信息,您将看到带有输入转换器和输入模板的目标详细信息。
输入转换器:{"InputPathsMap":{"detail-pipeline":"$.detail.pipeline","detail-state":"$.detail.state"},"InputTemplate":"\"管道 '<detail -pipeline>' 有 <详细状态>\""}
| 归档时间: |
|
| 查看次数: |
2377 次 |
| 最近记录: |