【问题标题】:Pulumi: how to create a CloudWatch event rule for a repositoryPulumi:如何为存储库创建 CloudWatch 事件规则
【发布时间】:2019-10-08 19:51:41
【问题描述】:

我正在尝试使用 Cloudwatch 从特定 ECR 存储库捕获 PutImage 事件以触发 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

【问题讨论】:

    标签: amazon-web-services amazon-cloudwatch aws-ecr pulumi


    【解决方案1】:

    问题是由myTestRepo.repository.name 的类型引起的:它不是string,而是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],
              },
        })),
    });
    

    您可以在Outputs and Inputs 文档中了解更多信息。

    【讨论】:

      【解决方案2】:

      问题在于"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-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-30
        • 1970-01-01
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多