This can happen if you are executing this query from different region than database's instance or from local.
If you haven't uploaded file with this query to the same region as your dynamoDb then it will take some time to build connection with your DB instance. Try to upload this script on the same region's server as your DB and then try again.