amazon-web-services - AWS EventBridge 作为 Lambda 目标
问题描述
我有两个 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属性。这应该与您的发件人正在使用的 SOURCE 的名称相匹配。如果您使用它来区分不同的事件类型,“详细”属性也应该是“详细类型” : https ://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html
如果您像这样(全部在 Yaml 中)编写 Events -config 会怎样?
Events:
EventBridgeEvent:
Type: EventBridgeRule
Properties:
EventBusName: !GetAtt EventBus.Name
Pattern:
source:
- "name.of.the.sender"
detail-type:
- "MyType"
推荐阅读
- android - 广播接收器无法正常工作并出现错误
- r - 1000 个列表中所有元素的快速平均值:返回 1 个值
- javascript - 复制 JS 功能在某些文件上无法运行,而在某些文件上完美运行
- sql-server - 如何查看 SQL Server 代理作业历史日志条目中的所有详细信息?
- amazon-web-services - Sagemaker 脚本处理器
- multithreading - 如何将通用 T 发送到另一个线程?
- python - Python phonenumbers 模块的 region_code_for_number 不适用于少数数字
- r - 如何在 R 中使用参数作为“dplyr”的过滤条件?
- html - 当我在头上添加脚本时内容没有出现,为什么?
- android - Firebase 规则:如何在节点的所有子节点中搜索值?