使用boto3调整EC2实例的大小

dan*_*ein 3 python amazon-ec2 amazon-web-services python-2.7 boto3

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

Joh*_*ein 12

这似乎有效:

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])
Run Code Online (Sandbox Code Playgroud)