How to modify EC2 Instance Properties using boto3?

0 votes

Hi Guys,

I have launched an EC2 instance using boto3. Now I want to modify some properties on that instance. How can I do that?

Oct 7 in AWS by akhtar
• 33,720 points
133 views

1 answer to this question.

0 votes

Hi@akhtar,

To modify an EC2 instance, first, you need to stop the instance and wait until it is properly stopped. Then, using the instance id, you can specify the attribute and new value with the 'modify_instance_attribute()' method, which will change the EC2 instance attribute. In your case, that attribute is 'instanceType'. Once that is done, start the instance with the modified properties as shown below.

import boto3
ec2 = boto3.client('ec2')
# choose an EC2 instance with id
instance_id = 'i-05ca5f05f965b3a4b'
# Stop the instance
ec2.stop_instances(InstanceIds=[instance_id])
waiter=ec2.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[instance_id])
# Change the instance type
ec2.modify_instance_attribute(InstanceId=instance_id, Attribute='instanceType', Value='t2.small')
# Start the instance
ec2.start_instances(InstanceIds=[instance_id])
answered Oct 7 by MD
• 79,930 points

Related Questions In AWS

0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 57,700 points
1,126 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to terminate EC2 instance using boto3?

Hi@akhtar, We can create and modify an EC2 ...READ MORE

answered Oct 7 in AWS by MD
• 79,930 points
98 views