使用Python boto3放入本地DynamoDB表超时 [英] Putting to local DynamoDB table with Python boto3 times out

查看:46
本文介绍了使用Python boto3放入本地DynamoDB表超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过触发Python lambda表达式以编程方式将数据放入本地运行的DynamoDB容器中.

I am attempting to programmatically put data into a locally running DynamoDB Container by triggering a Python lambda expression.

我正在尝试遵循此处提供的模板: https://hub.docker.com/r/amazon/dynamodb-local

I'm trying to follow the template provided here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html I am using the amazon/dynamodb-local you can download here: https://hub.docker.com/r/amazon/dynamodb-local

使用Ubuntu 18.04.2 LTS运行容器和Lambda服务器AWS Sam CLI运行我的Lambda APIDocker版本18.09.4Python 3.6(您可以在下面的sam日志中看到此内容)python lambda的启动命令只是"sam local start-api"

Using Ubuntu 18.04.2 LTS to run the container and lambda server AWS Sam CLI to run my Lambda api Docker Version 18.09.4 Python 3.6 (You can see this in sam logs below) Startup command for python lambda is just "sam local start-api"

首先是我的Lambda代码

First my Lambda Code

import json
import boto3

def lambda_handler(event, context):
    print("before grabbing dynamodb")
#    dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000",region_name='us-west-2',AWS_ACCESS_KEY_ID='RANDOM',AWS_SECRET_ACCESS_KEY='RANDOM')
    dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
    table = dynamodb.Table('ContactRequests')
    try:
        response = table.put_item(
            Item={
                'id': "1234",
                'name': "test user",
                'email': "testEmail@gmail.com"
                }
            )

    print("response: " + str(response))

    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world"
        }),
    }

我知道我应该在localhost:8000上有此表ContactRequests,因为我可以运行此脚本来查看我的docker容器dynamodb表我已经在boto.resource调用中使用各种值对它进行了测试,以包括访问密钥,区域名称和秘密密钥,但结果没有改善

I know that I should have this table ContactRequests available at localhost:8000, because I can run this script to view my docker container dynamodb tables I have tested this with a variety of values in the boto.resource call to include the access keys, region names, and secret keys, with no improvement to result

dev@ubuntu:~/Projects$ aws dynamodb list-tables --endpoint-url http://localhost:8000
{
    "TableNames": [
        "ContactRequests"
    ]
}

我也能够成功打入dynamodb提供的localhost:8000/shell

I am also able to successfully hit the localhost:8000/shell that dynamodb offers

不幸的是,在运行时,如果我撞到了触发此方法的端点,则会收到一个类似这样的超时记录

Unfortunately while running, if I hit the endpoint that triggers this method, I get a timeout that logs like so

Fetching lambci/lambda:python3.6 Docker container image......
2019-04-09 15:52:08 Mounting /home/dev/Projects/sam-app/.aws-sam/build/HelloWorldFunction as /var/task:ro inside runtime container
2019-04-09 15:52:12 Function 'HelloWorldFunction' timed out after 3 seconds
2019-04-09 15:52:13 Function returned an invalid response (must include one of: body, headers or statusCode in the response object). Response received: 
2019-04-09 15:52:13 127.0.0.1 - - [09/Apr/2019 15:52:13] "GET /hello HTTP/1.1" 502 -

请注意,没有触发任何打印方法,如果我删除对table.put的调用,则将成功调用打印方法.

Notice that none of my print methods are being triggered, if I remove the call to table.put, then the print methods are successfully called.

我在Stack Overflow上看到了类似的问题,例如 lambda python dynamodb写入获取超时错误,指出问题是我正在使用本地数据库,但是如果我将其指向本地运行的dynamodb实例,我是否仍不能使用boto3写入本地数据库?

I've seen similar questions on Stack Overflow such as this lambda python dynamodb write gets timeout error that state that the problem is I am using a local db, but shouldn't I still be able to write to a local db with boto3, if I point it to my locally running dynamodb instance?

推荐答案

在此处找到问题的解决方案:

Found the solution to the problem here: connecting AWS SAM Local with dynamodb in docker The question asker noted that he saw online that he may need to connect to the same docker network using:

docker网络创建本地lambda

因此创建了这个网络,然后更新了我的sam命令和docker命令以使用该网络,就像这样:

So created this network, then updated my sam command and my docker commands to use this network, like so:

docker run --name dynamodb -p 8000:8000 --network = local-lambda amazon/dynamodb-local

sam local start-api --docker-network local-lambda

此后,我不再遇到超时问题.我仍在努力确切地了解为什么这是问题所在

After that I no longer experienced the timeout issue. I'm still working on understanding exactly why this was the issue

不过,为了公平起见,同样重要的是,我也使用dynamodb容器名称作为我的boto3资源调用的主机.因此,最终,将上述解决方案与"Reto Aebersold"提供的答案结合在一起,创建了最终解决方案

To be fair though, it was important that I use the dynamodb container name as the host for my boto3 resource call as well. So in the end, it was a combination of the solution above and the answer provided by "Reto Aebersold" that created the final solution

dynamodb = boto3.resource('dynamodb',endpoint_url ="http://< DynamoDB_LOCAL_NAME>:8000")

这篇关于使用Python boto3放入本地DynamoDB表超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆