Lambda event filter dynamodb TTL deletions

0

I'm trying to trigger a Lambda from a DynamodDB stream, but only if the change is not a deletion due to TTL. I know that TTL deletes have 'userIdentity': {'principalId': 'dynamodb.amazonaws.com', 'type': 'Service'} in their events, while other events do not have useridentity at all, so I tried this filter: {"userIdentity": [ { "exists": false } ]}. This passes validation, but all events trigger the Lambda, including TTL deletes.

Any idea how I can filter the TTLs out? The reason I want to do that is I have a phase where no changes to the database should be made, and I'd like to verify that the Lambda is not triggered. Therefore I want to block TTL deletions before the Lambda invocation.

profile picture
질문됨 일 년 전420회 조회
1개 답변
-1

To filter out TTL deletions, you can't directly set up a filter on the event source mapping in Lambda. Instead, you can add a conditional check within your Lambda function to filter out TTL deletions based on the userIdentity field in the event record.

Here's an example of how you can modify your Lambda function to filter out TTL deletions:

import json

def lambda_handler(event, context):
    for record in event['Records']:
        if ('userIdentity' not in record['dynamodb']['NewImage']) or (record['dynamodb']['NewImage']['userIdentity']['principalId'] != 'dynamodb.amazonaws.com'):
            # Process the non-TTL deletion event
            print("Non-TTL event:", json.dumps(record, indent=2))

This code snippet checks each record in the event to see if the userIdentity field is present and if its principalId is equal to 'dynamodb.amazonaws.com'. If the condition is false, it processes the non-TTL event. By doing so, your Lambda function only processes events that are not TTL deletions.

profile picture
전문가
답변함 일 년 전
profile picture
전문가
검토됨 2달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠