我是比较新的JavaScript,所以我道歉,如果这是一个明显的问题。这么说,我还没有发现审查其他职位后的答案,所以这里有云:提取数据从嵌套对象 - AWS ec2.describeInstances在node.js中
我想提取有关使用describeInstances我的EC2实例从AWS的具体信息。具体来说,我希望能够提供InstanceIds列表并从结果对象中提取带有键的标签的值:“Name”。这里是基本代码:
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Load credentials and set region from JSON file
AWS.config.loadFromPath('./.aws/config.json');
// Create EC2 service object
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
var params = {
DryRun: false,
InstanceIds: ['i-0be50217a4028a044', 'i-08b83c1c428e9a1d2']
};
ec2.describeInstances(params, function(err, data) {
if (err) {
console.log("Error", err.stack);
} else {
console.log("Success", JSON.stringify(data));
}
});
在运行此代码,就返回一个大的,多毛的和嵌套对象。这样做的JSON.stringify()版本如下所示:
{
\t "Reservations": [{
\t \t "ReservationId": "r-04e32387e546387ba",
\t \t "OwnerId": "543800113692",
\t \t "Groups": [],
\t \t "Instances": [{
\t \t \t "InstanceId": "i-08b83c1c428e9a1d2",
\t \t \t "ImageId": "ami-8aa998ea",
\t \t \t "State": {
\t \t \t \t "Code": 16,
\t \t \t \t "Name": "running"
\t \t \t },
\t \t \t "PrivateDnsName": "ip-10-77-113-210.us-west-2.compute.internal",
\t \t \t "PublicDnsName": "ec2-35-165-200-222.us-west-2.compute.amazonaws.com",
\t \t \t "StateTransitionReason": "",
\t \t \t "KeyName": "Security1",
\t \t \t "AmiLaunchIndex": 0,
\t \t \t "ProductCodes": [],
\t \t \t "InstanceType": "t2.micro",
\t \t \t "LaunchTime": "2017-02-14T14:59:11.000Z",
\t \t \t "Placement": {
\t \t \t \t "AvailabilityZone": "us-west-2b",
\t \t \t \t "GroupName": "",
\t \t \t \t "Tenancy": "default"
\t \t \t },
\t \t \t "Monitoring": {
\t \t \t \t "State": "disabled"
\t \t \t },
\t \t \t "SubnetId": "subnet-228da755",
\t \t \t "VpcId": "vpc-af0f0dca",
\t \t \t "PrivateIpAddress": "10.77.113.210",
\t \t \t "PublicIpAddress": "35.165.200.222",
\t \t \t "Architecture": "x86_64",
\t \t \t "RootDeviceType": "ebs",
\t \t \t "RootDeviceName": "/dev/sda1",
\t \t \t "BlockDeviceMappings": [{
\t \t \t \t "DeviceName": "/dev/sda1",
\t \t \t \t "Ebs": {
\t \t \t \t \t "VolumeId": "vol-00e55d6bf114bfcaa0",
\t \t \t \t \t "Status": "attached",
\t \t \t \t \t "AttachTime": "2017-02-09T15:37:34.000Z",
\t \t \t \t \t "DeleteOnTermination": true
\t \t \t \t }
\t \t \t }],
\t \t \t "VirtualizationType": "hvm",
\t \t \t "ClientToken": "vOiiS1486654656072",
\t \t \t "Tags": [{
\t \t \t \t "Key": "Name",
\t \t \t \t "Value": "Fenris"
\t \t \t }],
\t \t \t "SecurityGroups": [{
\t \t \t \t "GroupName": "launch-wizard-2",
\t \t \t \t "GroupId": "sg-2312072c"
\t \t \t }],
\t \t \t "SourceDestCheck": true,
\t \t \t "Hypervisor": "xen",
\t \t \t "EbsOptimized": false
\t \t }]
\t }, {
\t \t "ReservationId": "r-0bbcb12e5c1162c23",
\t \t "OwnerId": "543800113692",
\t \t "Groups": [],
\t \t "Instances": [{
\t \t \t "InstanceId": "i-0be50217a40028a044",
\t \t \t "ImageId": "ami-8ba011ea",
\t \t \t "State": {
\t \t \t \t "Code": 80,
\t \t \t \t "Name": "stopped"
\t \t \t },
\t \t \t "PrivateDnsName": "ip-10-77-118-17.us-west-2.compute.internal",
\t \t \t "PublicDnsName": "",
\t \t \t "StateTransitionReason": "User initiated (2016-12-05 16:49:45 GMT)",
\t \t \t "KeyName": "Security3",
\t \t \t "AmiLaunchIndex": 0,
\t \t \t "ProductCodes": [],
\t \t \t "InstanceType": "t2.medium",
\t \t \t "LaunchTime": "2016-12-02T15:50:08.000Z",
\t \t \t "Placement": {
\t \t \t \t "AvailabilityZone": "us-west-2b",
\t \t \t \t "GroupName": "",
\t \t \t \t "Tenancy": "default"
\t \t \t },
\t \t \t "Monitoring": {
\t \t \t \t "State": "disabled"
\t \t \t },
\t \t \t "SubnetId": "subnet-228da700",
\t \t \t "VpcId": "vpc-af0f1ccb",
\t \t \t "PrivateIpAddress": "10.77.118.17",
\t \t \t "StateReason": {
\t \t \t \t "Code": "Client.UserInitiatedShutdown",
\t \t \t \t "Message": "Client.UserInitiatedShutdown: User initiated shutdown"
\t \t \t },
\t \t \t "Architecture": "x86_64",
\t \t \t "RootDeviceType": "ebs",
\t \t \t "RootDeviceName": "/dev/sda1",
\t \t \t "BlockDeviceMappings": [{
\t \t \t \t "DeviceName": "/dev/sda1",
\t \t \t \t "Ebs": {
\t \t \t \t \t "VolumeId": "vol-1c211ac8",
\t \t \t \t \t "Status": "attached",
\t \t \t \t \t "AttachTime": "2016-11-22T01:54:52.000Z",
\t \t \t \t \t "DeleteOnTermination": true
\t \t \t \t }
\t \t \t }],
\t \t \t "VirtualizationType": "hvm",
\t \t \t "ClientToken": "RQbhg1479762230132",
\t \t \t "Tags": [{
\t \t \t \t "Key": "Name",
\t \t \t \t "Value": "Heimdall"
\t \t \t }, {
\t \t \t \t "Key": "Type",
\t \t \t \t "Value": "Product Dev"
\t \t \t }],
\t \t \t "SecurityGroups": [{
\t \t \t \t "GroupName": "LinuxAPIdev",
\t \t \t \t "GroupId": "sg-5ea11777"
\t \t \t }],
\t \t \t "SourceDestCheck": true,
\t \t \t "Hypervisor": "xen",
\t \t \t "EbsOptimized": false
\t \t }]
\t }]
}
这是方式更多信息比我更需要或想要的。我想找到一种方法来仅从Reservations.Instances.Tags.Name键中获取Reservations.Instances.Tags.Value的值。
我认为只要这样写就行。但奇怪的是,我似乎无法访问Reservations.Instances对象都:
// Load the SDK for JavaScript
var AWS = require('aws-sdk');
// Load credentials and set region from JSON file
AWS.config.loadFromPath('./.aws/config.json');
// Create EC2 service object
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
var params = {
DryRun: false,
InstanceIds: ['i-0be5987a41191a044', 'i-08b83c3fc28e9a1d2']
};
// call EC2 to retrieve policy for selected bucket
ec2.describeInstances(params, function(err, data) {
if (err) {
console.log("Error", err.stack);
} else {
console.log("Success", JSON.stringify(data.Reservations.Instances));
}
});
这导致:
Success undefined
上午什么我做错了?如何访问实例中较低级别的数据?很显然,它显示在JSON.stringify()中,但显然我没有正确的协议来提取它。
在此先感谢您的帮助。
(PS由于AWS凭证,你将不能够不小的改动我的代码运行。你需要引用您自己的证书,并从自己的EC2实例InstanceIds。)
Whaddaya知道。完美的作品。谢谢。 – rucker
欢迎你 –