Reboot an AWS EC2 Instance (Python)
from __future__ import print_function
import sys
import boto3
from botocore.exceptions import ClientError
boto3.setup_default_session(profile_name='default')
instance_id = 'i-094cf9bbbea2bdfbd' #sys.argv[2]
#action = sys.argv[1].upper()
ec2 = boto3.client('ec2')
# Do a dryrun first to verify permissions
try:
ec2.start_instances(InstanceIds=[instance_id], DryRun=True)
except ClientError as e:
if 'DryRunOperation' not in str(e):
raise
# Dry run succeeded, run start_instances without dryrun
try:
response = ec2.start_instances(InstanceIds=[instance_id], DryRun=False)
print(response)
except ClientError as e:
print(e)