1
votes

I am trying to query my dynamodb table with a boto3 query using a FilterExpression, but no results are being returned because the attribute name that I wish to filter by has a '.' in it. ExpressionAttributeNames don't seem to work with the boto3.dynamodb.conditions.Attr, and I can't find any example python code online of a similar scenario.

kwargs = {'IndexName': constants.RESOURCE_TYPE_INDEX, 'KeyConditionExpression': Key(constants.RESOURCE_TYPE).eq(constants.AUTOSCALING_GROUP),
          'FilterExpression': Attr('configuration.loadBalancerNames').contains(filter_value),
          'ProjectionExpression': 'relationships, PRIMARY_KEY'}
print("Getting associations for: " + constants.AUTOSCALING_GROUP)
retry = True
while retry:
    retry = False
    try:
        response = table.query(**kwargs)
    except ClientError as e:
        if e.response['Error']['Code'] == 'ProvisionedThroughputExceededException':
            time.sleep(5)
            retry = True
            print("Retrying query")
        else:
            exception_type = str(e.__class__.__name__)
            logger.error(
                "Function: utilities.get_asg_records_for_lb_enrichment " + "Exception Type: " + exception_type + " Exception Message:" + str(e))
    except Exception as e:
        exception_type = str(e.__class__.__name__)
        logger.error(
            "Function: utilities.get_asg_records_for_lb_enrichment " + "Exception Type: " + exception_type + " Exception Message:" + str(e))

    if response:
        associations.extend(response.get('Items'))
        if response.get('LastEvaluatedKey'):
            kwargs['ExclusiveStartKey'] = response.get('LastEvaluatedKey')
            retry = True
            print("Last evaluated key found: " + str(response.get('LastEvaluatedKey')))
1
Can you post your actual code for the query?James Shapiro
@JamesShapiro just added itMatt Heidelbaugh

1 Answers

1
votes

I guess . is considered as a special character in DynamoDB, so please use an Expression Attribute Name instead.

So your kwargs would look something like this,

kwargs = {
    'IndexName': constants.RESOURCE_TYPE_INDEX,
    'KeyConditionExpression': Key(constants.RESOURCE_TYPE).eq(constants.AUTOSCALING_GROUP),
    'FilterExpression': 'contains(#cl, :val)',
    'ExpressionAttributeNames': {
        '#cl': 'configuration.loadBalancerNames'
    },
    'ExpressionAttributeValues': {
        ':val': filter_value
    },
    'ProjectionExpression': 'relationships, PRIMARY_KEY'
}