2016-07-29 54 views
1

我正在编写一个Python 2.7脚本,它将停止EC2实例,调整实例大小,然后启动实例备份。有没有办法使用boto3来调整实例的大小?如果没有,是否有另一种方式来以编程方式处理实例大小调整?使用boto3调整EC2实例的尺寸

+0

经过这太问题http://stackoverflow.com/questions/31907783/how-to-change-aws-ec2-instance-type – error2007s

+1

通过调整大小,做您的意思是更改实例类型或更改EBS卷大小? –

+0

@KarenB我应该澄清,我的意思是改变实例类型。 – danielhklein

回答

4

这似乎工作:

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])