Skip to content

Instantly share code, notes, and snippets.

@vilasmaciel
Last active March 10, 2026 15:30
Show Gist options
  • Select an option

  • Save vilasmaciel/add293657c9dd35bfff22e3266852384 to your computer and use it in GitHub Desktop.

Select an option

Save vilasmaciel/add293657c9dd35bfff22e3266852384 to your computer and use it in GitHub Desktop.
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
}
@vilasmaciel
Copy link
Copy Markdown
Author

vilasmaciel commented Nov 9, 2022

Too for consuming CloudWatch logs.

Dependencies:

  • fzf: brew install fzf
  • cw: brew tap lucagrulla/tap & brew install cw

Installation:

  1. Save the function to your .zshrc or similar
  2. Check that you have set correctly your aws credentials
  3. Install dependencies
  4. Enjoy

Usage:

cwtail - list all log sources
cwtail <word> - list all log sources filtering with the given word
cwtail | grep <word> - apply the grep command to the sected log source
cwtail -b1h - return logs from 1 hour ago

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment