我正在编写一个Python 2.7脚本,用于停止EC2实例,调整实例大小,然后重新启动实例。是否有一种使用boto3调整实例大小的方法?如果没有,是否还有其他方法来处理实例大小调整的编程问题?
我正在编写一个Python 2.7脚本,用于停止EC2实例,调整实例大小,然后重新启动实例。是否有一种使用boto3调整实例大小的方法?如果没有,是否还有其他方法来处理实例大小调整的编程问题?
这似乎有用:
import boto3
client = boto3.client('ec2')
# Insert your Instance ID here
my_instance = 'i-xxxxxxxx'
# Stop the instance
client.stop_instances(InstanceIds=[my_instance])
waiter=client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[my_instance])
# Change the instance type
client.modify_instance_attribute(InstanceId=my_instance, Attribute='instanceType', Value='m3.xlarge')
# Start the instance
client.start_instances(InstanceIds=[my_instance])
Waiter.InstanceStopped
文档中可以看到:每15秒轮询一次EC2.Client.describe_instances()
,直到达到成功状态。在40次失败检查后将返回错误。 - John Rotenstein