Shell Script Create DynamoDB Tables
# how to run
# dynamodb-scripts/create-device-dynamo-tables.sh {ENV} {WriteCapacityUnits} {ReadCapacityUnits}
# eg: dynamodb-scripts/create-device-dynamo-tables.sh int 5 5
echo "Start to create the DynamoDb table"
aws dynamodb create-table --table-name sillycat_device-$1-devicePairingInfo \
--key-schema AttributeName=serialNumber,KeyType=HASH \
--global-secondary-indexes \
IndexName=groupUUID-index,KeySchema=["{AttributeName=groupUUID,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
IndexName=pairingToken-index,KeySchema=["{AttributeName=pairingToken,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
IndexName=deviceUUID-index,KeySchema=["{AttributeName=deviceUUID,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
--attribute-definitions AttributeName=serialNumber,AttributeType=S AttributeName=groupUUID,AttributeType=S AttributeName=pairingToken,AttributeType=S AttributeName=deviceUUID,AttributeType=S \
--provisioned-throughput WriteCapacityUnits=$2,ReadCapacityUnits=$3
echo "Wait for the table creation finish on AWS"
aws dynamodb wait table-exists --table-name sillycat_device-$1-devicePairingInfo
echo "Add TTL column to that table"
aws dynamodb update-time-to-live --table-name sillycat_device-$1-devicePairingInfo --time-to-live-specification Enabled=true,AttributeName=pairingCodeDeleteTime
echo "Creation is done. Enjoy it."
There are several important parts in this script.
wait, we can wait until the table exists in the AWS. That is very useful.
ttl, we can set up ttl for one column in the table
GSI, we can create other GSI in this table.
References:
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-time-to-live.html
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/wait/index.html
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/wait/table-exists.html
https://aws.amazon.com/cn/blogs/aws/new-manage-dynamodb-items-using-time-to-live-ttl/
# how to run
# dynamodb-scripts/create-device-dynamo-tables.sh {ENV} {WriteCapacityUnits} {ReadCapacityUnits}
# eg: dynamodb-scripts/create-device-dynamo-tables.sh int 5 5
echo "Start to create the DynamoDb table"
aws dynamodb create-table --table-name sillycat_device-$1-devicePairingInfo \
--key-schema AttributeName=serialNumber,KeyType=HASH \
--global-secondary-indexes \
IndexName=groupUUID-index,KeySchema=["{AttributeName=groupUUID,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
IndexName=pairingToken-index,KeySchema=["{AttributeName=pairingToken,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
IndexName=deviceUUID-index,KeySchema=["{AttributeName=deviceUUID,KeyType=HASH}"],Projection={ProjectionType=ALL},ProvisionedThroughput="{WriteCapacityUnits=$2,ReadCapacityUnits=$3}" \
--attribute-definitions AttributeName=serialNumber,AttributeType=S AttributeName=groupUUID,AttributeType=S AttributeName=pairingToken,AttributeType=S AttributeName=deviceUUID,AttributeType=S \
--provisioned-throughput WriteCapacityUnits=$2,ReadCapacityUnits=$3
echo "Wait for the table creation finish on AWS"
aws dynamodb wait table-exists --table-name sillycat_device-$1-devicePairingInfo
echo "Add TTL column to that table"
aws dynamodb update-time-to-live --table-name sillycat_device-$1-devicePairingInfo --time-to-live-specification Enabled=true,AttributeName=pairingCodeDeleteTime
echo "Creation is done. Enjoy it."
There are several important parts in this script.
wait, we can wait until the table exists in the AWS. That is very useful.
ttl, we can set up ttl for one column in the table
GSI, we can create other GSI in this table.
References:
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/update-time-to-live.html
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/wait/index.html
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/wait/table-exists.html
https://aws.amazon.com/cn/blogs/aws/new-manage-dynamodb-items-using-time-to-live-ttl/