Skip to content

Instantly share code, notes, and snippets.

@jdahlin
Last active August 2, 2023 10:47
Show Gist options
  • Save jdahlin/fadf504a25470caceeeae8108a28dc5c to your computer and use it in GitHub Desktop.
Save jdahlin/fadf504a25470caceeeae8108a28dc5c to your computer and use it in GitHub Desktop.
Script to use batch-write-item to copy a dynamodb table from one account to another
#!/bin/bash
# Requires: aws cli and jq
set -eux
: ${OLD_TABLE:old_table}
: ${OLD_PROFILE:prod}
: ${NEW_TABLE=new_table}
: ${NEW_PROFILE:dev}
: ${REGION:eu-west-1}
COUNT=$(aws --region=$REGION \
--profile=$OLD_PROFILE \
dynamodb scan --table-name $OLD_TABLE --select "COUNT" | jq -r ".Count")
echo $COUNT
for i in `seq 0 25 $COUNT`; do
aws \
--region=$REGION \
--profile=dpp-planttofield-prod \
dynamodb scan --table-name $OLD_TABLE \
--select ALL_ATTRIBUTES \
--page-size 500 \
--max-items 100000 \
--output json | jq -r "{ \"$NEW_TABLE\": [{ \"PutRequest\": { \"Item\": .Items[]}}][$i:$((i+25))]}" > $i.json
aws \
--region=$REGION \
--profile=dpp-planttofield-dev \
dynamodb batch-write-item --request-items file://$i.json
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment