Last active
September 26, 2019 19:05
-
-
Save phin3has/7d10c0f3f710d47e62f68e9276078e63 to your computer and use it in GitHub Desktop.
Start 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.start_instances( | |
InstanceIds=[ | |
instID, | |
], | |
DryRun = False, | |
) | |
print("* - Starting the instance") | |
else: | |
print("!! - Instance seems to be running.") | |
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