Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

How can I delete an EC2 instance using Python boto3?

Can someone help me with the code?

1 Answer

0 votes
by (25.1k points)

You can do this very easily using the boto3 library.

all you need is to create a list of ids of all the ec2 instances you wish to terminate. Then using the filter method pass in that list of ids as a keyworded argument named InstanceIds and call the terminate method on the returned value and you are done.

Here is the code for the same:

import boto3

ids = ['e1','e2','e3','e4','e5']

ec2 = boto3.resource('ec2')

ec2.instances.filter(InstanceIds = ids).terminate()

In case you wish to learn more about python and it's libraries in-depth, you can watch this video:

Related questions

0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...