TL;DR
Input to the "Enrichment" function
The first argument "event" of the Lambda function is passed as an array of payloads from each AWS service.
Output from the "Enrichment" function
return ... is passed to the "target" of the EventBridge Pipes.
Explanation
This section is based on a Lambda function (as will be described later) that receives an event from a source or filter and passes the event to a target without doing anything (pass-through).
def lambda_handler(event, context):
print(event)
return event
export const handler = async (event, context) => {
console.log(event);
return event;
};
Input to the function of "Enrichment"
The first argument "event" of the Lambda function is passed as a payload from the source or filter. The second argument "context" can also be used.
For example, if you set Amazon SQS as the source and put the data into the SQS queue of MessageBody as {"v":1}
, the result of print(event)
(in case of python) will look like this.
[
{
'messageId': '8c8b72a2-85f0-47be-bf9f-81edff5197e9',
'receiptHandle': '.....',
'body': '{"v":1}',
'attributes': {
'ApproximateReceiveCount': '1',
'SentTimestamp': '1672477371472',
'SenderId': 'AID................KM',
'ApproximateFirstReceiveTimestamp': '1672477371473'
},
'messageAttributes': {},
'md5OfMessageAttributes': None,
'md5OfBody': '455c0c10d425837001a72a84239ac362',
'eventSource': 'aws:sqs',
'eventSourceARN': 'arn:aws:sqs:REGION:............:SQS_NAME',
'awsRegion': 'REGION'
}
]
The first level of "event" is an array. Therefore, fetch within a Lambda function can be made to, for example, messageId
as event[0]['messageId']
(in case of python).
def lambda_handler(event, context):
print(event[0]['messageId'])
return event
"event" is an array. Therefore, I thought multiple payloads would be entered. So far, no multiple payloads have been entered.
For example, even if 3 data were queued in SQS at once using send-message-batch
, the Lambda function specified as Enrichment was executed 3 times individually.
Output from the function of "Enrichment"
return ...
is passed to the target of the EventBridge Pipes.
The following code is a Lambda function that passes {"foo": "bar"}
"target".
def lambda_handler(event, context):
return {'foo': 'bar'}
If "empty" is returned, the target will not be executed. Empty means {}
, []
and ""
. The following code is a Lambda function(Python3) that does not execute "target".
def lambda_handler(event, context):
return {}
Implicit body data parsing
One point to note when processing data with Enrichment is Implicit body data parsing.
If Enrichment is not used, Implicit body data parsing is applied and can be referenced in the input transformer on the target side as <$.body.foo>
.
If Enrichment is used, Implicit body data parsing is not applied.
Therefore, when using Enrichment, it is better to design the input transformer to be fully formatted in the Enrichment Lambda function before passing it on to the target, to simplify the input transformer.
My strategy for implementing Enrichment Lambda functions
At first, if enrichment is to be used, it would be better to implement without target input transformers. This is to concentrate the formatting implementation.
Let's consider Slack's chat.postMessage
API . That requires a channel
and text
.
The first approach implements formatting to the API in a function. The second approach is to pass the necessary information to target, and the formatting to the API itself is done in the input transformer.
Approach 1: "Rewriting."
def lambda_handler(event, context):
payload = {
'channel': 'XXX',
'text': event[0]['body']
}
return payload
# => {
# 'channel': 'XXX',
# 'text': "STRING...."
#}
Input transformar in "target" is nothing.
Approach 2: "Adding."
def lambda_handler(event, context):
event[0]['_enrichment'] = {
'slack': {
'channel': 'XXX',
'text': event[0]['body']
}
}
return event[0]
# => {
# (ๅ
ใ
ใฎevent),
# '_enrichment': {
# 'slack': {
# 'channel': 'XXX',
# 'text': 'STRING....'
# }
# }
#}
Input transformar
{
"channel": <$._enrichment.slack.channel>,
"text": <$._enrichment.slack.text>
}
Conclusion
Amazon EventBridge Pipes allow AWS Lambda functions to be specified in the target. Therefore, some may argue that there is no reason for Enrichment to handle Lambda functions with
It appears that there is no need to use a Lambda function in the Enrichment.
Eliminating the implementation of calling external APIs from within the Lambda function would have the advantage of simplifying the Lambda function.
EoT
Top comments (0)