1

我可以使用 python boto 来更改我的实例的关闭行为:

基本上,我们从 Web 界面更改的内容相同:

在此处输入图像描述

我用来运行实例的代码是:

   # Create and run a instance based on our predefined image
    reservation= conn.run_instances(
            'ami-0072ee30',
            key_name='rajat',
            instance_type=instance_requested_type)

现场实例请求:

 reservs = conn.request_spot_instances(
            float(max_bid),
            'ami-0072ee30',
            count=1,
            type='one-time',
            instance_type=instance_requested_type)
4

1 回答 1

6

是的你可以。run_instances调用的方法有一个可选参数,instance_initiated_shutdown_behavior其值为"stop"or "terminate"。因此,要扩展上面的示例以指定您希望实例在被用户终止时停止,您可以执行以下操作:

import boto3
ec2_client = boto3.client('ec2')
reservation = ec2_client.run_instances(
                     ImageId='ami-0072ee30',
                     MinCount=1,
                     MaxCount=1, 
                     KeyName='rajat', 
                     InstanceType='t2.micro',
                     InstanceInitiatedShutdownBehavior='terminate'
               )

参考:https ://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html

于 2013-10-16T12:53:15.563 回答