Skip to content

Instantly share code, notes, and snippets.

@gautamdudeja90
Created August 17, 2020 18:19
Show Gist options
  • Save gautamdudeja90/0675abba09626b035595174b018a56f7 to your computer and use it in GitHub Desktop.
Save gautamdudeja90/0675abba09626b035595174b018a56f7 to your computer and use it in GitHub Desktop.
# update-dynamo-autoscale-settings
import os
import sys
import boto3
from botocore.exceptions import ClientError
# Add path for additional imports
sys.path.append('./lib/python3.7/site-packages')
# Initialize boto3 clients
dynamodb = boto3.resource('dynamodb')
dynamodb_scaling = boto3.client('application-autoscaling')
# Initialize variables
table_name = "your dynamo table name"
table = dynamodb.Table(table_name)
def update_auto_scale_settings(min_write_capacity: int, table_name: str):
max_write_capacity = 40000 #default number
dynamodb_scaling.register_scalable_target(ServiceNamespace = "dynamodb",
ResourceId = "table/{}".format(table_name),
ScalableDimension = "dynamodb:table:WriteCapacityUnits",
MinCapacity = min_write_capacity,
MaxCapacity = max_write_capacity)
# if you have indexes on the table, add their names to indexes list below
# indexes = ["index1", "index2", "index3"]
# for index_name in indexes:
# scaling_dynamodb.register_scalable_target(ServiceNamespace = "dynamodb",
# ResourceId = "table/{table_name}/index/{index_name}".format(table_name = table_name, index_name = index_name),
# ScalableDimension = "dynamodb:index:WriteCapacityUnits",
# MinCapacity = min_write_capacity,
# MaxCapacity = max_write_capacity)
#put_scaling policy is another call that needs to be made, if you want a different target utilization value.
def lambda_handler(event, context):
try:
# logic before updating
write_units = #number
update_auto_scale_settings(table_name,write_units)
# logic after updating
# Insert item into dynamo
# table.put_item(Item = record)
except Exception as e:
raise e
else:
print("success")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment