使用boto3调整EC2实例大小

5

我正在编写一个Python 2.7脚本,用于停止EC2实例,调整实例大小,然后重新启动实例。是否有一种使用boto3调整实例大小的方法?如果没有,是否还有其他方法来处理实例大小调整的编程问题?


浏览此 SO 问题:http://stackoverflow.com/questions/31907783/how-to-change-aws-ec2-instance-type。 - error2007s
1
通过“resize”,您是指更改实例类型还是更改EBS卷大小? - Karen B
@KarenB 我应该澄清一下,我是指更改实例类型。 - dededecline
@error2007s 我已经看过那篇帖子了,在我的帖子中我应该提到这一点。然而,它并没有什么帮助:问题中的代码是伪代码,答案是关于更改实例类型的一般过程,与boto3无关。 - dededecline
1个回答

23

这似乎有用:

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代表什么?我也是AWS Buto3的新手,正在尝试调整实例大小和类型。 - lisa_rao007
1
Waiter.InstanceStopped文档中可以看到:每15秒轮询一次EC2.Client.describe_instances(),直到达到成功状态。在40次失败检查后将返回错误。 - John Rotenstein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接