创建云监视事件规则后,我尝试向其添加目标,但无法添加输入转换。以前,添加目标具有允许输入转换的道具,但现在不再有。
codeBuildRule.addTarget(new SnsTopic(props.topic));
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')
})
}));
我在尝试在 CDK 中实现本教程时遇到了同样的问题:教程:设置 CloudWatch Events 规则以接收管道状态更改的电子邮件通知
我发现这也很有帮助:使用 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',
)}`,
),
}),
],
});
实施后,如果您导航到 Amazon EventBridge -> 规则,然后选择规则,然后选择目标,然后单击查看详细信息,您将看到带有输入转换器和输入模板的目标详细信息。
输入变压器: {"InputPathsMap":{"detail-pipeline":"$.detail.pipeline","detail-state":"$.detail.state"},"InputTemplate":"\" 管道“
”有 \""}
这适用于 CDK Python。 CodeBuild 到 SNS 通知。
sns_topic = sns.Topic(...)
codebuild_project = codebuild.Project(...)
sns_topic.grant_publish(codebuild_project)
codebuild_project.on_build_failed(
f'rule-on-failed',
target=events_targets.SnsTopic(
sns_topic,
message=events.RuleTargetInput.from_multiline_text(
f"""
Name: {events.EventField.from_path('$.detail.project-name')}
State: {events.EventField.from_path('$.detail.build-status')}
Build: {events.EventField.from_path('$.detail.build-id')}
Account: {events.EventField.from_path('$.account')}
"""
)
)
)
感谢 @pruthvi-raj 对上述答案的评论
我遇到了同样的问题,试图找出在哪里定义
inputPath
和 inputTemplate
。只有一些 CDK 抽象可用于一个输入字段(基于目标对象的 message
、event
或 input
)。事实证明,抽象已经包含了创建 inputPath
和 inputTemplate
所需的一切,这只是文档不足的问题。
这个抽象非常简洁,因为当您在 CDK 目标对象中指定
inputPath
(或 inputTemplate
、message
,...)时,它会自动为您创建 input
和 event
。对于您使用的每个唯一的 EventField.fromPath()
,它会为您创建一个 inputPath
键值对,并在 inputTemplate
中使用该键。
对于我自己来说,我通过模仿代码中的输入变压器使其变得更清楚:
import * as events from 'aws-cdk-lib/aws-events'
import * as targets from 'aws-cdk-lib/aws-events-targets'
...
const eventInputMapping = {
account: events.EventField.account,
region: events.EventField.region,
alarmName: events.EventField.fromPath('$.detail.alarmName'),
description: events.EventField.fromPath('$.detail.configuration.description'),
startTime: events.EventField.fromPath('$.detail.state.timestamp'),
state: events.EventField.fromPath('$.detail.state.value'),
metric: events.EventField.fromPath('$.detail.configuration.metrics[0].metricStat.metric.name'),
clusterName: events.EventField.fromPath('$.detail.configuration.metrics[0].metricStat.metric.dimensions.ClusterName'),
serviceName: events.EventField.fromPath('$.detail.configuration.metrics[0].metricStat.metric.dimensions.ServiceName'),
}
const eventInputTemplate = {
title: `Service ${eventInputMapping.serviceName} went into ${eventInputMapping.state} state for ${eventInputMapping.metric} ${eventInputMapping.description}.`,
text: `The ECS scaling alarm ${eventInputMapping.alarmName} got triggered at ${eventInputMapping.startTime}` +
` This impacts the scaling of ECS service ${eventInputMapping.serviceName} in ECS cluster ${eventInputMapping.clusterName}. `,
source_type_name: 'amazon ecs',
tags: [
`account-id:${eventInputMapping.account}`,
`region:${eventInputMapping.region}`,
`servicename:${eventInputMapping.serviceName}`,
`clustername:${eventInputMapping.clusterName}`,
],
}
new events.Rule(this, 'DefaultEcsMetricsScalingAlarms', {
...
targets: [new targets.ApiDestination(eventsDestination, {
event: events.RuleTargetInput.fromObject(eventInputTemplate)
}),
],
})
这将导致以下结果
inputPath
:
{
"account": "$.account",
"detail-alarmName": "$.detail.alarmName",
"detail-configuration-description": "$.detail.configuration.description",
"detail-configuration-metrics-0--metricStat-metric-dimensions-ClusterName": "$.detail.configuration.metrics[0].metricStat.metric.dimensions.ClusterName",
"detail-configuration-metrics-0--metricStat-metric-dimensions-ServiceName": "$.detail.configuration.metrics[0].metricStat.metric.dimensions.ServiceName",
"detail-configuration-metrics-0--metricStat-metric-name": "$.detail.configuration.metrics[0].metricStat.metric.name",
"detail-state-timestamp": "$.detail.state.timestamp",
"detail-state-value": "$.detail.state.value",
"region": "$.region"
}
和
inputTemplate
{
"title": "Service <detail-configuration-metrics-0--metricStat-metric-dimensions-ServiceName> went into <detail-state-value> state for <detail-configuration-metrics-0--metricStat-metric-name> <detail-configuration-description>.",
"text": "The ECS scaling alarm <detail-alarmName> got triggered at <detail-state-timestamp> This impacts the scaling of ECS service <detail-configuration-metrics-0--metricStat-metric-dimensions-ServiceName> in ECS cluster <detail-configuration-metrics-0--metricStat-metric-dimensions-ClusterName>. ",
"source_type_name": "amazon ecs",
"tags": [
"account-id:<account>",
"region:<region>",
"service:<detail-configuration-metrics-0--metricStat-metric-dimensions-ServiceName>",
"cluster:<detail-configuration-metrics-0--metricStat-metric-dimensions-ClusterName>"
]
}