Skip to content

Instantly share code, notes, and snippets.

@atze234
Created June 2, 2023 13:44
Show Gist options
  • Save atze234/60dbef2991e08aba93b875c73578cf41 to your computer and use it in GitHub Desktop.
Save atze234/60dbef2991e08aba93b875c73578cf41 to your computer and use it in GitHub Desktop.
import boto3
import time
def message_deduplication(message):
alerthash = hashlib.md5(message.encode())
ttl = str(int(time.time()+600))
dynamodbclient = boto3.client('dynamodb')
response = dynamodbclient.get_item(
Key={
'alerthash': {
'S': alerthash.hexdigest(),
}
},
TableName='deduplicationtab',
)
try:
found = response['Item']
if int(found['ttl']['N']) > int(time.time()) :
print("Found message hash in DB")
return True
except KeyError:
pass
response = dynamodbclient.put_item(
Item={
'alerthash': {
'S': alerthash.hexdigest(),
},
'ttl': {
'N': ttl,
}
},
TableName='deduplicationtab',
)
return False
def lambda_handler(event, context):
#[...]
if message_deduplication(message):
return
#[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment