AWS and boto3 recipes for Python
When this line of code is executed, the DryRun flag is set to True, which tells the code to run the command without actually starting any instances. If an error occurs while trying to start instances, an exception is raised and the code prints out the error.
Library: boto3
import sys
import boto3
from botocore.exceptions import ClientError
try:
# Add DryRun first if you want to check the operation can be done
response = ec2.start_instances(InstanceIds=[instanceid], DryRun=False)
print(response)
except ClientError as e:
logging.info("Cannot start EC2 instance")