Skip to content

Instantly share code, notes, and snippets.

@iconara
Created November 10, 2020 13:37
Show Gist options
  • Save iconara/447a569d00a7a9c4aff86e9e0b14ff11 to your computer and use it in GitHub Desktop.
Save iconara/447a569d00a7a9c4aff86e9e0b14ff11 to your computer and use it in GitHub Desktop.
Run Athena queries with aws-cli
#!/usr/bin/env bash
region=us-east-1
query='SELECT NOW()'
output_location="s3://aws-athena-query-results-1234567890-$region/"
query_execution_id=$(aws athena start-query-execution \
--region "$region" \
--query-string "$query" \
--result-configuration "OutputLocation=$output_location" \
--query QueryExecutionId \
--output text)
while true; do
status=$(aws athena get-query-execution \
--region "$region" \
--query-execution-id "$query_execution_id" \
--query QueryExecution.Status.State \
--output text)
if [[ $status != 'RUNNING' ]]; then
break
else
sleep 5
fi
done
if [[ $status = 'SUCCEEDED' ]]; then
result_location=$(aws athena get-query-execution \
--region "$region" \
--query-execution-id "$query_execution_id" \
--query QueryExecution.ResultConfiguration.OutputLocation \
--output text)
exec aws s3 cp "$result_location" -
else
reason=$(aws athena get-query-execution \
--region "$region" \
--query-execution-id "$query_execution_id" \
--query QueryExecution.Status.StateChangeReason \
--output text)
echo "Query $query_execution_id failed: $reason" 1>&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment