EC2按计划启动

发布于 2024-12-31 23:46:49 字数 375 浏览 1 评论 0原文

我需要每天(比如说)早上 6 点启动一个 EC2 实例。限制是我想避免让计算机整天运行来启动或使用像 ylastic 这样的付费解决方案。

alestic 的解决方案是迄今为止最接近的。该解决方案的缺点是启动时间较长,因为安装自定义软件和移动数据需要时间。

有没有办法只启动一个实例,而不是每次都创建一个新实例,如此 示例< /a>?

I need to start up an EC2 instance at (say) 6am every day. The constraints are that I'd like to avoid having a computer running all day to do the startup or use a paid solution like ylastic's.

The solution at alestic, is the closest so far. The downside of this solution is that the startup time is high because of the time required to install custom software and to move around data.

Is there a way to just boot up an instance instead of creating a new instance each time as shown in this example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

萌辣 2025-01-07 23:46:49

您现在可以使用 Amazon OpsWorks 执行此操作。设置完主要内容后,只需创建一个新实例并将其缩放类型设置为“基于时间”(一旦创建了实例,您就无法出于某种原因更改此设置):

在此处输入图像描述

现在,只需单击“Instances > Time-Based”类别并设置计划:

在此处输入图像描述

You can now do this with Amazon OpsWorks. After you've set up the main stuff, just make a new instance and set its scaling type to "time-based" (you can't change this for some reason once the instance has been made):

enter image description here

Now, just click on the "Instances > Time-Based" category and setup the schedule:

enter image description here

静若繁花 2025-01-07 23:46:49

考虑到您的限制,不幸的是,所需的功能并未被 AWS 产品和 AWS 产品提供的两种专用自动化机制所涵盖。现在的服务

  • 自动缩放 - 是一项 Web 服务,旨在根据用户定义的策略、计划和运行状况检查自动启动或终止 Amazon Elastic Compute Cloud (Amazon EC2) 实例。
  • AWS CloudFormation - 为开发人员和系统管理员提供了一种简单的方法来创建和管理 相关 AWS 资源的集合,以有序且可预测的方式配置和更新它们。

启动/停止/重新启动实例在概念上属于管理< /em>后者的类别,它不可用(顺便说一句,这就是我们专门为此功能提供单独任务的原因,同时已弃用 Bamboo AWS 插件 及其后继者 AWS 任务)。

因此,我在如何在办公时间打开/关闭云实例的回答中概述的方法仍然适用,尽管您需要找到免费托管您的脚本或持续集成解决方案的提供商的附加限制:

  • 通过这些 cron 作业提供商,托管脚本已经可以使用很长一段时间了。

  • 鉴于当前平台即服务 (PaaS) 解决方案的爆炸式增长,有相当多的解决方案提供商,这将允许您以一种或另一种方式执行主机脚本和/或持续集成解决方案。

显然,您需要根据相应的 验证用于此类目的的免费套餐是否可以接受相关提供商的使用条款

Given your constraints, the desired functionality is unfortunately not covered by the two dedicated automation mechanisms available as AWS Products & Services right now:

  • Auto Scaling - is a web service designed to automatically launch or terminate Amazon Elastic Compute Cloud (Amazon EC2) instances based on user-defined policies, schedules, and health checks.
  • AWS CloudFormation - gives developers and systems administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion.

While starting/stopping/rebooting an instance conceptually falls into the manage category of the latter, it is not available like so (which incidentally is the reason we provide a separate Task specifically for this functionality within the meanwhile deprecated Bamboo AWS Plugin and its successor Tasks For AWS).

Consequently the approaches outlined in my answer to How to turn on/off cloud instances during office hours are still applicable, albeit with the additional constraint that you would need to find a provider hosting your script or continuous integration solution for free:

  • Hosting scripts has e.g. been possible for quite a while already by means of those cron job providers.

  • Given the current explosion in Platform as a Service (PaaS) solutions there are quite some providers, that will allow you to do host scripts and/or continuous integration solutions one way or another as well.

Obviously you'll need to verify, whether using the free tiers available for purposes like this is acceptable according to the respective Terms of Use of a provider in question.

野味少女 2025-01-07 23:46:49

还有另一个基于 Java 的工具 EC2 Scheduler 可以帮助您对于这个问题。对我来说,我想在办公时间为我的团队提供一台服务器,即使它没有被任何人使用。这个应用程序帮助我实现了这个目标。希望这对你也有好处。

There is another java based tool EC2 Scheduler that can help you for this problem. For me I wanted to make a server available to my team during office times even it is not being used by anyone. This application helped me to achieve this goal. Hope this is good for you too.

半窗疏影 2025-01-07 23:46:49

我建议使用 AWS Lambda 安排 EC2 启动。

建议
查看来自 D 的建议。 Svanlund,Ace 用户:2000 多分,在此 AWS 论坛主题

优点
您只需要安排一两个小脚本即可。无需启动实例,只需快速调用您构建的脚本即可。选择您选择的编程语言并使用 AWS 开发工具包执行实例操作。一个相当轻量级的解决方案,

预计成本
该任务每天运行两次,时间通常少于 3 秒,内存消耗高达 128MB,通常成本低于 0.0004 美元/月(请参阅参考)

日程安排
2016 年 1 月,AWS Lambda 计划事件已转换为AWS CloudWatch 事件 。 CloudWatch Events 具有与 Lambda 调度事件相同的调度功能。每个区域 5 个计划事件的 AWS Lambda 计划事件限制已提升至 50 个 CloudWatch Events 规则。

方法
设置适合启动/停止调度的 EC2 类型。我建议使用EC2-VPC/EBS
创建 IAM策略角色。新创建角色的信任关系(参见参考)。
通过函数触发器为 Lambda 配置 CloudWatch Events 触发器 如下所示。

CloudWatch Events - Schedule

以下是运行时设置为 Node.js 的函数 start-server 的代码。
将 YOUR_REGION 和 YOUR_INSTANCE_ID 从 实例控制台

var AWS = require('aws-sdk');
exports.handler = function(event, context) {
 var ec2 = new AWS.EC2({region: 'YOUR_REGION'});
 ec2.startInstances({InstanceIds : ['YOUR_INSTANCE_ID'] },function (err, data) {
 if (err) console.log(err, err.stack); // an error occurred
 else console.log(data); // successful response
 context.done(err,data);
 });
};

注意:如果您需要功能停止服务器,只需将ec2.startInstances更改为ec2.stopInstances。 时,您甚至可能不需要停止功能

当您使用每次EC2启动时自动关闭 日志记录
如果创建的 IAM 角色具有必要的权限,则 Lambda 函数将创建 AWS每次运行的 CloudWatch Log Stream

AWS CloudWatch 日志流

I'd suggest to Schedule EC2 Startup using AWS Lambda.

Recommendation:
Check the suggestion from D. Svanlund, user with Ace: 2000+ pts, on this AWS Forum Thread.

Advantage:
You don't need anything more than a small script or two that you schedule. No instance to launch, just a quick invocation of the script you have built. Pick the programming language of your choice and use the AWS SDK to perform instance operations. A quite lightweight solution,

Estimated Cost:
The task running twice a day for typically less than 3 seconds with memory consumption up to 128MB typically costs less than $0.0004 USD/month (See Reference)

Scheduling
In January 2016 AWS Lambda scheduled events have been transformed into AWS CloudWatch Events. CloudWatch Events have the same scheduling capabilities as Lambda scheduled events used to have. The AWS Lambda scheduled events limitation of 5 scheduled events per region has been lifted to 50 CloudWatch Events rules.

Method
Setup EC2 types that is suitable for Start/Stop Scheduling. I recommend to use EC2-VPC/EBS.
Create IAM policy and role. Trust Relationship of the newly created role (See Reference).
Configure the CloudWatch Events trigger for Lambda via Function Trigger as shown below.

CloudWatch Events - Schedule

Here is the code of the function start-server which Runtime is set to Node.js.
Change YOUR_REGION and YOUR_INSTANCE_ID to yours from Instance Console.

var AWS = require('aws-sdk');
exports.handler = function(event, context) {
 var ec2 = new AWS.EC2({region: 'YOUR_REGION'});
 ec2.startInstances({InstanceIds : ['YOUR_INSTANCE_ID'] },function (err, data) {
 if (err) console.log(err, err.stack); // an error occurred
 else console.log(data); // successful response
 context.done(err,data);
 });
};

Note: Incase you need function stop-server just change ec2.startInstances to ec2.stopInstances. You may even no need the stop function when you use Automatic Shutdown Per EC2 Boot

Logging
If the IAM role is created with necessary permissions, then Lambda function will create AWS CloudWatch Log Stream for each of its run.

AWS CloudWatch Log Stream

忘羡 2025-01-07 23:46:49

我刚刚遇到了同样的问题,并通过使用 自动缩放 解决了它,就像这里提到的许多答案一样。您唯一需要的是要运行的 AMI 映像和自动扩展 API 命令行工具< /a>.

下载工具后,请按照自述文件中的说明设置环境变量并添加您的 AWS 凭证。将以下命令放入批处理文件中(此示例适用于 Windows):

as-create-launch-config --key "MYLAUNCHCONFIGNAME" --instance-type t1.micro --image-id MYAMI-IMAGEID --launch-config "MYLAUNCHCONFIGNAME"
as-create-auto-scaling-group --auto-scaling-group "MYSCALINGGROUPNAME" --launch-configuration "MYLAUNCHCONFIGNAME" --availability-zones "us-east-1a,us-east-1b,us-east-1c,us-east-1d" --min-size 0 --max-size 0

rem Don't restart instance after shutdown
as-suspend-processes "MYSCALINGGROUPNAME" --processes ReplaceUnhealthy

rem Start instance at 22:15
as-put-scheduled-update-group-action --name "startMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 1 --max-size 1   --recurrence "15 22 * * *"

rem Stop instance at 23:05
as-put-scheduled-update-group-action --name "stopMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 0 --max-size 0 --recurrence "05 23 * * *"

将大写名称替换为您选择的名称,并为 AMI 映像设置正确的 AMI-ID。基于您的 AMI 映像的新实例将于 22:15 启动并于 23:05 终止。当然,您也可以更改实例类型和可用区。

I just faced the same problem and solved it by using Autoscaling just as many of the answers here mentioned. The only thing you need for that is an AMI image that you want to run and the Autoscaling API command-line tools.

After you downloaded the tools, follow the instructions in the readme to set the environmental variables and add your AWS credentials. Than put the following commands in a batch file (this example is for Windows):

as-create-launch-config --key "MYLAUNCHCONFIGNAME" --instance-type t1.micro --image-id MYAMI-IMAGEID --launch-config "MYLAUNCHCONFIGNAME"
as-create-auto-scaling-group --auto-scaling-group "MYSCALINGGROUPNAME" --launch-configuration "MYLAUNCHCONFIGNAME" --availability-zones "us-east-1a,us-east-1b,us-east-1c,us-east-1d" --min-size 0 --max-size 0

rem Don't restart instance after shutdown
as-suspend-processes "MYSCALINGGROUPNAME" --processes ReplaceUnhealthy

rem Start instance at 22:15
as-put-scheduled-update-group-action --name "startMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 1 --max-size 1   --recurrence "15 22 * * *"

rem Stop instance at 23:05
as-put-scheduled-update-group-action --name "stopMyInstance" --auto-scaling-group "MYSCALINGGROUPNAME" --min-size 0 --max-size 0 --recurrence "05 23 * * *"

Replace the capitalized names with some of your choice and set the correct AMI-ID for your AMI image. A new instance based on your AMI image will start at 22:15 and terminate at 23:05. Of course you can also change the instance type and availability zone(s).

日裸衫吸 2025-01-07 23:46:49

恕我直言,向自动扩展组添加计划是前面提到的最好的“类似云”的方法。

但如果您无法终止实例并使用新实例,例如您有关联的弹性 IP 等,

您可以创建一个 Ruby 脚本来根据日期时间范围启动和停止实例。

#!/usr/bin/env ruby

# based on https://github.com/phstc/amazon_start_stop

require 'fog'
require 'tzinfo'

START_HOUR = 6 # Start 6AM
STOP_HOUR  = 0 # Stop  0AM (midnight)

conn = Fog::Compute::AWS.new(aws_access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
                             aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])

server = conn.servers.get('instance-id')

tz = TZInfo::Timezone.get('America/Sao_Paulo')

now = tz.now

stopped_range = (now.hour >= STOP_HOUR && now.hour < START_HOUR)
running_range = !stopped_range

if stopped_range && server.state != 'stopped'
  server.stop
end

if running_range && server.state != 'running'
  server.start

  # if you need an Elastic IP
  # (everytime you stop an instance Amazon dissociates Elastic IPs)
  #
  # server.wait_for { state == 'running' }
  # conn.associate_address server.id, 127.0.0.0
end

查看amazon_start_stop,使用Heroku 调度程序

IMHO adding a schedule to an auto scaling group is the best "cloud like" approach as mentioned before.

But in case you can't terminate your instances and use new ones, for example if you have Elastic IPs associated with etc.

You could create a Ruby script to start and stop your instances based on a date time range.

#!/usr/bin/env ruby

# based on https://github.com/phstc/amazon_start_stop

require 'fog'
require 'tzinfo'

START_HOUR = 6 # Start 6AM
STOP_HOUR  = 0 # Stop  0AM (midnight)

conn = Fog::Compute::AWS.new(aws_access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
                             aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'])

server = conn.servers.get('instance-id')

tz = TZInfo::Timezone.get('America/Sao_Paulo')

now = tz.now

stopped_range = (now.hour >= STOP_HOUR && now.hour < START_HOUR)
running_range = !stopped_range

if stopped_range && server.state != 'stopped'
  server.stop
end

if running_range && server.state != 'running'
  server.start

  # if you need an Elastic IP
  # (everytime you stop an instance Amazon dissociates Elastic IPs)
  #
  # server.wait_for { state == 'running' }
  # conn.associate_address server.id, 127.0.0.0
end

Have a look at amazon_start_stop to create a scheduler for free using Heroku Scheduler.

病毒体 2025-01-07 23:46:49

AWS Data Pipeline 非常适合此任务。数据管道
使用 AWS 技术,并且可以配置为运行 AWS CLI 命令
没有外部依赖性的设定时间表。数据管道可以写
记录到 S3 并在 IAM 角色的上下文中运行,这消除了
关键管理要求。数据管道也具有成本效益;为了
例如,数据管道免费层可用于停止和启动
每天一次。

https://aws.amazon.com/premiumsupport/knowledge-中心/停止-启动-ec2-instances/

The AWS Data Pipeline is uniquely suited to this task. Data Pipeline
uses AWS technologies and can be configured to run AWS CLI commands on
a set schedule with no external dependencies. Data Pipeline can write
logs to S3 and runs in the context of an IAM role, which eliminates
key management requirements. Data Pipeline is also cost effective; for
example, the Data Pipeline free tier can be used to stop and start
instances once per day.

https://aws.amazon.com/premiumsupport/knowledge-center/stop-start-ec2-instances/

2025-01-07 23:46:49

自动缩放似乎是解决您问题的最佳解决方案,AWS 提供了这样的功能。如果您正在寻找像 ylastic 这样的第三方解决方案,但又不想付费,我知道的唯一选择是我工作的地方 Scalr。 Scalr 是开源的,因此您只需下载源代码并自行安装

其他替代方案包括 RightScale 和 enStratus。在我看来,RightScale 免费帐户不包括自动扩展,而 enStratus“免费”计划则按 0.20 美元/服务器小时的标准收取自动扩展费用。

Autoscaling seems the best solution for your problem and AWS provides such a feature. If you are looking for a third-party solution like ylastic, but you don't want to pay for it, the only alternative I know is Scalr where I work. Scalr is open-source, so you just have to download the source code and install it yourself.

Other alternatives includes RightScale and enStratus. To my mind RightScale free account does not include auto-scaling, while enStratus "free" plan charges autoscaling on a $0.20/server hour basis.

空城旧梦 2025-01-07 23:46:49

亚马逊现在有预定的预留实例

计划预留实例:这些实例可在您预留的时间范围内启动。此选项允许您
将您的容量预留与可预测的循环时间表相匹配
这只需要一天、一周或一个月的一小部分。为了
例如,如果您有可预测的工作负载,例如每月
财务风险分析,您可以安排它在前五个运行
该月的天数。另一个例子是安排夜间账单
每个工作日下午 4 点至凌晨 12 点处理。

但是也

期限
计划预留实例有一个
1 年任期承诺。

付款选项:预定预留实例
按小时累积费用,并在期限内按月增量计费。

阅读更多
https://aws.amazon.com/ec2/purchasing-options/reserved-实例/

Amazon now has Scheduled Reserved Instances

Scheduled Reserved Instances: These instances are available to launch within the time windows you reserve. This option allows you to
match your capacity reservation to a predictable recurring schedule
that only requires a fraction of a day, a week, or a month. For
example, if you have a predictable workload such as a monthly
financial risk analysis, you can schedule it to run on the first five
days of the month. Another example would be scheduling nightly bill
processing from 4pm-12am each weekday.

but also

Term:
Scheduled Reserved Instances have a
1 year term commitment.

Payment Option: Scheduled Reserved Instances
accrue charges hourly, billed in monthly increments over the term.

Read more
https://aws.amazon.com/ec2/purchasing-options/reserved-instances/

脸赞 2025-01-07 23:46:49
  • 这里也一样!令人震惊的是,亚马逊没有提供内置方法来简单地安排您的 EC2 启动和运行。在某个时间停止。 (好吧,不要那么震惊,因为他们可能不希望您能够将其关闭,对吧?:-)

  • 答案:我发现自动缩放方法是最好的方法要自动化此操作并且免费,无需第三方应用程序或 24/7 运行的单独服务器。这里需要充分了解 AS、AMI 和 EC2。

    转到下面的 URL 链接,了解如何将“预定操作”添加到自动扩展组中。效果很好!

    我现在可以让我的 EC2 服务器在中午 12 点启动并在晚上 8 点停止运行(实际上它会终止)。非常酷。祝你好运!

  • 下面是我的设置的屏幕截图。这些设置将每天创建并启动 EC2 一小时。干杯!

http://docs.aws.amazon.com/autoscaling /latest/userguide/schedule_time.html#sch-actions_rules

自动缩放计划操作:

  • Same here! Shocked to see Amazon does not provide a built-in way to simply schedule your EC2 to start & stop at a certain time. (Well, not that shocked as they probably don't want you to be able to just turn it off, right? :-)

  • ANSWER: I found the Auto-Scaling method the best way to automate this and for free, w/o the need for third party apps or a separate server running 24/7. A good understanding of AS, AMI's and EC2's is needed here.

    Goto URL link below to see how you can add "SCHEDULED ACTIONS" to your Auto-scaling groups. Works great!

    I can now have my EC2 server spun up at 12noon and spin down (Really it gets terminated) @ 8PM. Very cool. Good luck!

  • Below is screenshot of my settings. These setting will create and launch an EC2 everyday for one hour a day. CHEERS!

http://docs.aws.amazon.com/autoscaling/latest/userguide/schedule_time.html#sch-actions_rules

Auto-Scaling Scheduled Actions:

送君千里 2025-01-07 23:46:49

Amazon 最近宣布了两项新功能来实现这一目标,无需直接从 AWS Web Console EC2 部分进行配置进行自定义实现。

  • 使用计划扩展进行应用程序自动扩展(创建自动扩展组后,有一个选项卡,您可以在其中添加其他基于时间的自动扩展规则)
    输入图片此处描述

  • 预留预定 EC2 实例(在 EC2 控制台的实例下,可以选择预留预定 EC2 实例)
    输入图片此处描述

Amazon recently announced two new features to achieve this without custom implementations directly as configuration from AWS Web Console EC2 section.

  • Using Scheduled Scaling for Application Auto Scaling (After creating an autoscaling group, there is a tab where you can add additional time based autoscaling rules)
    enter image description here

  • Reserving Scheduled EC2 instances (In the EC2 console, under Instances, there is the option to reserve Scheduled EC2 instances)
    enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文