Search code examples
aws-lambdaamazon-dynamodbboto3

AWS Lambda python boto3 dynamodb table scan - An error occurred (ValidationException) when calling the Scan operation: ExpressionAttributeNames


I am writing a simple dynamodb table scan with a filter expression using the boto3 documentation https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.scan

I am getting this error: { "errorMessage": "An error occurred (ValidationException) when calling the Scan operation: ExpressionAttributeNames can only be specified when using expressions", "errorType": "ClientError", Code: import boto3 import pprint from pprint import pprint from boto3.dynamodb.conditions import Key from boto3.dynamodb.conditions import Attr

    dbresource =boto3.resource('dynamodb')

    def lambda_handler(event, context):

    tableobject = dbresource.Table('basicSongsTable')

    response = tableobject.scan(ExpressionAttributeNames={'artist': ':variableartist'},
                            ExpressionAttributeValues={':variableartist': {'S': 'Matt'}})

    pprint(response)

Solution

  • I'm guessing you want to Scan the table and return when the artist=Matt?

    dbresource =boto3.resource('dynamodb')
    
        def lambda_handler(event, context):
    
        tableobject = dbresource.Table('basicSongsTable')
    
        response = tableobject.scan(
        ExpressionAttributeNames={'#a': 'artist'},
        ExpressionAttributeValues={':v': {'S': 'Matt'}},
        FilterExpression='#a=:v'
        )
    
        print(response)
    

    More examples here: https://github.com/aws-samples/aws-dynamodb-examples/tree/master/DynamoDB-SDK-Examples/python/WorkingWithScans