我已经开始广泛使用Amazon EC2并且为了控制成本,我在离开工作之前手动关闭实例并在我进入工作时将其启动。有时我忘记关闭它们。亚马逊仪表板(或任何其他方式)中是否有一种机制可以在下午6点自动关闭实例并在早上6点将其启动?如果有任何API可用,我很乐意编写脚本或程序。如果您已经编写了一些代码,那么如果您可以共享它将会很棒。
答案 0 :(得分:1)
有两种解决方案。
AWS Data Pipeline - 您可以像cron
一样安排实例启动/停止。每次开始/停止都需要花费一小时的t1.micro实例
AWS Lambda - 定义在预定义时间触发的lambda函数。 lambda函数可以启动/停止实例。您的费用将非常低或$ 0
在转移到Lambda之前,我使用了Data Pipeline很长一段时间。数据管道非常简单。只需粘贴AWS CLI命令即可停止和启动实例。 Lambda更多参与其中。
答案 1 :(得分:0)
我实际上碰巧有一个cron工作为此而运行。我会在这里提出我的'开始实例'代码(供参考,以帮助您入门)
#!/usr/bin/python
import boto.ec2
import boto
import boto.regioninfo
import time
import sys
import boto.ec2.connection
from boto.ec2 import EC2Connection
from boto.regioninfo import RegionInfo
from boto import ec2
from boto import regioninfo
ec2_region = ec2.get_region(aws_access_key_id='QWERTACCESSKEY', aws_secret_access_key='QWERTSECRETKEY', region_name='ap-southeast-2')
conn = boto.ec2.connection.EC2Connection(aws_access_key_id='QWERTACCESSKEY', aws_secret_access_key='QWERTSECRETKEY', region=ec2_region)
instance1 = conn.get_only_instances(instance_ids=['i-xxxxxxx1'])
instance2 = conn.get_only_instances(instance_ids=['i-xxxxxxx2'])
instance3 = conn.get_only_instances(instance_ids=['i-xxxxxxx3'])
instance4 = conn.get_only_instances(instance_ids=['i-xxxxxxx4'])
def inst(): # updates states of instances
instance1[0].update()
instance2[0].update()
instance3[0].update()
instance4[0].update()
def startservers():
inst()
if instance1[0].state in 'stopped':
instance1[0].start()
if instance2[0].state in 'stopped':
instance2[0].start()
if instance3[0].state in 'stopped':
instance3[0].start()
if instance4[0].state in 'stopped':
instance4[0].start()
def check(): # a double check to make sure the servers are down
inst()
while instance1[0].state in 'stopped' or instance2[0].state in 'stopped' or instance3[0].state in 'stopped' or instance4[0].state in 'stopped':
startservers()
time.sleep(30)
startservers()
time.sleep(60)
check()
这是我的cronjob
31 8 * * 1-5 python /home/user/startaws
这是从星期一到星期五的上午8:31运行。
请注意 这个脚本对我来说很好,但它绝对可以使它更清洁和简单。还有比这更好的方法。 (我写的很匆忙,所以我确定我有一些不必要的代码行)我希望它能让你知道如何开始:)
答案 2 :(得分:0)
如果您的用例允许终止和重新启动,您可以在Auto Scaling组中考虑Scheduled Scaling。 AWS最近添加了管理控制台UI中管理调度规则的工具。
但我不相信Auto Scaling会涵盖启动和停止同一个实例,保留状态。
答案 3 :(得分:0)
您不需要编写任何脚本来实现此目的,只需使用AWS控制台API即可。这是使用Scheduled Scaling进行AutoScaling的完美用例。步骤进行:
每天早上放大
aws autoscaling put-scheduled-update-group-action --scheduled-action-name scaleup-schedule --auto-scaling-group-name MY_AUTOSCALING_GROUP1 --recurrence" 0 6 * * *" - 所需容量5
每天晚上缩小
aws autoscaling put-scheduled-update-group-action --scheduled-action-name scaledown-schedule --auto-scaling-group-name MY_AUTOSCALING_GROUP1 --recurrence" 0 18 * * *" - 期望容量0
每天早上(早上6点)开始使用5个EC2实例,并通过将以上计划的操作应用于您的自动缩放组,每天晚上(下午6点)终止。
答案 4 :(得分:0)
您可以从一个简单的解决方案开始,比如cron作业和AWS CLI吗?
start.sh
lenders
stop.sh
if (db.checkIfCardAdded(pagerItems.get(position).getCardID())) {
Log.e("Card already added", pagerItems.get(position).getCardName());
saveCard.setEnabled(false);
} else {
saveCard.setEnabled(true);
}
然后 crontab -e 并添加类似
的内容# some preparations
# ...
$(aws ec2 start-instances --instance-ids $LIST_OF_IDS)
$(aws ec2 wait instance-running --instance-ids $LIST_OF_IDS)
答案 5 :(得分:0)
使用AWS Lambda对scheduled event来说非常简单。您可以找到完整的代码脚本here
例如,启动EC2实例
import boto3 def lambda_handler(event, context): client = boto3.client('ec2') response = client.start_instances( InstanceIds=[ 'i-xxxxxx', 'i-xxxxxx', ] ) print response
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents", "ec2:StartInstances" ], "Resource": [ "arn:aws:logs:*:*:*", "arn:aws:ec2:*:*:*" ] } ] }
点击创建lambda函数并添加事件源(CloudWatch事件 - 日程安排)
最后,测试你的功能以确定它是否正常工作