I'm trying to automate the process for updating AWS Lambda layers and the functions that use them. To get a list of functions that use a particular layer I'm parsing the JSON output of the AWS CLI when listing the current functions in my account. Calling aws lambda list-functions
returns a JSON block similar to the example below (I have deliberately removed some irrelevant content to focus on the issue):
{"Functions": [ {"TracingConfig": {"Mode": "PassThrough" },"FunctionArn": "arn:aws:lambda:eu-west-2:000000000000:function:function-1" }, {"Layers": [ {"CodeSize": 11359101,"Arn": "arn:aws:lambda:eu-west-2:000000000000:layer:layer1:12" } ],"TracingConfig": {"Mode": "PassThrough" },"FunctionArn": "arn:aws:lambda:eu-west-2:000000000000:function:function-2" }, {"Layers": [ {"CodeSize": 11359101,"Arn": "arn:aws:lambda:eu-west-2:000000000000:layer:layer1:12" }, {"CodeSize": 11359101,"Arn": "arn:aws:lambda:eu-west-2:000000000000:layer:layer2:5" } ],"TracingConfig": {"Mode": "PassThrough" },"FunctionArn": "arn:aws:lambda:eu-west-2:000000000000:function:function-3" } ]}
In the example above I have three functions defined, two of which make use of at least one layer. What I need to do is get a list of FunctionArn
values for functions that use a specific layer. So far I have been able to filter out the function that doesn't use any layers using this command:
aws lambda list-functions | jq '.Functions[] | select(.Layers)'
What I really need to do is create a select()
statement that can filter inside the "Layers" array from the top level:
aws lambda list-functions | jq '.Functions[] | select(.Layers[] | contains("layer2"))'
https://jqplay.org/s/SiFSE3RxZV
But I keep getting "Cannot iterate over null" error messages that I think are coming from inside the select()
statement?
The plan is to filter the list down to functions that use the particular layer in question, then return the FunctionArn
value for each result for use in my script.