Skip to content

Instantly share code, notes, and snippets.

@phin3has
Last active September 26, 2019 19:04
Show Gist options
  • Save phin3has/0287686e7086f895a0b896609222b53e to your computer and use it in GitHub Desktop.
Save phin3has/0287686e7086f895a0b896609222b53e to your computer and use it in GitHub Desktop.
Stop an EC2 instance
#! /usr/bin/python3
import boto3
session = boto3.Session(profile_name='default', region_name='us-west-2')
client = session.client('ec2')
instID = 'ID'
response = client.describe_instance_status(
InstanceIds=[
instID,
],
DryRun = False,
IncludeAllInstances = True,
)
status = response['InstanceStatuses'][0]['InstanceState']['Code']
"""
Status codes:
0 : pending
16 : running
32 : shutting-down
48 : terminated
64 : stopping
80 : stopped
"""
if status == 16:
response = client.stop_instances(
InstanceIds=[
instID,
],
DryRun = False,
)
print("* - Stopping the Instance")
else:
print("!! - Instance seems stopped already")
print("!! - Status Code: " + str(status))
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment