Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb create_table calls fails

I am following up Python tutorial for dynamodb setting local dynomodb on port 8000 http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.01.html

from __future__ import print_function # Python 2/3 compatibility
import boto3

dynamodb = boto3.resource('dynamodb',  aws_access_key_id="anything", aws_secret_access_key="anything", region_name='us-west-2', endpoint_url="http://localhost:8000")


table = dynamodb.create_table(
TableName='users',
KeySchema=[
    {
        'AttributeName': 'username',
        'KeyType': 'HASH'
    },
    {
        'AttributeName': 'last_name',
        'KeyType': 'RANGE'
    }
],
AttributeDefinitions=[
    {
        'AttributeName': 'username',
        'AttributeType': 'S'
    },
    {
        'AttributeName': 'last_name',
        'AttributeType': 'S'
    },

],
ProvisionedThroughput={
    'ReadCapacityUnits': 5,
    'WriteCapacityUnits': 5
}

)
print("Table status:", table.table_status)

However python run to the code is failing with below issue.

Not sure what is causing it.

Traceback (most recent call last):
File "C:\Users\rbharadw\workspace\dynamoDb\dynamoDb.py", line 32, in <module>
'WriteCapacityUnits': 5
File "Python\Python35\lib\site-packages\boto3\resources\factory.py", line  520, in do_action
response = action(self, *args, **kwargs)
File "Python\Python35\lib\site-packages\boto3\resources\action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(**params)
File "Python\Python35\lib\site-packages\botocore\client.py", line 159, in _api_call
return self._make_api_call(operation_name, kwargs)
File "Python\Python35\lib\site-packages\botocore\client.py", line 483, in _make_api_call
operation_model, request_dict)
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 117, in make_request
return self._send_request(request_dict, operation_model)
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 144, in _send_request
request, operation_model, attempts)
File "Python\Python35\lib\site-packages\botocore\endpoint.py", line 203, in _get_response
response_dict, operation_model.output_shape)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 211, in parse
parsed = self._do_parse(response, shape)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 587, in _do_parse
parsed = self._parse_shape(shape, original_parsed)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape
return handler(shape, node)
 File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure
raw_value)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape
return handler(shape, node)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure
raw_value)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape
return handler(shape, node)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 522, in _handle_structure
raw_value)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 258, in _parse_shape
return handler(shape, node)
File "Python\Python35\lib\site-packages\botocore\parsers.py", line 539, in _handle_timestamp
return self._timestamp_parser(value)
File "Python\Python35\lib\site-packages\botocore\utils.py", line 327, in parse_timestamp
return datetime.datetime.fromtimestamp(value, tzlocal())
File "Python\Python35\lib\site-packages\dateutil\tz\tz.py", line 99, in utcoffset
  if self._isdst(dt):
File "Python\Python35\lib\site-packages\dateutil\tz\tz.py", line 143, in    _isdst
return time.localtime(timestamp+time.timezone).tm_isdst
OSError: [Errno 22] Invalid argument

however it looks like tables get created as second run gives error for

botocore.exceptions.ClientError: An error occurred (ResourceInUseException)  when calling the CreateTable operation: Cannot create preexisting table

Any suggestions!!!

like image 211
Ruchir Bharadwaj Avatar asked Aug 12 '16 13:08

Ruchir Bharadwaj


People also ask

What causes DynamoDB system errors?

The most likely cause is an invalid AWS access key ID or secret key. This error can occur for several reasons, such as a required parameter that is missing, a value that is out of range, or mismatched data types. The error message contains details about the specific part of the request that caused the error.

Does DynamoDB PutItem overwrite?

By default, the DynamoDB write operations ( PutItem , UpdateItem , DeleteItem ) are unconditional: Each operation overwrites an existing item that has the specified primary key.

What is KeySchema in DynamoDB?

KeySchema. Specifies the attributes that make up the primary key for a table or an index. The attributes in KeySchema must also be defined in the AttributeDefinitions array. For more information, see Data Model in the Amazon DynamoDB Developer Guide.

What DynamoDB feature would prevent the most number of overwrite?

If you would like to prevent overwriting an existing Item, you'll need to use condition expressions. If you only want to update portions of an existing Item rather than overwriting the entire Item, you'll need to use the UpdateItem API call.


2 Answers

Unfortunately that set os.environ["TZ"] = "UTC" doesn't work for me.

So I follow one thread, locate the site-packages\dateutil\tz\tz.py file. In the def _naive_is_dst(self, dt) function, change it into

   # workaround the bug of negative offset UTC prob
    if timestamp+time.timezone < 0:
        current_time = timestamp + time.timezone + 31536000
    else:
        current_time = timestamp + time.timezone

    return time.localtime(current_time).tm_isdst
like image 159
Chen Wang Avatar answered Jan 01 '23 17:01

Chen Wang


This is due to a bug with one of the date processing libraries that boto uses.

The bug occurs when your timezone offset is a positive number.

You can work around it by inserting the following code before boto (or the library it uses) is imported.

import os
os.environ["TZ"] = "UTC"
like image 44
Noah McIlraith Avatar answered Jan 01 '23 19:01

Noah McIlraith