简体   繁体   中英

lambda python dynamodb write gets timeout error

I have been trying to work through my first tutorial on lambda dynamodb connections and am running into a timeout error.

In the lambda console I have the following code:

from __future__ import print_function
import json
import boto3

print('Loading function')

def lambda_handler(event, context):

    dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000")
    print('Dynamodb loaded')
    pages_table = dynamodb.Table('Pages')
    print('Pages table referenced')

    for item in event:
        print('Item: {}'.format(item))
        response = pages_table.put_item( Item=item)
        print('Response: {}'.format(response))

    return "hi"

I have created the Pages table via the dynamodb console.

When I test run the lambda function from the lambda console, I get:

START RequestId: 4008f77f-3b3e-11e7-ad79-0713f3bd7f4e Version: $LATEST
Dynamodb loaded
Pages table referenced
Item: {'UID': 1, 'id': 1, 'label': 'Original', 'snippet': 'Style', 'type': 'item', '$$hashKey': 'object:4'}
END RequestId: 4008f77f-3b3e-11e7-ad79-0713f3bd7f4e
REPORT RequestId: 4008f77f-3b3e-11e7-ad79-0713f3bd7f4e  Duration: 3002.30 ms    
Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 36 MB  
2017-05-17T20:20:24.159Z 4008f77f-3b3e-11e7-ad79-0713f3bd7f4e Task timed out after 3.00 seconds

My debugging print statements indicate that the very first pages_table.put_item() is where the timeout happens since there is no subsequent print of the response. (I have a few elements in my Item array for testing)

When I check my dynamodb table there is no data in the table, even days later.

I think I have accurately followed all the steps in the AWS documentation for creating a dynamodb accessible via lambda (IAM permissions, dynamodb table keys, etc), but have been stumped when searching for other instances of similar experiences with timeouts. The closest things I can find references VPC configuration, but that doesn't seem to make sense for me with this essentially default setup from the tutorials since the tutorials didn't mention VPC setup. Also, when I dug into the VPC console the settings there appeared to be default.

What am I missing?

The endpoint URL for DynamoDB seems to be incorrect:

endpoint_url="http://localhost:8000"

This is used only for local testing of DynamoDB. From Lambda function, the endpoint for us-east-1 needs to be:

endpoint_url="https://dynamodb.us-east-1.amazonaws.com"

As per the AWS Regions and Endpoints document.

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