amazon-web-services - AWS EventBridge 作为 Lambda 目的地

标签 amazon-web-services aws-lambda aws-event-bridge

我有两个 Lambda 函数,一个 EventProducer 和一个 EventConsumer。所需场景如下:EventProducer 在自定义 AWS EventBridge 总线中添加一个事件,EventConsumer 读取该事件。

我想使用 Lambda 目的地实现此目的,但 EventBridge 似乎不起作用。 我通过显式调用 AmazonEventBridge::putEvent 来推送我的事件,设法使消费者可以使用事件,但我还没有通过返回输出和发送输出来做到这一点。

如果我使用 Lambda 或 SQS 作为目标,而不是 EventBridge,代码就可以工作。当我使用 AWS CLI 发送消息时,消费者也会读取消息。

有没有人有使用 Lambda 目标从 Lambda 函数在 EventBridge 中推送事件的工作示例?

我的代码如下:

Handler code:

@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        DataciteDoiRequest sentDirectly = newDataciteDoiRequest();
        logger.info(lOG_HANDLER_HAS_RUN);
        putEventDirectlyToEventBridge(sentDirectly); 

        DataciteDoiRequest sentThroughLambdaDestination =
            sentDirectly.copy().withPublicationId(URI.create("https://localhost/fromOutputStream")).build();
        writeOutput(sentThroughLambdaDestination, output);
    }

    private <I> void writeOutput(I event, OutputStream outputStream)
        throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream))) {
            String responseJson = Optional.ofNullable(objectMapper.writeValueAsString(event))
                .map(StringUtils::replaceWhiteSpacesWithSpace)
                .map(StringUtils::removeMultipleWhiteSpaces)
                .orElseThrow();
            logger.info(responseJson);
            writer.write(responseJson);
        }
    }

    private void putEventDirectlyToEventBridge(DataciteDoiRequest dataciteDoiRequest) {
        PutEventsRequestEntry putEventsRequestEntry = new PutEventsRequestEntry()
            .withDetail(dataciteDoiRequest.toString())
            .withEventBusName(environment.readEnv(EVENT_BUS_ENV_VAR))
            .withSource(SOURCE)
            .withDetailType(dataciteDoiRequest.getType());

        PutEventsRequest putEventsRequest = new PutEventsRequest().withEntries(putEventsRequestEntry);
        eventBridgeClient.putEvents(putEventsRequest);
    }


CloudFormation template:

 EventConsumer:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dynamo-event-to-datacite-request
      Handler: handlers.EventConsumer::handleRequest
      Runtime: java11
      MemorySize: 1400
      Role: !GetAtt LambdaRole.Arn
      Environment:
        Variables:
          EVENT_BUS: !GetAtt EventBus.Name
          AWC_ACCOUNT_ID: !Ref AWS::AccountId
      Events:
        EventBridgeEvent:
          Type: EventBridgeRule
          Properties:
            EventBusName: !GetAtt EventBus.Name
            Pattern: { "detail": { "type": [ "MyType" ] } }
  EventProducer:
    DependsOn:
      - EventBus
      - FailQueue
      - EventConsumer
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dynamo-event-to-datacite-request
      Handler: handlers.EventProducer::handleRequest
      Runtime: java11
      MemorySize: 1400
      Role: !GetAtt LambdaRole.Arn
      EventInvokeConfig:
        DestinationConfig:
          OnSuccess:
            Type: EventBridge
            Destination: !GetAtt EventBus.Arn
          OnFailure:
            Type: SQS
            Destination: !GetAtt FailQueue.Arn

      Environment:
        Variables:
          EVENT_BUS: !GetAtt EventBus.Name
  EventBus:
    Type: AWS::Events::EventBus
    Properties:
      Name: orestis-event-bus
  FailQueue:
    Type: AWS::SQS::Queue
    Properties:
      MaximumMessageSize: 262144
      QueueName: orestis-failure-queue


最佳答案

您的 EventConsumer Events -config 对我来说看起来不正确 EventBridgeRule。 Pattern 缺少 source -property。这应该与您的发件人使用的 SOURCE 的名称相匹配。此外,"detail" 属性应该是 "detail-type" 如果您使用它来区分不同的事件类型:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html

如果你这样写你的事件配置(全部在 Yaml 中)会怎么样?

Events:
  EventBridgeEvent:
    Type: EventBridgeRule
    Properties:
      EventBusName: !GetAtt EventBus.Name
      Pattern:
       source:
         - "name.of.the.sender"
       detail-type:
         - "MyType"

关于amazon-web-services - AWS EventBridge 作为 Lambda 目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64267703/

相关文章:

amazon-web-services - AWS EventBridge 规则中是否有通配符?

amazon-web-services - 有条件地在 AWS sam 模板文件中启用事件计划

amazon-web-services - 如何在调用并行 Lambda 函数的 AWS Step Function 上禁用(或重定向)日志记录

amazon-web-services - 如何从 AWS API 网关获取端点/URI?

amazon-web-services - 在权限策略中将 AWS Lambda 设置为委托(delegate)人

amazon-web-services - 尝试在另一个 AWS 帐户的 S3 存储桶中备份 Codecommit 存储库

amazon-web-services - 让 Cloudwatch 将 CreateLogGroup 消息发送到 EventBridge

java - 键值存储作为主数据库

amazon-web-services - 按 VPC ID 过滤 LoadBalancer

amazon-web-services - 如何将 AWS Lambda 转换回 CloudFormation 模板