0

我正在尝试PutImage使用 Cloudwatch 从特定 ECR 存储库捕获事件以触发 Lambda。

我的问题是 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]
        }
    }),
});

结果事件规则如下所示:

{
   "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"
   ]
}

对象myTestRepo包含一个有效的存储库,并且不是问题的一部分,为什么它不包含在此处。

问:如何捕获PutImage特定的存储库?

4

2 回答 2

3

问题是由 的类型引起的myTestRepo.repository.name:它不是 astring而是 a pulumi.Output<string>。它的值在程序第一次运行时是未知的,所以你不能在字符串插值中使用它。

相反,您可以使用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],
          },
    })),
});

您可以在输出和输入文档中了解更多信息。

于 2019-10-08T11:51:41.500 回答
0

问题在于线路"repositoryName": [myTestRepo.repository.name]

尝试

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()]
        }
    });
于 2019-10-08T11:24:02.647 回答