我如何设置 2 个 AWS lambda 函数,一个在 eventBridge 上触发事件,另一个对此做出反应?

How can I set up 2 AWS lambda functions, with one firing an event on eventBridge and the other reacting to it?

我正在使用无服务器框架来尝试测试 EventBridge。

文档有点少,但为了我的测试,我想创建两个 lambda 函数:第一个发布一个事件,第二个使用它。

这是我的 YAML:

service: events
frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: '20201221'

functions:
  vehicle:
    handler: handler.vehicle
    events:
      - httpApi:
          path: /vehicle
          method: '*'
  bundle:
    handler: handler.bundle
    events:
      - httpApi:
          path: /bundle
          method: '*'
      - eventBridge:
          eventBus: vehicle-bus 
          pattern:
            source:
              - aos.vehicle.upload
          detail-type:
            - VehicleUpload

和我的handler.js

"use strict";
const AWS = require('aws-sdk');

module.exports.vehicle = async (event) => {
  const eventBridge = new AWS.EventBridge({ region: 'us-east-1' });

  const vrm = 'WR17MMN'
  return eventBridge.putEvents({
    Entries: [
      {
        EventBusName: 'veihcle-bus',
        Source: 'aos.vehicle.upload',
        DetailType: 'VehicleUpload',
        Detail: `{ "Registration": "${vrm}" }`,
      },
    ]
  }).promise()
};

module.exports.bundle = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: "BUNDLE",
        input: event,
        aos: "First test OK",
      },
      null,
      2
    ),
  };
};

(我意识到我不能只 return 来自 Lambda,但它也需要是一个端点。如果我将 bundle 的函数体设为空我仍然收到服务器错误。

我错过了什么?

所以你需要这个最小的设置:

org: myOrg
app: my-events
service: event-bridge-serverless

provider:
  name: aws
  runtime: nodejs10.x
  region: eu-west-1
  lambdaHashingVersion: 20201221
  environment:
    DYNAMODB_TABLE: ${self:service}-dev
  eventBridge:
    useCloudFormation: true

iamRoleStatements:
  - Effect: "Allow"
    Action:
        - "events:PutEvents"
    Resource: "*"


functions:
  asset:
     handler: handler.asset
    events:
      - eventBridge:
        eventBus: my-events 
        pattern:
           source:
             - my.event