Skip to content

Instantly share code, notes, and snippets.

@masayoshi634
Last active July 3, 2019 06:06
Show Gist options
  • Save masayoshi634/905efd0c49f5d90ad599179f6ec280e0 to your computer and use it in GitHub Desktop.
Save masayoshi634/905efd0c49f5d90ad599179f6ec280e0 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -ex
PROFILE=${AWS_PROFILE:="default"}
REGION=${AWS_REGION:="ap-northeast-1"}
print_help() {
cat <<-EOF >> /dev/stdout
Select and get log for cloudwatch logs
Depends:
awslogs (https://github.com/jorgebastida/awslogs)
peco (https://github.com/peco/peco)
Usage: $0 [-p AWS_PROFILE] [-r AWS_REGION] [-- awslogs_get_options]
Options:
-p [AWS_PROFILE] AWS Profile
-r [AWS_REGION] AWS Region
-h print this message and exit
Example: $0 -p test-account -r ap-northeast-1 -- -S --timestamp --start=10m -w
EOF
}
while getopts "p:r:h" opt; do
case ${opt} in
p ) PROFILE=${OPTARG} ;;
r ) REGION=${OPTARG} ;;
h ) print_help; exit ;;
* ) print_help; exit 1 ;;
esac
done
shift $((OPTIND - 1))
AWSLOGS_ARGS=${*}
echo "awslogs groups --profile="${PROFILE}" --aws-region="${REGION}" | peco | xargs -I%% awslogs get --profile="${PROFILE}" --aws-region="${REGION}" ${AWSLOGS_ARGS} %%"
LOG_GROUP=$(awslogs groups --profile="${PROFILE}" --aws-region="${REGION}" | peco)
echo "awslogs get --profile="${PROFILE}" --aws-region="${REGION}" ${AWSLOGS_ARGS} ${LOG_GROUP}"
awslogs get --profile="${PROFILE}" --aws-region="${REGION}" ${AWSLOGS_ARGS} ${LOG_GROUP}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment