Skip to content

Instantly share code, notes, and snippets.

@coingraham
Forked from stewmi/boto3_handleexceptions.py
Created August 27, 2019 16:29
Show Gist options
  • Save coingraham/dc6585ed010b3b3b0c5aa39468052c1d to your computer and use it in GitHub Desktop.
Save coingraham/dc6585ed010b3b3b0c5aa39468052c1d to your computer and use it in GitHub Desktop.
Handling Exceptions in Boto3
import boto3
from botocore.exceptions import ClientError
try:
iam = boto3.client('iam')
user = iam.create_user(UserName='fred')
print("Created user: %s" % user)
except ClientError as e:
if e.response['Error']['Code'] == 'EntityAlreadyExists':
print("User already exists")
else:
print("Unexpected error: %s" % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment