ary
ary

Reputation: 959

in Lambda python query DynamoDb does not return any data

I wrote this function to return data from table where Artist name is Joe. But the code below is not resulting anything. It does first print. But after that nothing. Not sure what I am doing wrong.

from __future__ import print_function # Python 2/3 compatibility
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Music')

def handler(event, context):    
    print("Joe's music")
    print(table.creation_date_time)

response = table.query(
    KeyConditionExpression=Key('Artist').eq('Joe')
)

for i in response['Items']:
    print(i['Artist'], ":", i['Artist'])

Here is the result I am getting.

START RequestId: ...... Version: $LATEST
Joe's music
2017-07-19 03:07:54.701000+00:00
END RequestId: ...

Upvotes: 1

Views: 4039

Answers (1)

Chris Nauroth
Chris Nauroth

Reputation: 9854

Quoting a piece of your code sample:

def handler(event,context):    
    print("Joe's music")
    print(table.creation_date_time)

response = table.query(
    KeyConditionExpression=Key('Artist').eq('Joe')
)

for i in response['Items']:
    print(i['Artist'], ":", i['Artist'])

Is that exactly the code you have, including the indentation? Recall that indentation is significant to program structure in Python. That would mean your handler consists of just the 2 print statements, and the table lookup code is outside of that. It would make sense that you would only see the results of those 2 print statements.

Perhaps you meant something more like this, with indentation changed to group the table lookup and response handling code with the rest of the handler code.

def handler(event,context):    
    print("Joe's music")
    print(table.creation_date_time)

    response = table.query(
        KeyConditionExpression=Key('Artist').eq('Joe')
    )

    for i in response['Items']:
        print(i['Artist'], ":", i['Artist'])

Additionally, perhaps you want to return a value, depending on your needs. The AWS documentation on Lambda Function Handler (Python) has more details, including discussion of when the return value is significant.

Upvotes: 2

Related Questions