简体   繁体   中英

AWS Lambda function to read DynamoDB Table

I am new to coding and writing a func to read data (ie scan table) from Dynamodb table. I am getting an error at step when we create DDB resource.

What am I missing here?

import json
import boto3

def lambda_handler(event, context):
    return {
        
        dynamodb = boto3.resource("dynamodb")
        table = dynamodb.Table("TrackerTable")
        
        try:
            response = table.scan(
                FilterExpression=Attr('Id').eq('1745696') & Attr('Code').eq('LTA-121647')
            )
        except ClientError as e:
            print(e.response['Error']['Message'])
        
        print("total count of element: ",len(response))
    }

Error message:

errorMessage "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 7)" errorType "Runtime.UserCodeSyntaxError" requestId "7333c030-1930-4396-ba84-07d7027482e1" stackTrace 0 " File "/var/task/lambda_function.py" Line 7\n dynamodb = boto3.resource("dynamodb")\n"

Your code is invalid Python . Not sure what you want to do, but you can't enclose everything with return . Maybe it should be:

import json
import boto3

def lambda_handler(event, context):

    
    dynamodb = boto3.resource("dynamodb")
    table = dynamodb.Table("TrackerTable")
    
    try:
        response = table.scan(
            FilterExpression=Attr('Id').eq('1745696') & Attr('Code').eq('LTA-121647')
        )
    except ClientError as e:
        print(e.response['Error']['Message'])
    
    print("total count of element: ",len(response))

    return json.dumps(response, default=str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM