I am using dynamodb trigger for my lambda.So i am considering two conditions, whenever my table 1 gets updated lambda should be triggered and based on condition expression table 2 should be updated just like the changes i have applied in table 2. Since i am going to use boto 3 resource need sample logic regrading the same. I am new to handling events with dynamodb triggers. Any help will be highly appreciated.
2 Answers
Generally you would setup event source mapping between table 1 and your lambda function.
The lambda function would process the records from table 1, e.g. filter them out based on some condition, and then the remaining records would be written to the table 2.
For writing to table 2, boto3 provides interface to dynamodb.
2 Comments
NARESH GOVINDARAJ
Do you have any boto3 logic for reading the stream data for insert operation for table 1
Marcin
You don't have to read it yourself. That's why you use event source mapping. It will invoke your function automatically and pass the records from the stream. I recommend reading more on how event source mapping works.
Table-1 ~~~~~~~~> AWS Lambda --------->Table2
Event received by AWS Lambda will look something like this. What you need to do is based on event name process the values present and store it in Table-2.
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb', region_name='eu-west-1')
table2 = dynamodb.Table('Table-2')
# Read the records in the event
for record in event['Records']:
# process record and prepare required object for Table-2
table_2_obj = process_record(record)
put_item_response = table2.put_item(table_2_obj)
# Verify the response if required
print(put_item_response)