Reputation: 237
I am trying to modify the lambda-microservice example to submit a GET query to dynamodb from an aws-api-gateway. I have gotten the example to work but it does a table scan, not a query.
The query fails trying to set the KeyConditionExpression with the following message: "Lambda execution failed with status 200 due to customer function error: name 'Key' is not defined."
The relevant python is here:
operations = {
'GET': lambda dynamo, x: dynamo.query(**x),
}
dynamodb = boto3.resource('dynamodb')
params = {
'KeyConditionExpression': Key('flight').eq("2002")
}
table = dynamodb.Table('Test')
response = table.query(params)
This query form is the same one that worked in this posting:
[Working query][1]
and I got the same error above.
Alternatively, I get a different error when I try to set the KeyConditions: "Lambda execution failed with status 200 due to customer function error: An error occurred (ValidationException) when calling the Query operation: Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.. "
The relevant python is here:
params = {
'TableName': 'Test',
'KeyConditions': {
"flight": {
'ComparisonOperator': "EQ",
'AttributeValueList': [{'S': '2002'}]
}
}
}
My table (Test) has a GSI index, flight-index, with flight as a partition key and the items (all type string) are simply these:
A different post said: "KeyConditionExpression is not supported with nodejs SDK." Is there any similar problem with my using Python 3.7?
Also I am not running DynamoDB local. (I'm grasping at straws for clues.)
As simple as this whole test case is, I must be missing something awfully simple, but I have struggled for several days and read dozens of posts to find it.
Can someone help make either of these ways of specifying the query target work?
Upvotes: 0
Views: 3522
Reputation: 237
There were a couple of different issues that complicated my problem. I presume no one answered because there wasn't an obvious fix.
What I had to do:
Import Key python library:
Many thanks to @user3303554 for the advice in this answer to import Key from boto3.dynamodb.conditions import Key
DynamoDB query permission: That finally gave me an AccessDeniedException that showed my lambda function did not have permission to query DynamoDb. Many additional thanks to @Lisa M Shon in this answer for pointing me to http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/using-identity-based-policies.html because that showed that the microservice example I started modifying had read/write perms, but it did not have a query perm. Added that to the role, and it worked like a charm.
Hopes this helps somebody else just trying to do a simple DynamoDB query.
Upvotes: 2