Last active
March 10, 2026 15:30
-
-
Save vilasmaciel/add293657c9dd35bfff22e3266852384 to your computer and use it in GitHub Desktop.
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
| cwtail() { | |
| local _dim='\033[2m' _green='\033[32m' _yellow='\033[33m' _red='\033[31m' | |
| local _cyan='\033[36m' _bold='\033[1m' _reset='\033[0m' | |
| local time="" filter="" | |
| if ! [ -z $1 ] && ! [ -z $2 ] ; | |
| then | |
| filter=$1 | |
| time=$2 | |
| fi | |
| if ! [ -z $1 ] && [ -z $2 ] && [[ $1 =~ ^--since ]]; | |
| then | |
| time=$1 | |
| else | |
| filter=$1 | |
| fi | |
| # --- Step 1: Fetch log groups (auto-login on SSO expiry) --- | |
| local query | |
| if [ -z $filter ]; then | |
| printf "${_dim}[1/2]${_reset} Fetching log groups...\n" | |
| query="logGroups[*].logGroupName" | |
| else | |
| printf "${_dim}[1/2]${_reset} Fetching log groups matching ${_cyan}${filter}${_reset}...\n" | |
| query="logGroups[?contains(logGroupName, '${filter}')].logGroupName" | |
| fi | |
| local output | |
| output=$(aws logs describe-log-groups --query "$query" 2>&1) | |
| if echo "$output" | grep -qi "SSO.*expired\|SSO.*invalid\|Token has expired"; then | |
| printf "${_yellow} SSO session expired.${_reset} Launching login...\n" | |
| aws sso login | |
| if [ $? -ne 0 ]; then | |
| printf "${_red} Login failed. Aborting.${_reset}\n" | |
| return 1 | |
| fi | |
| printf "${_green} SSO session restored.${_reset} Retrying...\n" | |
| output=$(aws logs describe-log-groups --query "$query" 2>&1) | |
| fi | |
| local selected | |
| selected=$(echo "$output" | jq -r '.[]' 2>/dev/null | fzf --height 60% --reverse) | |
| if [ -z "$selected" ]; then | |
| printf "${_yellow} No log group selected.${_reset}\n" | |
| return 0 | |
| fi | |
| # --- Step 2: Tail logs --- | |
| printf "${_dim}[2/2]${_reset} Tailing ${_bold}${selected}${_reset}\n" | |
| printf "${_dim}> aws logs tail ${selected} --follow --format short ${time} ${3}${_reset}\n" | |
| aws logs tail $selected --follow --format short $time $3 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Too for consuming CloudWatch logs.
Dependencies:
brew install fzfbrew tap lucagrulla/tap & brew install cwInstallation:
.zshrcor similarUsage: