Dynamo Db JSONResponseError with boto under flash
I have a very ugly PUT problem at Dynamo that I have been heading against the last two days.
I have this module that connects to Dynamo and gives me some functionality:
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
Note that I am developing and testing an EC2 instance and authenticating with Dynamo using an IAM role attached to the instance.
So when I import this function create_session
in python interpreter and call it, it works:
>>> from issuesite.session_handler import create_session
>>> create_session()
True
True
But, when I try to use it anywhere near the flask, it has a fusion:
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"
Thrown error:
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'}
In fact, here's the whole tracing in all its beauty:
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)
Does anyone have any ideas as to what is going on here?
Thanks in advance!:)
source to share
From the code you posted, you are using the usual Boto connection methods, while you should use the ones that are for IAM ( http://boto.readthedocs.org/en/latest/ref/iam.html ).
Hope this helps.
source to share
I tried to use actual AWS credentials instead of the IAM role, and I end up taking the instance AMI and launching another instance from it with the same IAM role, and interestingly, it had no dynamism issues.
I ended up with a replacement instance.
However, I cannot be sure what exactly caused the problem.
source to share