如何将信息直接从 EC2 输出到文件(例如 .txt 或 .json)?

How do you output information directly from EC2 to a file (such as .txt or .json)?

我想将安全组规则从 EC2 导出到一种文件类型(例如 .txt 或 .json)。我正在使用 javascript/node.js 来生成此输出。不幸的是,我测试了它无济于事。

我开发了一个代码,可以让我输出安全组(规则、入口、出口等),同时将它们放入文本文件中。我会先将 SDK 加载到我的 javascript 文件中并设置区域。然后,我在检索信息之前创建服务对象。

这是我用于此实验的代码:

// Load the SDK for JavaScript
var AWS = require('aws-sdk');

// Set the region 
AWS.config.update({region: 'us-east-1'});

var fs = require('fs');

// Create EC2 service object
var ec2 = new AWS.EC2({apiVersion: '2016-11-15'});

// Retrieve security group descriptions

ec2.describeSecurityGroups(function(err, data) {
    if (err) {
        console.log("Failed to retrieve information", err);
    }
    var sGroups = data.SecurityGroups;
    console.log("Your input has been confirmed!", JSON.stringify(sGroups));

    fs.writeFile('AWSnodeTest.txt', sGroups, function(err, result) {
        if(err) console.log("Failed to output into file", err);
    });
});

当我通过 PowerShell 测试实验时,一开始它很完美。来自安全组的所有信息都很好地显示在 PowerShell 上。但是,基于:

fs.writeFile('AWSnodeTest.txt', sGroups, function(err, result) {
    if(err) console.log("Failed to output into file", err);
});

我只收到提到的文件的以下输出:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

看来帮助我编写文件的那段代码有问题。我需要什么函数才能通过 javascript 将 EC2 安全组检索到文件中?

您正在传递变量 sGroups 作为要写入文件的内容,而不是 JSON 序列化字符串。 我还冒昧地添加了一个 return 语句以防出错,请参阅下面更新的代码:

ec2.describeSecurityGroups(function(err, data) {
    if (err) {
        console.log("Failed to retrieve information", err);
        return;  // Added this as there's no point in continuing.
    }
    var sGroupsJSON = JSON.stringify(data.SecurityGroups);
    console.log("Your input has been confirmed!", sGroupsJSON);

    fs.writeFile('AWSnodeTest.txt', sGroupsJSON, function(err, result) {
        if(err) console.log("Failed to output into file", err);
    });
});

希望对您有所帮助。