Before Amazon SNS FIFO, developers had to design receivers to be idempotent. In some cases, where the event cannot be idempotent, this requires the receiver to be implemented in an idempotent way. Often, this is done by adding a key-value store like Amazon DynamoDB or Amazon ElastiCache for Redis to the service. Using this approach, the receiver can track if the event has been seen before.
xxxxxxxxxx
import boto3
from datetime import datetime
import json
import os
import random
import uuid
TOPIC_ARN = os.environ['TOPIC_ARN']
sns = boto3.client('sns')
def lambda_handler(event, context):
jobId = str(random.randrange(0, 1000))
send_job_created_event(jobId)
send_job_updated_event(jobId)
send_job_deleted_event(jobId)
return
def send_job_created_event(jobId):
messageId = str(uuid.uuid4())
response = sns.publish(
TopicArn=TOPIC_ARN,
Subject=f'Job {jobId} created',
MessageDeduplicationId=messageId,
MessageGroupId=f'JOB-{jobId}',
Message={ },
MessageAttributes = {
'eventType': {
'DataType': 'String',
'StringValue': 'JobCreated'
}
}
)
print('sent message and received response: {}'.format(response))
return
def send_job_updated_event(jobId):
messageId = str(uuid.uuid4())
response = sns.publish( )
print('sent message and received response: {}'.format(response))
return
def send_job_deleted_event(jobId):
messageId = str(uuid.uuid4())
response = sns.publish( )
print('sent message and received response: {}'.format(response))
return
https://aws.amazon.com/blogs/compute/building-event-driven-architectures-with-amazon-sns-fifo/