[英]Pulumi: how to create a CloudWatch event rule for a repository
I am trying to capture PutImage
event from a specific ECR repository using Cloudwatch to trigger a Lambda. 我正在尝试使用Cloudwatch从特定的ECR存储库捕获
PutImage
事件,以触发Lambda。
My problem is with eventPattern being typed as 'string': 我的问题是eventPattern被键入为“字符串”:
export const myTestRepo = ECRTemplate('my-test-repo');
export const eventRule = new aws.cloudwatch.EventRule("putimagerule", {
eventPattern: JSON.stringify({
"detail-type": [
"AWS API Call via CloudTrail"
],
"source": ["aws.ecr"],
"detail": {
"eventName": ["PutImage"],
"repositoryName": [myTestRepo.repository.name]
}
}),
});
and a resulting event rule looks like this: 结果的事件规则如下所示:
{
"detail":{
"eventName":[
"PutImage"
],
"repositoryName":[
"Calling [toJSON] on an [Output\u003cT\u003e] is not supported.\n\nTo get the value of an Output as a JSON value or JSON string consider either:\n 1: o.apply(v =\u003e v.toJSON())\n 2: o.apply(v =\u003e JSON.stringify(v))\n\nSee https://pulumi.io/help/outputs for more details.\nThis function may throw in a future version of @pulumi/pulumi."
]
},
"detail-type":[
"AWS API Call via CloudTrail"
],
"source":[
"aws.ecr"
]
}
Object myTestRepo
contains a valid Repository and is not a part of the problem that why it is not included here. 对象
myTestRepo
包含有效的存储库,因此不是此处未包含该对象的问题的一部分。
Q: How to catch PutImage
for a specific repository? 问:如何捕获特定存储库的
PutImage
?
The problem is caused by the type of myTestRepo.repository.name
: it's not a string
but a pulumi.Output<string>
. 该问题是由
myTestRepo.repository.name
的类型引起的:它不是string
而是pulumi.Output<string>
。 Its value is unknown at the time when the program first runs, so you can't use it inside string interpolation. 在程序首次运行时,它的值是未知的,因此您不能在字符串插值中使用它。
Instead, you can use apply
function: 相反,您可以使用
apply
函数:
const eventRule = new aws.cloudwatch.EventRule("putimagerule", {
eventPattern: myTestRepo.repository.name.apply(repositoryName =>
JSON.stringify({
"detail-type": [
"AWS API Call via CloudTrail",
],
"source": ["aws.ecr"],
"detail": {
eventName: ["PutImage"],
repositoryName: [repositoryName],
},
})),
});
You can learn more in the Outputs and Inputs docs. 您可以在“ 输出和输入”文档中了解更多信息。
The issue is with the line "repositoryName": [myTestRepo.repository.name]
问题出在
"repositoryName": [myTestRepo.repository.name]
Try 尝试
export const myTestRepo = ECRTemplate('my-test-repo');
export const eventRule = new aws.cloudwatch.EventRule("putimagerule", {
eventPattern: {
"detail-type": [
"AWS API Call via CloudTrail"
],
"source": ["aws.ecr"],
"detail": {
"eventName": ["PutImage"],
"repositoryName": [myTestRepo.repository.name.apply(v => v.toJSON()]
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.