![](/img/trans.png)
[英]How to use AWS EventBridge with Python Lambda and Boto?
[英]AWS EventBridge as Lambda destination
我有兩個 Lambda 函數,一個 EventProducer 和一個 EventConsumer。 所需的場景如下:EventProducer 在自定義 AWS EventBridge 總線中添加一個事件,EventConsumer 讀取該事件。
我想使用 Lambda 目的地實現此目的,但 EventBridge 似乎無法正常工作。 我通過顯式調用 AmazonEventBridge::putEvent 來推送我的事件,設法使消費者可以使用事件,但我沒有設法通過返回 output 並發送 output 來做到這一點。
如果我使用 Lambda 或 SQS 作為目標而不是 EventBridge,則代碼有效。 當我使用 AWS CLI 發送消息時,消費者也會讀取它們。
有沒有人有使用 Lambda 目的地從 Lambda function 在 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
如果你這樣寫你的 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.