How to terminate EC2 instance using boto3?

0 votes

Hi Guys,

I have launched some EC2 instances. Now I want to terminate these instances using boto3. How can I do that?

Oct 7 in AWS by akhtar
• 33,470 points
97 views

1 answer to this question.

0 votes

Hi@akhtar,

We can create and modify an EC2 instance using boto3 also provided a method to terminate instances that are no longer required. If the following code sample is saved in a python file ec2.py,

import boto3, sys
ec2 = boto3.resource('ec2')
# iterate through instance IDs and terminate them
for id in sys.argv[1:]:
 instance = ec2.Instance(id)
 print(instance.terminate())
answered Oct 7 by MD
• 79,390 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,125 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to modify EC2 Instance Properties using boto3?

Hi@akhtar, To modify an EC2 instance, first, you ...READ MORE

answered Oct 7 in AWS by MD
• 79,390 points
132 views