简体   繁体   English

从 NodeJS 关闭系统或停止 AWS EC2 实例

[英]Shutdown system or stop AWS EC2 instance from NodeJS

I have AWS EC2 instances running Debian with systemd running Node as a service.我有运行 Debian 的 AWS EC2 实例,而 systemd 运行 Node 作为服务。 (Hereinafter these instances are called the "Node servers".) (以下这些实例称为“节点服务器”。)

The Node servers are started by another instance (hereinafter called "the manager instance") that is permanently on.节点服务器由另一个永久运行的实例(以下称为“管理器实例”)启动。

When a Node server experiences some predefined period of inactivity, I want it to shut down automatically.当 Node 服务器遇到一些预定义的不活动时间时,我希望它自动关闭。

I am considering the following options:我正在考虑以下选项:

  1. (After sensing a period of inactivity in Node) execute a child_process in Node that runs the shutdown now command. (在 Node 中检测到一段时间不活动后)在 Node 中执行一个child_process来运行shutdown now命令。
  2. (After sensing a period of inactivity in Node) call AWS SDK's stopInstances with the instance's own resource ID. (在 Node 中检测到一段时间不活动后)使用实例自己的资源 ID 调用 AWS 开发工具包的stopInstances
  3. Expose an HTTP GET endpoint called last-request-time on each Node server, which is periodically polled by a "manager instance", which then decides whether/when to call AWS SDK's stopInstances .在每个节点服务器上公开一个名为last-request-time的 HTTP GET 端点,该端点由“管理器实例”定期轮询,然后决定是否/何时调用 AWS SDK 的stopInstances

I am unsure which of these approaches to take and would appreciate any advice.我不确定要采取哪种方法,并希望得到任何建议。 Explicitly shutting down a machine from Node running on that same machine feels somehow inappropriate.从运行在同一台机器上的 Node 显式关闭一台机器感觉有点不合适。 But option 3 requires periodic HTTP polling, not to mention that it feels more risky to rely on another instance for auto-shutdown.但是选项 3 需要定期进行 HTTP 轮询,更不用说依赖另一个实例进行自动关闭感觉风险更大。 (If the manager is down all the instances keep going.) (如果管理器宕机,所有实例都会继续运行。)

Or perhaps it is possible to get systemd to shut down the machine when a particular service exits with a particular code?或者,当特定服务以特定代码退出时,是否有可能让 systemd 关闭机器? This, if possible, would feel like the best solution as the Node process would only need to abort itself after the period of inactivity with a particular exit code.如果可能的话,这会是最好的解决方案,因为 Node 进程只需要在不活动的一段时间后使用特定的退出代码中止自身。

You could create a lambda function that acts as an api and uses the SDK's stopInstances functionality.您可以创建一个用作 api 的 lambda 函数并使用 SDK 的 stopInstances 功能。 That would also allow you to make it have the full functionality of a "manager instance" and save even more on instances since it will only run when needed.这也将允许您使其具有“管理器实例”的全部功能,并在实例上节省更多,因为它只会在需要时运行。

Or you could cut out the middle-man and migrate the "Node servers" to lambda.或者你可以去掉中间人并将“节点服务器”迁移到 lambda。 ( lambda documentation ) 拉姆达文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM