Guest User

Untitled

a guest
Feb 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import boto3
  2.  
  3. def creation_table_scaling(table_name: str, key_name: str, key_type: str, max_read_capacity: int, max_write_capacity: int):
  4. """
  5. Create table dynamodb and make it scale then wait for it to be created
  6. Args:
  7. table_name (str): the name of the table to create
  8. key_name (str): the name of the key, and main attribute of the table
  9. key_type (str): the data type for the attribute, S for string, N for number, B for binary
  10. max_read_capacity(int) : Max nb of read per minute
  11. max_write_capacity(int) : Max nb of writes per minute
  12. Returns:
  13. """
  14. dynamodb = boto3.resource('dynamodb')
  15. scaling_dynamodb = boto3.client('application-autoscaling')
  16. min_read_capacity = 5
  17. min_write_capacity = 5
  18. table = dynamodb.create_table(
  19. TableName=table_name,
  20. KeySchema=[
  21. {
  22. 'AttributeName': key_name,
  23. 'KeyType': 'HASH'
  24. },
  25. ],
  26. AttributeDefinitions=[
  27. {
  28. 'AttributeName': key_name,
  29. 'AttributeType': key_type
  30. },
  31. ],
  32. ProvisionedThroughput={
  33. 'ReadCapacityUnits': min_read_capacity,
  34. 'WriteCapacityUnits': min_write_capacity
  35. }
  36. )
  37. scalable_dimensions = {'dynamodb:table:ReadCapacityUnits': [min_read_capacity, max_read_capacity],
  38. 'dynamodb:table:WriteCapacityUnits': [min_write_capacity, max_write_capacity]}
  39. for scalable_dimension, capacity in scalable_dimensions.items():
  40. scaling_dynamodb.register_scalable_target(ServiceNamespace="dynamodb",
  41. ResourceId="table/{}".format(table_name),
  42. ScalableDimension=scalable_dimension,
  43. MinCapacity=capacity[0],
  44. MaxCapacity=capacity[1])
  45. # create scaling policy, without them the auto scaling will not be applied
  46. metrics_and_dimension = {'DynamoDBReadCapacityUtilization': 'dynamodb:table:ReadCapacityUnits',
  47. 'DynamoDBWriteCapacityUtilization': 'dynamodb:table:WriteCapacityUnits'}
  48. percent_of_use_to_aim_for = 50.0
  49. scale_out_cooldown_in_seconds = 60
  50. scale_in_cooldown_in_seconds = 60
  51. for metric, dimension in metrics_and_dimension.items():
  52. LOGGER.info(metric)
  53. LOGGER.info(dimension)
  54. scaling_dynamodb.put_scaling_policy(ServiceNamespace="dynamodb",
  55. ResourceId="table/{}".format(table_name),
  56. PolicyType='TargetTrackingScaling',
  57. PolicyName="Scale{}".format(metric),
  58. ScalableDimension=dimension,
  59. TargetTrackingScalingPolicyConfiguration={
  60. 'TargetValue': percent_of_use_to_aim_for,
  61. 'PredefinedMetricSpecification': {
  62. 'PredefinedMetricType': metric
  63. },
  64. 'ScaleOutCooldown': scale_out_cooldown_in_seconds,
  65. 'ScaleInCooldown': scale_in_cooldown_in_seconds
  66. })
  67. table.meta.client.get_waiter('table_exists').wait(TableName=table_name)
Add Comment
Please, Sign In to add comment