Created
April 8, 2023 09:29
-
-
Save sasasin/265a06e4e1dc356dcb54cbde296c822e to your computer and use it in GitHub Desktop.
AWS Amplify Hosting の任意の年月日のアクセスログをダウンロードしてS3にアップするやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -e | |
| set -o pipefail | |
| # AWS Amplify Hosting の任意の年月日のアクセスログをダウンロードしてS3にアップするやつ。 | |
| # 1個2個なら app id や domain name を直接指定してもいいけど、数が多いと面倒なので、 | |
| # app id と domain name のリストを作って、ループで回して全部取ってくる。 | |
| # このシェルスクリプトを毎日1回 cron で回せば毎日のアクセスログを取得できる。 | |
| # 本音は CloudFront みたくポチポチしたらS3に勝手に出るようになってほしい。 | |
| # この S3 バケットのパスにアップロードする | |
| S3_BUCKET_NAME_PATH=$1 | |
| # 何日前のログを取得するか正の整数で指定する | |
| START_DAYS_AGO=$2 | |
| # Amplify hosting のアクセスログを取得する範囲 | |
| START_TIME=$(gdate -d "$START_DAYS_AGO days ago" "+%Y-%m-%dT00:00:00") | |
| END_TIME=$(gdate -d "$START_DAYS_AGO days ago" "+%Y-%m-%dT23:59:59") | |
| # Athena で日付範囲でパーティション射影を効かせるため、S3のパスに日付を含める | |
| START_DATE=$(gdate -d "$START_DAYS_AGO days ago" "+%Y/%m/%d") | |
| echo $S3_BUCKET_NAME_PATH | |
| echo $START_DAYS_AGO | |
| echo $START_TIME | |
| echo $END_TIME | |
| echo $START_DATE | |
| # get amplify hosting app id and domain | |
| rm -f amplify.list | |
| touch amplify.list | |
| aws amplify list-apps \ | |
| --no-cli-pager \ | |
| | jq -r '.apps[] | [.appId, .defaultDomain] | @tsv' \ | |
| | sed -e 's/"//g' \ | |
| | while read AMPLIFY_APP_ID AMPLIFY_DEFAULT_DOMAIN; do | |
| # Default Domain | |
| echo "$AMPLIFY_APP_ID $AMPLIFY_DEFAULT_DOMAIN" >> amplify.list | |
| # Custom Domain | |
| aws amplify list-domain-associations \ | |
| --app-id $AMPLIFY_APP_ID \ | |
| | jq -r '.domainAssociations[].domainName' \ | |
| | while read AMPLIFY_DOMAIN_NAME; do | |
| echo "$AMPLIFY_APP_ID $AMPLIFY_DOMAIN_NAME" >> amplify.list | |
| done | |
| done | |
| cat amplify.list | |
| # download access logs | |
| mkdir tmp | |
| cat amplify.list \ | |
| | while read AMPLIFY_APP_ID AMPLIFY_DOMAIN_NAME; do | |
| # https://awscli.amazonaws.com/v2/documentation/api/latest/reference/amplify/generate-access-logs.html | |
| aws amplify generate-access-logs \ | |
| --app-id $AMPLIFY_APP_ID \ | |
| --domain-name $AMPLIFY_DOMAIN_NAME \ | |
| --start-time $START_TIME \ | |
| --end-time $END_TIME \ | |
| --query logUrl \ | |
| | xargs curl \ | |
| --retry 5 --retry-delay 3 --retry-connrefused \ | |
| -sS \ | |
| -o tmp/$AMPLIFY_APP_ID.$AMPLIFY_DOMAIN_NAME.csv | |
| done | |
| # compress access logs | |
| gzip tmp/*.csv | |
| # upload access logs | |
| # Athena で日付範囲でパーティション射影を効かせるため、S3のパスに日付を含める | |
| cat amplify.list \ | |
| | while read AMPLIFY_APP_ID AMPLIFY_DOMAIN_NAME; do | |
| aws s3 cp \ | |
| tmp/$AMPLIFY_APP_ID.$AMPLIFY_DOMAIN_NAME.csv.gz \ | |
| s3://$S3_BUCKET_NAME_PATH/$AMPLIFY_APP_ID/$AMPLIFY_DOMAIN_NAME/$START_DATE/$AMPLIFY_APP_ID.$AMPLIFY_DOMAIN_NAME.csv.gz | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment