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

使用json-query遍历Terraform状态数据获取ami值失败排查

问题:使用json-query提取Terraform状态中aws_instance的AMI值失败?

我编写了如下JavaScript代码,想要使用json-query库遍历Terraform状态数据对象中的resources,提取所有aws_instance类型资源的ami值,但当前查询返回result.value为null,无法得到预期的["ami-094axxxx","ami-0443c"]结果,不清楚问题出在哪里。

现有代码

var jsonQuery = require('json-query');

var data = {
    "version": 3,
    "terraform_version": "0.12.31",
    "serial": 1,    
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "aws_instance_tfer--i-0022xxxxxxxxeaf_Oracle-Dev_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-0022xxx"
                },
                "aws_instance_tfer--i-0100xxx_SRE-AMPP02_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01000xxx"
                },
                "aws_instance_tfer--i-0142545fe3066b603_test-windows-agent_id": {
                    "sensitive": false,
                    "type": "string",
                    "value": "i-01425xxxxx3"
                }
            },
            "resources": {
                "aws_instance.tfer--i-0022xxxxxaf_Oracle-Dev-CMS": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-00221xxxxx",
                        "attributes": {
                            "ami": "ami-094axxxx",
                            "arn": "arn:aws",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east",
                            "vpc_security_group_ids.0": "sg-0284ccc"
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                },
                "aws_instance.tfer--i-0100xxxx_SRE-AMVAPP02": {
                    "type": "aws_instance",
                    "depends_on": [],
                    "primary": {
                        "id": "i-010001xxxxx",
                        "attributes": {
                            "ami": "ami-0443c",
                            "arn": "arn:aws4",
                            "associate_public_ip_address": "false",
                            "availability_zone": "us-east"    
                        },
                        "meta": {
                            "schema_version": 1
                        },
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }
            },
            "depends_on": []
        }
    ]
}


var result = jsonQuery('modules[0].resources[*type=aws_instance].primary.attributes.ami', {data: data});


console.log(result.value);

预期结果

[
  "ami-094axxxx",
  "ami-0443c"
]

问题原因与解决方法

问题出在json-query的语法使用错误:你用的[*type=aws_instance]是数组遍历过滤的语法,但resources是一个对象(不是数组),所以需要先遍历对象的所有属性,再过滤类型匹配的资源。

正确的查询语法应该是:

var result = jsonQuery('modules[0].resources.*[type=aws_instance].primary.attributes.ami', {data: data});

其中:

  • resources.*:遍历resources对象下的所有键值对
  • [type=aws_instance]:过滤出type属性等于aws_instance的资源
  • 后续的.primary.attributes.ami即可提取目标AMI值

替换查询语句后,运行代码就能得到预期的数组结果。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 01:52:20