如何使用最新版本的 NodeJS AWS SDK 启动 EC2 实例?

How do I start an EC2 instance using the latest version of the NodeJS AWS SDK?

我正在尝试在 Discord.JS 中制作一个简单的 Discord 机器人,它在命令为 运行 时打开 EC2 实例。我已经完成了大部分工作,但我似乎无法通过这一部分代码。我注意到它一直告诉我我没有启动 EC2 实例的权限,即使它在 root 用户上 运行ning(我知道安全性很差,我打算很快移动它)这是写在代码中的(取自文档),但我不确定是什么导致我没有权限。

这是我正在使用的代码

        if (message.content === ">start") {
// Load the AWS SDK for Node.js
            var AWS = require('aws-sdk');
// Set the region
            AWS.config.update({region: 'REGION'});

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

            var params = {
                InstanceIds: "i-0c5de602d730d1d24",
                DryRun: true
            };
         // Call EC2 to start the selected instances
                ec2.startInstances(params, function(err, data) {
                    if (err && err.code === 'DryRunOperation') {
                        params.DryRun = false;
                        ec2.startInstances(params, function(err, data) {
                            if (err) {
                                console.log("Error", err);
                            } else if (data) {
                                console.log("Success", data.StartingInstances);
                            }
                        });
                    } else {
                        console.log("You don't have permission to start instances.");
                    }
                });
            }
});
} catch (error) {
    console.error(error);
}

感谢阅读,希望能从中吸取教训,以后不会遇到类似的问题!

尝试

InstanceIds: ["i-0c5de602d730d1d24"],