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?
Asked
Active
Viewed 8,461 times
5
-
Go through this SO question http://stackoverflow.com/questions/31907783/how-to-change-aws-ec2-instance-type – Piyush Patil Jul 29 '16 at 17:28
-
1By resize, do you mean change the instance type or change the EBS volume size? – Karen B Jul 29 '16 at 18:09
-
@KarenB I should have clarified, I meant changing the instance type. – dededecline Jul 29 '16 at 18:50
-
@error2007s I had already seen that post, which I guess I should have mentioned in my post. It is not helpful, however: the code in the question is pseudocode and the answer is about the general process of changing an instance type and has nothing to do with boto3. – dededecline Jul 29 '16 at 22:12
1 Answers
23
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])

Shailesh Sutar
- 370
- 1
- 6
- 22

John Rotenstein
- 241,921
- 22
- 380
- 470
-
What is waiter representing here. I am also new to aws buto3 and trying to resize the instance and the type – lisa_rao007 Sep 07 '19 at 02:50
-
1@user3713336 From [`Waiter.InstanceStopped` documentation](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Waiter.InstanceStopped): _Polls `EC2.Client.describe_instances()` every 15 seconds until a successful state is reached. An error is returned after 40 failed checks._ – John Rotenstein Sep 07 '19 at 10:10