3

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.

0

1 Answer 1

6

From elements of Functions, select ones that have a Layers key whose value -an array- contains at least one object having layer2 as a substring in its Arn field -a string-, and extract their FunctionArns. E.g:

.Functions[] | select(has("Layers") and any(.Layers[].Arn; index("layer2"))) | .FunctionArn
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.