Skip to content

Instantly share code, notes, and snippets.

@gwsu2008
Last active January 3, 2020 05:35
Show Gist options
  • Save gwsu2008/6650742c7edc4a3040ba13b1916d805f to your computer and use it in GitHub Desktop.
Save gwsu2008/6650742c7edc4a3040ba13b1916d805f to your computer and use it in GitHub Desktop.
Python boto3 client exception
"""
===========================================
['Error']['Code'] e.g. 'EntityAlreadyExists' or 'ValidationError'
['ResponseMetadata']['HTTPStatusCode'] e.g. 400
['ResponseMetadata']['RequestId'] e.g. 'd2b06652-88d7-11e5-99d0-812348583a35'
['Error']['Message'] e.g. "An error occurred (EntityAlreadyExists) ..."
['Error']['Type'] e.g. 'Sender'
""""
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. Received error: %s", e, exc_info=True"
else:
print "Unexpected error: %s" % e
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment