DEV Community

Shuichi
Shuichi

Posted on

Search for a keyword across all AWS Lambda functions with zipgrep

How to

  1. Download all AWS Lambda functions as ZIP files.
  2. Search for a keyword in the ZIP files.

Use case

I used it to check for Lambda containing specific keywords in a chaotic verification account that includes many direct-written Lambda not managed in the version control.

1. Download all AWS Lambda functions as ZIP files

Use this excellent script.

nemani/download_all_lambda_functions.sh

Prerequisites

Usage

# This block is not necessary if you set a default profile
export AWS_DEFAULT_PROFILE=xxxxx
export AWS_DEFAULT_REGION=ap-northeast-1
export AWS_DEFAULT_OUTPUT=text

# Run
bash download_all_lambda_functions.sh 
Enter fullscreen mode Exit fullscreen mode

Note

Script says, "Completed Downloading all the Lamdba Functions!" but just threw the task in the background, so it hasn't finished.
No signal is said when all background tasks are finished.
So, I considered it complete when the download task stops and the number of zip files matches the functions.

Output result

A zip file for each function is downloaded in lambda_functions/*.

Note

It's a bit annoying to get them by region.
I recommend separating the download destination folder for each region.

2. Search for a keyword in the ZIP files

Example usage

Search for foobar

cd lambda_functions

for file in *.zip; do
    res=`zipgrep -n foobar $file`
    if [ -n "$res" ]; then
        echo "x:$file";
        echo "$res";
    else
        echo "o:$file";
    fi
done
Enter fullscreen mode Exit fullscreen mode

Example output results

x:aaaa-checker.zip
lambda_function.py:30: target = "foobar",
o:bbbbb-deleter.zip
o:ccccc-notifier.zip
x:ddddd-test.zip
lambda_function.py:21: print("foobar"),
...
Enter fullscreen mode Exit fullscreen mode

Remarks

Another way that didn't work

find . /lambda_functions -name '*.zip' | xargs -I file zipgrep foobar file
Enter fullscreen mode Exit fullscreen mode

This command was insufficient for me because it did not know the ZIP where the keyword was detected.

To take time

If Lambda comes with libraries and .env environment, grep will take a long time.

Discussion (0)