· 6 years ago · Mar 09, 2019, 04:58 PM
1import os
2import boto3
3import datetime
4
5region = os.environ.get('AWS_DEFAULT_REGION', 'us-west-2')
6dynamodb = boto3.client('dynamodb', region_name=region)
7
8class DailyResize(object):
9
10 FIRST_DAY_RCU, FIRST_DAY_WCU = 300, 1000
11 SECOND_DAY_RCU, SECOND_DAY_WCU = 100, 1
12 THIRD_DAY_RCU, THIRD_DAY_WCU = 1, 1
13
14 def __init__ (self, table_prefix):
15 self.table_prefix = table_prefix
16
17 def create_new(self):
18 # create new table (300 RCU, 1000 WCU)
19 today = datetime.date.today()
20 new_table_name = "%s_%s" % (self.table_prefix, self._format_date(today))
21 dynamodb.create_table(
22 TableName=new_table_name,
23 KeySchema=[
24 { 'AttributeName': "pk", 'KeyType': "HASH"}, # Partition key
25 { 'AttributeName': "sk", 'KeyType': "RANGE" } # Sort key
26 ],
27 AttributeDefinitions=[
28 { 'AttributeName': "pk", 'AttributeType': "N" },
29 { 'AttributeName': "sk", 'AttributeType': "N" }
30 ],
31 ProvisionedThroughput={
32 'ReadCapacityUnits': self.FIRST_DAY_RCU,
33 'WriteCapacityUnits': self.FIRST_DAY_WCU,
34 },
35 )
36
37 print("Table created with name '%s'" % new_table_name)
38 return new_table_name
39
40
41 def resize_old(self):
42 # update yesterday's table (100 RCU, 1 WCU)
43 yesterday = datetime.date.today() - datetime.timedelta(1)
44 old_table_name = "%s_%s" % (self.table_prefix, self._format_date(yesterday))
45 self._update_table(old_table_name, self.SECOND_DAY_RCU, self.SECOND_DAY_WCU)
46
47 # update the day before yesterday's table (1 RCU, 1 WCU)
48 the_day_before_yesterday = datetime.date.today() - datetime.timedelta(2)
49 very_old_table_name = "%s_%s" % (self.table_prefix, self._format_date(the_day_before_yesterday))
50 self._update_table(very_old_table_name, self.THIRD_DAY_RCU, self.THIRD_DAY_WCU)
51
52 return "OK"
53
54
55 def _update_table(self, table_name, RCU, WCU):
56 """ Update RCU/WCU of the given table (if exists) """
57 print("Updating table with name '%s'" % table_name)
58 try:
59 dynamodb.update_table(
60 TableName=table_name,
61 ProvisionedThroughput={
62 'ReadCapacityUnits': RCU,
63 'WriteCapacityUnits': WCU,
64 },
65 )
66 except dynamodb.exceptions.ResourceNotFoundException as ex:
67 print("DynamoDB Table %s not found" % table_name)
68
69
70 @staticmethod
71 def _format_date(d):
72 return d.strftime("%Y-%m-%d")