【问题标题】:Determining Amazon EC2 instance creation date/time确定 Amazon EC2 实例创建日期/时间
【发布时间】:2013-09-25 19:18:16
【问题描述】:

是否可以(通过 boto)确定特定 EC2 实例的创建时间?

http://boto.readthedocs.org/en/latest/ref/ec2.html 在这种情况下似乎没有提供任何帮助。需要找出一组特定 EC2 实例的创建日期。

谢谢!

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto ec2-api-tools


    【解决方案1】:

    EC2 实例没有名为create_time 的属性,只有launch_time 可用。

    但是,您可以使用以下 Python 代码了解卷的创建时间,从而为您提供实例创建时间(注意我说的是创建实例时附加的卷):

    import boto3
    ec2 = boto3.resource('ec2', region_name='instance_region_name')
    volume = ec2.Volume('vol-id')
    print volume.create_time.strftime("%Y-%m-%d %H:%M:%S")
    

    替代方法是使用自定义代码。当您使用create_instances() 创建实例时,您可以将给定实例的launch_time 连同其实例 ID 和名称一起记录到 DynamoDB 等某个地方,以便您可以随时使用实例 ID 检索“创建时间”。

    【讨论】:

      【解决方案2】:

      假设您使用的是 EBS 支持的实例并且没有做任何花哨的驱动器杂耍,确定实例创建日期的最佳方法是查看驱动器根卷的创建时间。虽然每次停止和启动实例的启动时间都会发生变化,但 EBS 卷的创建时间是静态的。

      这是一个快速脚本,用于查找您当前运行的实例的创建时间:

      import boto
      import subprocess
      
      instance_id = subprocess.check_output(['curl', '-s', 'http://169.254.169.254/latest/meta-data/instance-id'])
      
      conn = boto.connect_ec2()
      
      root_device = conn.get_instance_attribute(instance_id, 'rootDeviceName')['rootDeviceName']
      root_vol = conn.get_all_volumes(filters={"attachment.instance-id": instance_id, "attachment.device": root_device})[0]
      
      print root_vol.create_time
      

      请注意,这要求实例的 IAM 角色具有 ec2:DescribeInstanceAttributeec2:DescribeVolumes 权限

      【讨论】:

        【解决方案3】:

        如下图:

        http://boto.readthedocs.org/en/latest/ref/ec2.html#module-boto.ec2.instance

        每个 Instance 对象都有一个名为 launch_time 的属性,其中包含一个 IS08601 日期时间字符串,表示该实例何时启动。

        在 boto 中,您可以执行以下操作:

        import boto.ec2
        
        conn = boto.ec2.connect_to_region('us-west-1')
        reservations = conn.get_all_instances()
        for r in reservations:
            for i in r.instances:
                print('%s\t%s' % (i.id, i.launch_time)
        

        【讨论】:

        • 但这和创建时间一样吗?在启动时间和创建时间之间感到困惑。 :(
        • 也许我对您要查找的内容感到困惑。启动时间将告诉您该特定实例实际启动的时间。就像虚拟机启动并开始启动一样。这就是你要找的吗?
        • 其实不,不等于。如果您启动和实例,将其在一夜之间停止,然后在早上启动,您的启动时间将是早上的日期,而不是您第一次创建实例的日期。
        • @npiani AFAIK,创建时间不会存储在实例元中的任何位置。
        【解决方案4】:

        无法直接获取EC2实例的创建时间。 由于 EC2 实例的启动时间会在每次实例启动和停止时更新。

        我们可以通过两种方式获取实例创建时间:

        1) 通过获取Instance的Network interface attach time

        2) 如上图所示通过获取Volume attach time。

        如何在 boto3 中获取网络接口连接时间

        import boto3
        from datetime import datetime
        instance_details = client.describe_instances()
        num_ins=(len(instance_details['Reservations'])
        for x in range(num_ins):
        
        
           InstanceID=(instance_details['Reservations'][x]['Instances'][0]['InstanceId'])
           NetworkInterfaceID = (instance_details['Reservations'][x]['Instances'][0]['NetworkInterfaces'][0]['NetworkInterfaceId'])
           NetworkInterface_details = client.describe_network_interfaces(NetworkInterfaceIds=[NetworkInterfaceID])
           networkinterface_id_attachedtime = NetworkInterface_details['NetworkInterfaces'][0]['Attachment']['AttachTime']
        
           print(networkinterface_id_attachedtime)
        

        打印当前实例的网络接口附加时间。

        【讨论】:

          【解决方案5】:

          如果您想根据 EC2 实例的启动时间计算当前的正常运行时间,可以试试这个:

          import datetime
          lt_datetime = datetime.datetime.strptime(i.launch_time, '%Y-%m-%dT%H:%M:%S')
          lt_delta = datetime.datetime.utcnow() - lt_datetime
          uptime = str(lt_delta)
          

          【讨论】:

            猜你喜欢
            • 2013-08-08
            • 1970-01-01
            • 2011-06-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多