Executing a Lambda Python Query on a DynamoDB table

0

I am trying to do something simple. I need to make sure at the time of registration in my app, that the Username they request is not used in my AWS backend.

To complete this, I have a small table in DynamoDB named: D1UsersLookup. It has the following attributes:

  • string Username - the PK
  • string Role - the Sortkey
  • string AccountID Currently, I have 2 records (One day expect 100ks records)

Username Role AccountID runghof Fan 1 Elvis Artist 2

My Lambda Python function attempts to query the table to make sure the table does not have a Username a new user would like to have during a registration process. If there is no match for my query, then I can add it to this table. Conversely, if there is a match, then I need to inform my client app (.Net-Maui/C# code) that they need to choose a different Username. Sounds simple, but it hasn't been.

Here's the base of the code, Logging and timing stripped out for now:

import boto3 from boto3 import resource from boto3.dynamodb.conditions import Key, Attr

def lambda_handler(event, context): myUsername = 'Elvis' dynamodb = boto3.resource("dynamodb") table= dynamodb.Table("D1UsersLookup") response = table.query(KeyConditionExpression=Key('Username').eq(myUsername)) print('Searching for myUsernane ' + myUsername) print(response) IItems = response ['Items'] print(IItems)

The complete response string is: {'Items': [], 'Count': 0, 'ScannedCount': 0, 'ResponseMetadata': {'RequestId': 'QO9KDI66TK3T3LQMC9D8P93PFJVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 08 May 2024 01:14:11 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '39', 'connection': 'keep-alive', 'x-amzn-requestid': 'QO9KDI66TK3T3LQMC9D8P93PFJVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '3413411624'}, 'RetryAttempts': 0}}

Note: Since there's a record with the Username set to: Elvis, I would expect the Count to be 'Count': 1 Continueing

myUsername = 'bob'
response = table.query(KeyConditionExpression=Key('Username').eq(myUsername))
print('Searching for myUsernane ' + myUsername)
print(response)
IItems = response ['Items']
print(IItems)

The response is: {'Items': [], 'Count': 0, 'ScannedCount': 0, 'ResponseMetadata': {'RequestId': 'IJ7V182G2AIGPQOQV54RGT8VMVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Wed, 08 May 2024 01:14:11 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '39', 'connection': 'keep-alive', 'x-amzn-requestid': 'IJ7V182G2AIGPQOQV54RGT8VMVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '3413411624'}, 'RetryAttempts': 0}}

Note: In this case, I expected the count to be 0-zero

Finally, I've chosen query because I've read a scan can get expensive.

I feel I'm missing something basic.

Any help would be appreciated.

Petrus

Petrus
asked 12 days ago465 views
2 Answers
0
Accepted Answer

Eventual consistency means consistency is usually achieved within a second. So if the records have been there a while then no, you won't need to do a strongly-consistent read.

EXPERT
answered 11 days ago
  • I have switched to performing a scan instead of a query. It's finding one of the records I placed in the terminal a day ago. I does NOT find the record I placed in the table 2 hours ago.....Does anyone know what is going on? Help, please?

0

Hi,

Which consistency rule did you apply to your DDB table? By default it is "eventually consistent" which means:

When issuing eventually consistent reads to a DynamoDB table or an index, 
the responses may not reflect the results of a recently completed write operation.
 If you repeat your read request after a short time, the response should eventually
 return the more recent item. 

as per documentation at https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadConsistency.html

In your use case, you may want to use Strongly Consistent Read.

Best,

Didier

profile pictureAWS
EXPERT
answered 12 days ago
profile pictureAWS
EXPERT
reviewed 11 days ago
  • Seems odd, but I've read lots of documentation. I will give this a try.

    response = table.query(KeyConditionExpression=Key('Username').eq(myUsername) consistent_read=True )

    Is this the correct syntax? Also, are there additional costs involved adding this to every read operation?

  • Well, that didn't work! The error indicated this option is not allowed on the query method, suggesting it should be on the table method. So:

    table= dynamodb.Table("D1UsersLookup", consistent_read=True) That also didn't work:. Error: errorMessage": "Unknown keyword argument: consistent_read",

    What I don't understand is that the data has been in the table for 24 hours. Surely it should be able to be queried on or scanned?!?!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions