【问题标题】:Stop ec2 instance after 5 minutes with sleep - time.sleep() not working as expected?睡眠 5 分钟后停止 ec2 实例 - time.sleep() 没有按预期工作?
【发布时间】:2020-12-27 16:45:08
【问题描述】:

我在 lambda 中运行以下代码。我想在 5 分钟后停止我的 EC2 实例。为此,我在发出 stop_instance 命令之前使用了 time.sleep 间隔 5 分钟。但是time.sleep之后代码没有通过,而是从头开始循环lambda脚本?

 ec2 = boto3.client('ec2', region_name=region)
 resp = ec2.describe_instance_status(InstanceIds=instances)
 if(len(resp['InstanceStatuses']) == 0):
          ec2.start_instances(InstanceIds=instances)
          print("Started instance with Instance_id : ",instances)
    else:
        instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code']
        if instance_status == 80:
            ec2.start_instances(InstanceIds=instances)
            print("Started instance with Instance_id : ",instances)
            print("Instance staus - 1 **: ",instance_status)

    status = 0
    counter = 5
    while (status != 16 and counter > 0):
        resp = ec2.describe_instance_status(InstanceIds=instances)
        if len(resp['InstanceStatuses']) != 0:
            status = resp['InstanceStatuses'][0]['InstanceState']['Code']
            time.sleep(5)
            counter=counter-1
            print("Instance staus - 3 **: ",status)

    print("Wait for 5 minutes please !!!")
    i = 0
    while (i < 1):
       time.sleep(300)
       i = i + 1

    print("Wait time is over, instance status : ", status)
    resp = ec2.describe_instance_status(InstanceIds=instances)
    instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code']
    
    print("Instance staus - 2 **: ",instance_status)
    if instance_status == 16:
        #check if file exists in the S3 location, and if yes, then stop instances.
        ec2.stop_instances(InstanceIds=instances)
        print("Stopped instance with Instance_id : ",instances)

这是我在 cloudwatch 日志中看到的,并且实例没有停止。以下是我的 cloudwatch 日志:

有人可以帮我完成这项工作,以便在 x 分钟后停止 ec2。谢谢。

【问题讨论】:

  • Lambda 函数的执行时间最长,可能不到五分钟。
  • @AKX 已从 5 分钟增加到 15 分钟 - 在撰写本文时。
  • 您的实际目标是什么?也就是说,你想要达到什么目的?您是否希望 EC2 实例在启动后停止 x 分钟?如果是这样,最好让实例通过简单的 shell 脚本自行关闭。请告诉我们您想要实现的什么以及为什么(而不是如何),我们可能会提出更好的方法.

标签: python python-3.x amazon-ec2 aws-lambda boto3


【解决方案1】:

当你只调用一次时,为什么还要打扰它。只需删除While

更好的方法是设置一个 UserData EC2 脚本:

A) 将 .py 文件保存到机器(关闭 S3)并执行它以在 5 分钟内关闭机器。或者

B) 用户数据中的 bash/PowerShell timer.sleep(300)

编辑

您可以简单地将其转换为 Bash/PowerShell 并将其放入 UserData 中吗?

time.sleep(300) 
print("Wait time is over, instance status : ", status) 
resp = ec2.describe_instance_status(InstanceIds=instances) 
instance_status = resp['InstanceStatuses'][0]['InstanceState']['Code'] 
print("Instance staus - 2 **: ",instance_status)
 if instance_status == 16:
   ec2.stop_instances(InstanceIds=instances)

【讨论】:

  • 感谢 Jeremy,最初我尝试不使用 while 循环,但遇到了同样的问题。然后我提到了一些 SO 响应,其中建议有 while 或 if 条件。想知道为什么它应该从 lamda 脚本的开头开始。
  • 如果当 i = 1 或大于 1 时 while 循环失败,我所期望的,那么它将继续到下一行,即打印“等待时间结束,实例状态..” ,这不会发生。
  • 也许 Lambda 中的计时器在一段时间后被禁止,因为人们试图滥用保持他们的 lambdas 温暖。看到这个,我认为 Cloud Watch 事件现在称为 EventBridge,所以这些说明可能已经过时,但你明白了:stackoverflow.com/q/63166951/495455
  • 谢谢。我已经在用户数据中有一个 ML python 脚本,它在 EC2 启动时运行。我只想给 5 分钟的时间,所以 python 脚本完全完成,并将文件上传到 S3。如果我只是添加 STOP INSTANCE 没有睡眠,userdata 中的 python 脚本没有完全运行,我错过了 S3 中的输出文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-15
  • 2021-10-13
  • 1970-01-01
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多