我有这个模块连接到Dynamo,创建一个会话表,并在其中放置一个项目:
import boto.dynamodb2
from boto.dynamodb2.table import Table
from boto.dynamodb2.fields import HashKey
from boto.regioninfo import RegionInfo
from boto.dynamodb2.layer1 import DynamoDBConnection
import uuid
try:
sessions = Table(
table_name='IssueBoxSessions',
schema=[HashKey('SessionId')],
connection=DynamoDBConnection(
region=RegionInfo(name='eu-west-1',
endpoint='dynamodb.eu-west-1.amazonaws.com')
))
except:
print("Dynamo can't connect")
def create_session():
id = str(uuid.uuid4())
res = sessions.put_item(data={
'SessionId': id,
'data': {
'user_id': 1,
'ip': '10.1.1.10',
'datetime': 'now'
}
})
print(res)
return res
请注意,我正在开发和测试EC2实例,并使用附加到实例的IAM角色对Dynamo进行身份验证。
因为我正在使用IAM,所以身份验证不需要代码。它们允许实体(我的实例)访问某些服务(dynamo)。它们旨在防止必须将访问密钥放入某些AWS资源的代码中。如果是认证的情况,它也不适用于解释器。我相信这个问题来自IAM身份验证以外的其他问题。
因此,当我在python解释器中导入这个create_session
函数并调用它时,它可以工作:
>>> from issuesite.session_handler import create_session
>>> create_session()
True
True
但是,当我尝试在烧瓶附近的任何地方使用它时,它会熔化:
from flask import request
from issuesite.session_handler import create_session
from issuesite import app
@app.route('/login', methods=['GET'])
def login():
if create_session():
return "ok"
抛出的错误是:
JSONResponseError: JSONResponseError: 400 Bad Request
{u'message': u'The security token included in the request is invalid.', u'__type': u'com.amazon.coral.service#UnrecognizedClientException'}
事实上,这是所有美丽的全部追溯:
Traceback (most recent call last):
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/server/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/server/issuesite/views/base.py", line 14, in login
if create_session():
File "/server/issuesite/session_handler/__init__.py", line 29, in create_session
'datetime': 'now'
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 821, in put_item
return item.save(overwrite=overwrite)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/items.py", line 455, in save
returned = self.table._put_item(final_data, expects=expects)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/table.py", line 835, in _put_item
self.connection.put_item(self.table_name, item_data, **kwargs)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 1510, in put_item
body=json.dumps(params))
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2842, in make_request
retry_handler=self._retry_handler)
File "/server/flask/lib/python2.7/site-packages/boto/connection.py", line 954, in _mexe
status = retry_handler(response, i, next_sleep)
File "/server/flask/lib/python2.7/site-packages/boto/dynamodb2/layer1.py", line 2885, in _retry_handler
data)
答案 0 :(得分:0)
从您发布的代码中,您使用的是Boto的常规连接方法,而您应该使用特定于IAM(http://boto.readthedocs.org/en/latest/ref/iam.html)的方法。
我希望这会有所帮助。
答案 1 :(得分:0)
这看起来像凭据问题。您使用的是正确的凭据吗?看起来与此issue类似。可能值得设置debug logger以查看从何处/正在提取的凭据。
答案 2 :(得分:0)
我尝试使用实际的AWS凭据而不是IAM角色,最后我采用了实例的AMI并使用相同的IAM角色从其中启动另一个实例,有趣的是,与Dynamo交谈没有任何问题。
我最后继续使用替换实例。
但是,我无法确定究竟是什么导致了这个问题。