Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 create image using instance id

I am using boto3 with python3 for aws usage, in the aws console there is a very simple option for creating an image from a running / stopped ec2 instance. I am looking for something similar in the boto3 SDK, the closest this I found is how to create an image from the EBS volume like so:

session = boto3.Session(...)
ec2 = session.client("ec2") 
ec2.create_image(...)

as explained here

I would like to save the need to find out the volume ID for the instance, and just use the instance ID, is that possible?

I tried to follow this Using this code:

ec2.create_image(instance_id) 

But got this error

TypeError: create_image() only accepts keyword arguments.

like image 656
thebeancounter Avatar asked Oct 15 '25 13:10

thebeancounter


2 Answers

You should be able to simply pass an Instance ID, using the following:

ec2.create_image(InstanceId=instance_id, Name="abc").

Note that this will attempt to reboot your EC2 instance. If you don't want the instance to reboot, use:

ec2.create_image(InstanceId=instance_id, NoReboot=True, Name="abc")

like image 83
Mark B Avatar answered Oct 18 '25 06:10

Mark B


When trying the same i got an error ->

"AttributeError: 'ec2.ServiceResource' object has no attribute 'create_image'"

so my suggestion is use

"boto3.client('ec2').create_image" instead of "ec2.create_image"
like image 31
manish soni Avatar answered Oct 18 '25 07:10

manish soni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!