Name 'Key' not defined Lambda function to access DynamoDB
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Python error NameError: name 'Key' is not defined appears frequently in AWS Lambda code that queries DynamoDB with boto3. The issue is usually simple: the code is building a key condition expression with Key(...) but never imported the helper class that defines it.
Why Key Needs an Import
When you use the higher-level DynamoDB resource API, methods such as query accept expression-builder objects instead of raw condition strings. Key is one of those helpers, and it comes from boto3.dynamodb.conditions.
Without the import, Python raises NameError before DynamoDB ever receives a request.
Fix the Lambda Function Directly
The usual fix is just to add the missing import and keep the query expression explicit.
This is the correct pattern when you are querying by partition key, or by partition key plus sort key conditions.
Know the Difference Between query and get_item
Another source of confusion is that get_item also uses a parameter called Key, but that Key is just a dictionary field in the request, not the imported builder class.
So there are really two unrelated things with very similar names:
- the imported
Keyhelper class for condition expressions - the request parameter named
Keyin methods such asget_item
Mixing them up makes this error more common than it should be.
Key Versus Attr
If you are filtering on non-key attributes, you may need Attr instead of Key, or both together.
Use Key for partition and sort key conditions. Use Attr for other attributes in filter expressions.
Add Validation and Logging in Lambda
While the import fixes the specific NameError, Lambda code should also validate inputs and log failures cleanly so other DynamoDB problems are easier to diagnose.
This makes the function easier to operate once you get past the missing import problem.
Common Pitfalls
One pitfall is assuming that importing boto3 alone also imports Key. It does not. You must import the expression helpers explicitly.
Another is trying to use Key for non-key attribute filters. In those cases, Attr is usually the correct helper.
Developers also forget to ask whether query is the right operation at all. If you already know the full primary key, get_item is often simpler and does not need a key condition expression.
Summary
- '
NameError: name 'Key' is not definedusually means the import is missing.' - Add
from boto3.dynamodb.conditions import Keywhen usingKeyConditionExpression. - Remember that the
Keyargument inget_itemis not the same thing as theKeybuilder class. - Use
Attrfor non-key filters. - Add input validation and logging so Lambda failures are easier to debug.
Related reading
- NameError uninitialized constant PaperclipStorageS3AWS
- Necessary s3cmd S3 permissions for PUT/Sync
- Need architecture hint Data replication into the cloud data cleansing
- Need to perform AWS calls for account xxx, but no credentials have been configured
- Named colors in matplotlib
- Named placeholders in string formatting
- NamedPipeServerStream.BeginWaitForConnection fails with System.IO.Exception The pipe is being
- NameError global name 'xrange' is not defined in Python 3

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.