Last active
September 26, 2019 19:04
-
-
Save phin3has/0287686e7086f895a0b896609222b53e to your computer and use it in GitHub Desktop.
Stop an EC2 instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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