Complete the code to add an idempotency token attribute named 'IdempotencyKey' to the DynamoDB item.
item = { 'OrderId': '123', 'Amount': 100, 'IdempotencyKey': [1] }The idempotency token should be a unique string to identify the request. Here, 'unique-token-123' is a valid string token.
Complete the code to include the idempotency token in the DynamoDB PutItem request condition expression to prevent duplicate writes.
response = table.put_item(Item=item, ConditionExpression='attribute_not_exists([1])')
The ConditionExpression uses 'attribute_not_exists' on the 'IdempotencyKey' to ensure the item is only inserted if the token is not already present.
Fix the error in the code by completing the parameter to generate a unique idempotency token using Python's uuid module.
import uuid idempotency_token = [1]
The correct way to generate a unique UUID token is by calling uuid.uuid4().
Fill both blanks to complete the DynamoDB update expression that sets the idempotency token only if it does not already exist.
response = table.update_item(
Key={'OrderId': '123'},
UpdateExpression='SET [1] = :token',
ConditionExpression='attribute_not_exists([2])',
ExpressionAttributeValues={':token': idempotency_token}
)Both the UpdateExpression and ConditionExpression must refer to the 'IdempotencyKey' attribute to ensure the token is set only if it does not exist.
Fill all three blanks to complete the Python dictionary comprehension that creates a map of order IDs to their idempotency tokens, filtering only orders with tokens present.
idempotency_map = {order['[1]']: order['[2]'] for order in orders if '[3]' in order}The dictionary maps each 'OrderId' to its 'IdempotencyKey', including only orders that have the 'IdempotencyKey' attribute.