简体   繁体   English

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

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

I am trying to make a simple Discord bot in Discord.JS that turns on an EC2 instance when a command is run.我正在尝试在 Discord.JS 中创建一个简单的 Discord 机器人,该机器人在运行命令时会打开 EC2 实例。 I've gotten most of it to work, but I cannot seem to get past this one part of code.我已经完成了大部分工作,但我似乎无法超越这部分代码。 I've noticed that it keeps on telling me that I don't have permissions to start an EC2 instance even though it's running on the root user (bad security I know, I'm planning to move it soon) which is written in the code (taken from the docs), but I'm not sure what could be causing me to not have the permissions.我注意到它一直告诉我我没有权限启动 EC2 实例,即使它在 root 用户上运行(我知道安全性很差,我打算尽快移动它),它写在代码(取自文档),但我不确定是什么导致我没有权限。

Here is the code I am working with这是我正在使用的代码

        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);
}

Thanks for reading and I hope that I can learn from this so that I don't have similar issues in the future!感谢您的阅读,我希望我能从中吸取教训,这样我以后就不会遇到类似的问题了!

Try尝试

InstanceIds: ["i-0c5de602d730d1d24"],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何部署Node.js并表达到AWS EC2实例 - How to deploy nodejs and express to AWS EC2 Instance 使用适用于Node.js的AWS开发工具包,如何等待EC2实例添加到ECS集群中? - Using AWS SDK for Node.js, how to wait for EC2 instance being added to ECS cluster? 如何在 EC2 AWS 实例中启动 node js 项目 - how to start node js project in a EC2 AWS instance 如何从本地计算机建立与AWS ec2实例的MYSQL连接? - How do I establish MYSQL connection to AWS ec2 instance from local machine? 如何使用Lambda启动和停止EC2实例 - How to start and stop EC2 instance using Lambda AWS WebSocket API: how can I invoke API Gateway WebSocket URL from nodejs code running in EC2 instance? - AWS WebSocket API: how can I invoke API Gateway WebSocket URL from nodejs code running in EC2 instance? 无法使用带有浏览器的nod​​ejs aws-sdk找到EC2服务 - Cannot find EC2 service using nodejs aws-sdk with browserify 无法使用 AWS EC2 实例上的端口 25 从 nodejs nodemailer 发送 gmail 地址的邮件 - Cannot send mails for gmail address from nodejs nodemailer using port 25 on AWS EC2 instance AWS,NodeJS-将应用程序连接到另一个EC2实例上的Mongodb - AWS, NodeJS - Connecting app to Mongodb on another EC2 instance 从 NodeJS 关闭系统或停止 AWS EC2 实例 - Shutdown system or stop AWS EC2 instance from NodeJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM