You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jq解析AWS CLI输出获取目标组注销延迟超时值

提取AWS目标组注销延迟超时时间的正确jq命令

问题背景

执行AWS CLI命令获取目标组属性:

aws elbv2 describe-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

返回的JSON结构如下:

{
    "Attributes": [
        {
            "Value": "false",
            "Key": "stickiness.enabled"
        },
        {
            "Value": "300",
            "Key": "deregistration_delay.timeout_seconds"
        },
        {
            "Value": "lb_cookie",
            "Key": "stickiness.type"
        },
        {
            "Value": "86400",
            "Key": "stickiness.lb_cookie.duration_seconds"
        },
        {
            "Value": "0",
            "Key": "slow_start.duration_seconds"
        }
    ]
}

此前通过数组索引jq -r '.Attributes[1].Value'提取deregistration_delay.timeout_seconds对应的Value,但部分目标组中该属性的位置会变化,导致命令失效,需要不受位置影响的提取方式。

解决方案

使用jq的select()函数根据Key值精准筛选目标属性,无论其在数组中的位置如何都能正确提取。

完整命令:

aws elbv2 describe-target-group-attributes \
    --target-group-arn arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/73e2d6bc24d8a067 \
| jq -r '.Attributes[] | select(.Key == "deregistration_delay.timeout_seconds").Value'

命令说明

  • .Attributes[]:遍历Attributes数组中的每一个属性对象
  • select(.Key == "deregistration_delay.timeout_seconds"):筛选出Key匹配目标属性名的对象
  • .Value:提取该对象对应的Value字段
  • -r:输出不带引号的原始字符串,方便后续直接使用

内容的提问来源于stack exchange,提问作者cloudbud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 10:57:41