+1 vote
1 view
in AWS by (1.4k points)
edited by

I'm using python for my web application and boto3 to integrate AWS services in my code. How to stop an Instance using Python boto3?

Any suggestions would be helpful.

1 Answer

0 votes
by (2.6k points)
edited by

The below code can be used for stopping or terminating aws ec2 instance using Python boto3

import boto3

ids = ['i-0bec2a0bf000bb71c']

ec2 = boto3.resource('ec2')

ec2.instances.filter(InstanceIds = ids).stop() #for stopping an ec2 instance

ec2.instances.filter(InstanceIds = ids).terminate() #for terminating an ec2 instance

And for stopping and terminating multiple instances 

 ids = ['instance-id-1', 'instance-id-2', ...]

# Boto 2.x

ec2_connection.stop_instances(instance_ids=ids)

ec2_connection.terminate_instances(instance_ids=ids)

# Boto 3

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

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

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...