Skip to content

Instantly share code, notes, and snippets.

@StephenKing
Created February 7, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StephenKing/48c10e7483885d1767d6bc6546553209 to your computer and use it in GitHub Desktop.
Save StephenKing/48c10e7483885d1767d6bc6546553209 to your computer and use it in GitHub Desktop.
CloudTrail Athena: Create Partitions
#!/bin/bash
# creates Athena partitions for cloudtrail logs (per account, region, year, month)
# usage:
# ./create-partition.sh my-cloudtrail-bucket 1234566 | pbcopy
BUCKET=$1
ACCOUNT=$2
YEAR=$(date +'%Y')
echo "ALTER TABLE cloudtrail_logs ADD IF NOT EXISTS"
for REGION in $(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text); do
for MONTH in $(seq -w 1 12); do
echo "PARTITION (account='$ACCOUNT', region='$REGION', year='$YEAR', month='$MONTH') LOCATION 's3://$BUCKET/AWSLogs/$ACCOUNT/CloudTrail/$REGION/$YEAR/$MONTH/'"
done
done
echo ";"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment