AWS and boto3 recipes for Python
The code tries to stop the specified EC2 instances. If the instances aren't stopped, the code prints an error and terminates.
Library: boto3
import boto3
from botocore.exceptions import ClientError
try:
response = ec2.stop_instances(InstanceIds=[instanceid], DryRun=False)
print(response)
except ClientError as e:
logging.error("Cannot stop EC2 instance")
print(e)