2017-02-14 72 views
0

我是比較新的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。)

回答

2

ReservationsInstancesTags全部是數組是「的原因是:

Reservations[0].Instances[0].Tags[0].Value 
// value of 1ˢᵗ tag of 1ˢᵗ instance of 1ˢᵗ reservation 

和不

Reservations.Instances.Tags.Value 
+0

Whaddaya知道。完美的作品。謝謝。 – rucker

+0

歡迎你 –

1

在你的describeInstances()方法,你可以簡單地使用

// for InstanceId : "i-08b83c1c428e9a1d2" 
data.Reservations[0].Instances[0].Tags[0].Value; 
// and for InstanceId : "i-0be50217a40028a044" 
data.Reservations[1].Instances[0].Tags[0].Value; 

您仍然可以創建一個功能,如果需要的話,如果對象結構是已知的返回值的標籤。