【问题标题】:Create a Event Bridge stack with Amazon CDK使用 Amazon CDK 创建事件桥堆栈
【发布时间】:2020-12-24 03:01:11
【问题描述】:

我正在使用 AWS CDK 创建事件桥堆栈。 总之,这个想法是,CDK 项目将负责创建事件总线、规则和目标。

在我的场景中,目标是已经存在的 Lambda 函数。

我正在关注这个文档:

https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.Function.html#properties

我认为下面代码的问题是我没有指向存在 lambda 的存储桶,但我无法指向,因为我想直接指向已部署的 lambda 函数。

用CDK可以实现吗?

const cdk = require('@aws-cdk/core');
const busCdk = require('@aws-cdk/aws-events')
const ruleCdk = require('@aws-cdk/aws-events')
const targets = require('@aws-cdk/aws-events-targets')
const functionCdk = require('@aws-cdk/aws-lambda')

class PocCdkStack extends cdk.Stack {
  /**
   *
   * @param {cdk.Construct} scope
   * @param {string} id
   * @param {cdk.StackProps=} props
   */
  constructor(scope, id, props) {
    super(scope, id, props);

    const bus = new busCdk.EventBus(this,'ProfileEventBus',{
      eventBusName: "PocProfileBus"
    })

    const rule = new ruleCdk.Rule(this, "newRule", {
      description: "description",
      eventPattern: {
        source: ["order_service"]
      },
      eventBus: bus
    });

    const func = new functionCdk.Function(this, 'testLambda', {
      functionArn: 'arn:aws:lambda:us-east-1:111111111111:function:ccna-poc-eventbus-project-test-triggerByEventBusTwo'
    });

    rule.addTarget(new targets.LambdaFunction(func));
  }
}

module.exports = { PocCdkStack }

谢谢大家,

【问题讨论】:

    标签: javascript aws-lambda aws-cdk


    【解决方案1】:

    我相信我已经找到了解决办法。

    const func = functionCdk.Function.fromFunctionArn(this, 'testLambda', 'arn:aws:lambda:us-east-1:11111:function:ccna-poc-eventbus-project-test-triggerByEventBusTwo')

    有一个静态方法可以完成这项工作。

    【讨论】:

    • 它对你有用吗?对我来说,相同的代码创建了事件总线和规则,但它并没有真正将规则连接到 lambda 函数(我没有看到该函数的相应触发器)。
    猜你喜欢
    • 2023-01-12
    • 2021-02-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2019-11-29
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多