[英]AWS EventBridge as Lambda destination
I have two Lambda functions, an EventProducer and an EventConsumer.我有两个 Lambda 函数,一个 EventProducer 和一个 EventConsumer。 The desired scenario is the following: The EventProducer adds an event in a custom AWS EventBridge bus and the EventConsumer reads the event.
所需的场景如下:EventProducer 在自定义 AWS EventBridge 总线中添加一个事件,EventConsumer 读取该事件。
I want to achieve this using the Lambda destinations but EventBridge does no seem to be working.我想使用 Lambda 目的地实现此目的,但 EventBridge 似乎无法正常工作。 I have managed to make an event available to the Consumer by pushing my an event by explicitly calling `AmazonEventBridge::putEvent`` but I have not managed to do so by returning an output and sending the output.
我通过显式调用 AmazonEventBridge::putEvent 来推送我的事件,设法使消费者可以使用事件,但我没有设法通过返回 output 并发送 output 来做到这一点。
The code works if instead of EventBridge I use Lambda or SQS as a destination.如果我使用 Lambda 或 SQS 作为目标而不是 EventBridge,则代码有效。 The Consumer also reads messages when I send them using the AWS CLI.
当我使用 AWS CLI 发送消息时,消费者也会读取它们。
Does anyone have a working example of pushing an event in EventBridge from a Lambda function using Lambda destinations?有没有人有使用 Lambda 目的地从 Lambda function 在 EventBridge 中推送事件的工作示例?
My code is the following:我的代码如下:
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
Your EventConsumer Events -config doesn't look correct EventBridgeRule to me.您的 EventConsumer Events -config 对我来说看起来不正确 EventBridgeRule 。 The Pattern is missing the source -property.
Pattern缺少source -property。 This should match the name of the SOURCE that your sender is using.
这应该与您的发件人使用的 SOURCE 的名称相匹配。 Also the "detail" property should be "detail-type" if you use it to differentiate different event types: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html
如果您使用它来区分不同的事件类型, “detail”属性也应该是“detail-type” : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html
What if you write your Events -config like this (all in Yaml)?如果你这样写你的 Events -config(全部在 Yaml 中)怎么办?
Events:
EventBridgeEvent:
Type: EventBridgeRule
Properties:
EventBusName: !GetAtt EventBus.Name
Pattern:
source:
- "name.of.the.sender"
detail-type:
- "MyType"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.