Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search through values of all AWS Lambda function's environment variables in AWS account

I have AWS account with huge amount of AWS Lambda functions and I'd like to check all environment variables of all functions and try to find functions which use some specific values there. How can I to do that without manual checking of each function in AWS console? Does AWS CLI allow that?

like image 553
Gleb Avatar asked Oct 11 '19 13:10

Gleb


People also ask

How do I view Lambda environment variables?

Your function uses the name of the key to retrieve the value of environment variable. Open the Functions page of the Lambda console. Choose a function. Choose Configuration, then choose Environment variables.

Which AWS tools or services can be used to list all AWS Lambda functions running in an account?

You can use the AWS Command Line Interface to manage functions and other AWS Lambda resources. The AWS CLI uses the AWS SDK for Python (Boto) to interact with the Lambda API.

Where are AWS environment variables stored?

To set environment variables Sign in to the AWS Management Console and open the Amplify console . In the Amplify console, choose App Settings, and then choose Environment variables. In the Environment variables section, choose Manage variables. In the Manage variables section, under Variable, enter your key.


2 Answers

Yes the AWS CLI allows you to check your Lambda environment variables. To automate it end to end you'll need to chain some commands together. Also, I defaulted to using jq for the heavy lifting. I'm sure there is a way to do this in JMESPath but I didn't have time to figure it out.

Here's the overview of how it works:

  1. Get a list of Lambda function names by calling list-functions and pipe that list as text to xargs
  2. Take the output from the previous step and use it to get all the Lambda function configuration details by calling get-function-configuration.
  3. Pipe that output to jq, ensure the Environment field is not null and search for the Variable you want. In this case I'm searching for a variable called customer which has a value of shared_services.
  4. Output the Lambda function name.

The code:

aws lambda list-functions --query 'Functions[*].[FunctionName]' --output text | xargs -I {} aws lambda get-function-configuration --function-name {} | jq -r 'select((.Environment) and select(.Environment.Variables.customer == "shared_services"))| .FunctionName'

Output

copy_snaps_shared_services
snapshot_by_customer

References

JQ Manual
JQ Presence of Key before Iterating
AWS CLI Usage Examples
AWS Lambda Get Function Configuration
AWS Lambda List Functions

like image 117
kenlukas Avatar answered Sep 30 '22 19:09

kenlukas


You should be able to use the AWS CLI get-function to get the meta about the Lambda function. The returned meta-data includes a presigned URL for downloading the deployment package. You can donwload the deployment package, unpack it, and search the source code for references to the environment variables you are looking for.

Take a look at this script to see how it can easily be done: https://gist.github.com/nemaniarjun/defdde356b6678352bcd4af69b7fe529

like image 42
Ashaman Kingpin Avatar answered Sep 30 '22 20:09

Ashaman Kingpin