我正在尝试自动化更新AWS Lambda层和使用它们的函数的过程。为了获得使用特定层的函数列表,我在列出帐户中的当前函数时解析了AWS CLI的JSON输出。调用aws lambda list-functions会返回一个类似于下面示例的JSON块(我特意删除了一些不相关的内容来关注这个问题):
{
"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"
}
]
}在上面的示例中,我定义了三个函数,其中两个至少使用了一个层。我需要做的是获取使用特定层的函数的FunctionArn值列表。到目前为止,我已经能够使用以下命令过滤出不使用任何层的函数:
aws lambda list-functions | jq '.Functions[] | select(.Layers)'我真正需要做的是创建一个select()语句,它可以从顶层过滤"Layers“数组:
aws lambda list-functions | jq '.Functions[] | select(.Layers[] | contains("layer2"))'https://jqplay.org/s/SiFSE3RxZV
但是我一直收到"Cannot iterate over null“错误消息,我认为这些错误消息来自select()语句内部?
我们的计划是将列表过滤到使用特定层的函数,然后返回每个结果的FunctionArn值,以便在我的脚本中使用。
发布于 2019-12-01 19:47:20
从Functions的元素中,选择具有值为-an数组的Layers键的元素-在其Arn字段-a FunctionArn中包含至少一个以layer2为子字符串的对象-,并提取它们的字符串。例如:
.Functions[] | select(has("Layers") and any(.Layers[].Arn; index("layer2"))) | .FunctionArnhttps://stackoverflow.com/questions/59124696
复制相似问题