Skip to content

Instantly share code, notes, and snippets.

@jamesoff
Created March 10, 2020 21:32
Show Gist options
  • Save jamesoff/f7f9d4bda7a283219f647e7073e72a75 to your computer and use it in GitHub Desktop.
Save jamesoff/f7f9d4bda7a283219f647e7073e72a75 to your computer and use it in GitHub Desktop.
Grep your Lambda functions for a string: https://twitter.com/esh/status/1237436147409666048
#!/usr/bin/env bash
#MIT No Attribution
#
#Copyright 2020 James Seward
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this
#software and associated documentation files (the "Software"), to deal in the Software
#without restriction, including without limitation the rights to use, copy, modify,
#merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Grep your Lambda functions for a string. If ripgrep is installed, uses that.
set -e
needle=${1?Missing needle param}
if rg --help >/dev/null 2>&1; then
grep_command=rg
else
grep_command="grep -r"
fi
function_names=$( aws lambda list-functions --output text --query 'Functions[].FunctionName' )
for f in $function_names; do
url=$( aws lambda get-function --function-name "$f" --output text --query 'Code.Location' )
filename="$f.zip"
[[ -f $filename ]] && rm -f "$filename"
curl -s -o "$filename" "$url"
[[ -d $f ]] && rm -rf "$f"
mkdir "$f"
unzip -qq "$filename" -d "$f/"
$grep_command "$needle" "$f/" || true
done
@ehammond
Copy link

Maybe avoid overwriting pre-existing zip files and subdirectories by using a temp directory?

tmpdir=$(mktemp -d /tmp/lambda-grep-XXXXXX)
cd $tmpdir

...

rm -r $tmpdir

@jamesoff
Copy link
Author

Good call. I did consider also testing for the existence of the file before bothering to download it :)

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