I am writing a Python 2.7 script that will stop an EC2 instance, resize the instance, then start the instance back up. Is there a way to use boto3 to resize an instance? If not, is there another way to handle instance resizing programmatically?
This seems to work:
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With