Categories
Python

AWS Lambda – name ‘Key’ is not defined

When going through the example from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html I ran into the following error message:

Response:
{
  "errorMessage": "name 'Key' is not defined",
  "errorType": "NameError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 20, in lambda_handler\n    KeyConditionExpression=Key('year').eq(1985)\n"
  ]
}

The error come from this line

response = table.query(
    KeyConditionExpression=Key('year').eq(1985)
)

The issue was that I was only importing boto3, this is not enough. Guess I have to read the whole tutorial. You’ll have to add the following import.

from boto3.dynamodb.conditions import Key