1

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: ...

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Chris, Its working now. I am learning this and this indentation thing is so painful. Not sure what kind of brain designed this logic :( Also is there any good IDE to work with aws python. AWS dosn't have code insight etc.
@ary , some people like the indentation better than balancing out curly braces. Different strokes I guess. :-) I don't use a Python IDE myself, but I have heard good things about PyCharm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.