Skip to content

Instantly share code, notes, and snippets.

@guillaumeportes
Created September 5, 2023 13:26
Show Gist options
  • Save guillaumeportes/10626e31b4c7d3238f1c32c89ce317e8 to your computer and use it in GitHub Desktop.
Save guillaumeportes/10626e31b4c7d3238f1c32c89ce317e8 to your computer and use it in GitHub Desktop.
# Create the dynamodb tables
tables=("collection/player-id/card-id" "decks/player-id" "deck/deck-id" "packs/player-id/pack-id")
create_dynamodb_tables() {
echo "Creating dynamodb tables."
for table in "${tables[@]}"; do
# split up table name and attributes
local table_name="${table%%/*}"
local attributes="${table#*/}"
local attribute1="${attributes%/*}"
local attribute2="${attributes#*/}"
# when deploying to the cloud, prefix the table name with the environment one
if [[ "$ecs" = true ]]; then
table_name="$app_cluster"-"$table_name"
fi
# check whether the table already exists
local cmd="aws dynamodb describe-table --region eu-west-1 --table-name $table_name"
if [[ "$aws_region" = "local" || "$aws_region" = "" ]]; then
cmd="$cmd --endpoint-url http://localhost:9000"
fi
local contents=$($cmd)
if [[ "$contents" = "" ]]; then
# if it doesn't, create it
echo "Creating $table_name."
# note: we only support string attributes
cmd="aws dynamodb create-table --region eu-west-1 --table-name $table_name --attribute-definitions AttributeName=$attribute1,AttributeType=S"
if [[ "$attribute1" != "$attribute2" ]]; then
cmd="$cmd AttributeName=$attribute2,AttributeType=S"
fi
cmd="$cmd --key-schema AttributeName=$attribute1,KeyType=HASH"
if [[ "$attribute1" != "$attribute2" ]]; then
cmd="$cmd AttributeName=$attribute2,KeyType=RANGE"
fi
cmd="$cmd --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1"
if [[ "$aws_region" = "local" || "$aws_region" = "" ]]; then
cmd="$cmd --endpoint-url http://localhost:9000"
fi
$cmd > /dev/null
# setup autoscaling
if [[ "$ecs" = true ]]; then
policy_name="$app_cluster"-scaling-policy-"$table"
aws application-autoscaling register-scalable-target --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:WriteCapacityUnits" --min-capacity 1 --max-capacity 100 > /dev/null
aws application-autoscaling register-scalable-target --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:ReadCapacityUnits" --min-capacity 1 --max-capacity 100 > /dev/null
aws application-autoscaling put-scaling-policy --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:WriteCapacityUnits" --policy-name "$policy_name"-write --policy-type "TargetTrackingScaling" --target-tracking-scaling-policy-configuration file://scaling-policy-write.json > /dev/null
aws application-autoscaling put-scaling-policy --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:ReadCapacityUnits" --policy-name "$policy_name"-read --policy-type "TargetTrackingScaling" --target-tracking-scaling-policy-configuration file://scaling-policy-read.json > /dev/null
fi
else
echo "Table $table_name already exists - skipping."
fi
done
}
# Delete the dynamodb tables
delete_dynamodb_tables() {
echo "Deleting dynamodb tables."
for table in "${tables[@]}"; do
table_name="$app_cluster"-"$table"
policy_name="$app_cluster"-scaling-policy-"$table"
local contents=$(aws dynamodb describe-table --region eu-west-1 --table-name "$table_name")
if [[ "$contents" = "" ]]; then
echo "Table $table_name does not exist. Skipping."
else
echo "Deleting $table_name."
aws application-autoscaling deregister-scalable-target --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:ReadCapacityUnits" > /dev/null
aws application-autoscaling deregister-scalable-target --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:WriteCapacityUnits" > /dev/null
aws application-autoscaling delete-scaling-policy --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:ReadCapacityUnits" --policy-name "$policy_name"-read > /dev/null
aws application-autoscaling delete-scaling-policy --service-namespace dynamodb --resource-id "table/$table_name" --scalable-dimension "dynamodb:table:WriteCapacityUnits" --policy-name "$policy_name"-write > /dev/null
aws dynamodb delete-table --region eu-west-1 --table-name "$table_name" > /dev/null
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment