简体   繁体   English

Pulumi:如何为存储库创建CloudWatch事件规则

[英]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.

相关问题 如何在AWS CloudWatch规则中创建自定义事件模式? - How to create a custom event pattern in aws cloudwatch rule? 通过 terraform 创建用于任务状态更改的 Cloudwatch 事件规则 - Create Cloudwatch event rule for task state change via terraform 使用 Terraform 中的不同输入为 lambda function 创建一个 cloudwatch 事件规则目标 - Create a cloudwatch event rule target to a lambda function with different inputs in Terraform 为 S3 前缀创建 AWS CloudWatch 事件规则 - Create AWS CloudWatch Event rule for S3 prefix 如何将数据从 CloudWatch 事件规则传递到 Lambda? - How to pass data from CloudWatch event rule to Lambda? 如何设置 AWS CloudWatch 事件规则以触发多个 Step Functions - How to setup an AWS CloudWatch event rule to trigger on multiple Step Functions 如何使用 cloudformation 模板创建 cloudwatch 事件? - How to create cloudwatch event using cloudformation template? AWS ECR事件触发的Cloudwatch事件规则 - Cloudwatch event rule triggered by AWS ECR event 我可以从该 function 中为 AWS lambda function 创建/删除 cloudwatch 事件规则吗? - Can I create/delete a cloudwatch event rule for an AWS lambda function from within that function? 日志事件触发的 AWS CloudWatch 规则 - AWS CloudWatch Rule triggered by Log Event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM