How to handle errors with boto3? How to handle errors with boto3? python python

How to handle errors with boto3?

Use the response contained within the exception. Here is an example:

import boto3from botocore.exceptions import ClientErrortry:    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)

The response dict in the exception will contain the following:

  • ['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'

For more information see:

[Updated: 2018-03-07]

The AWS Python SDK has begun to expose service exceptions on clients (though not on resources) that you can explicitly catch, so it is now possible to write that code like this:

import botocoreimport boto3try:    iam = boto3.client('iam')    user = iam.create_user(UserName='fred')    print("Created user: %s" % user)except iam.exceptions.EntityAlreadyExistsException:    print("User already exists")except botocore.exceptions.ParamValidationError as e:    print("Parameter validation error: %s" % e)except botocore.exceptions.ClientError as e:    print("Unexpected error: %s" % e)

Unfortunately, there is currently no documentation for these errors/exceptions but you can get a list of the core errors as follows:

import botocoreimport boto3[e for e in dir(botocore.exceptions) if e.endswith('Error')]

Note that you must import both botocore and boto3. If you only import botocore then you will find that botocore has no attribute named exceptions. This is because the exceptions are dynamically populated into botocore by boto3.

You can get a list of service-specific exceptions as follows (replace iam with the relevant service as needed):

import boto3iam = boto3.client('iam')[e for e in dir(iam.exceptions) if e.endswith('Exception')]

[Updated: 2021-09-07]

In addition to the aforementioned client exception method, there is also a third-party helper package named aws-error-utils.

I found it very useful, since the Exceptions are not documented, to list all exceptions to the screen for this package. Here is the code I used to do it:

import botocore.exceptionsdef listexns(mod):    #module = __import__(mod)    exns = []    for name in botocore.exceptions.__dict__:        if (isinstance(botocore.exceptions.__dict__[name], Exception) or            name.endswith('Error')):            exns.append(name)    for name in exns:        print('%s.%s is an exception type' % (str(mod), name))    returnif __name__ == '__main__':    import sys    if len(sys.argv) <= 1:        print('Give me a module name on the $PYTHONPATH!')    print('Looking for exception types in module: %s' % sys.argv[1])    listexns(sys.argv[1])

Which results in:

Looking for exception types in module: boto3boto3.BotoCoreError is an exception typeboto3.DataNotFoundError is an exception typeboto3.UnknownServiceError is an exception typeboto3.ApiVersionNotFoundError is an exception typeboto3.HTTPClientError is an exception typeboto3.ConnectionError is an exception typeboto3.EndpointConnectionError is an exception typeboto3.SSLError is an exception typeboto3.ConnectionClosedError is an exception typeboto3.ReadTimeoutError is an exception typeboto3.ConnectTimeoutError is an exception typeboto3.ProxyConnectionError is an exception typeboto3.NoCredentialsError is an exception typeboto3.PartialCredentialsError is an exception typeboto3.CredentialRetrievalError is an exception typeboto3.UnknownSignatureVersionError is an exception typeboto3.ServiceNotInRegionError is an exception typeboto3.BaseEndpointResolverError is an exception typeboto3.NoRegionError is an exception typeboto3.UnknownEndpointError is an exception typeboto3.ConfigParseError is an exception typeboto3.MissingParametersError is an exception typeboto3.ValidationError is an exception typeboto3.ParamValidationError is an exception typeboto3.UnknownKeyError is an exception typeboto3.RangeError is an exception typeboto3.UnknownParameterError is an exception typeboto3.AliasConflictParameterError is an exception typeboto3.PaginationError is an exception typeboto3.OperationNotPageableError is an exception typeboto3.ChecksumError is an exception typeboto3.UnseekableStreamError is an exception typeboto3.WaiterError is an exception typeboto3.IncompleteReadError is an exception typeboto3.InvalidExpressionError is an exception typeboto3.UnknownCredentialError is an exception typeboto3.WaiterConfigError is an exception typeboto3.UnknownClientMethodError is an exception typeboto3.UnsupportedSignatureVersionError is an exception typeboto3.ClientError is an exception typeboto3.EventStreamError is an exception typeboto3.InvalidDNSNameError is an exception typeboto3.InvalidS3AddressingStyleError is an exception typeboto3.InvalidRetryConfigurationError is an exception typeboto3.InvalidMaxRetryAttemptsError is an exception typeboto3.StubResponseError is an exception typeboto3.StubAssertionError is an exception typeboto3.UnStubbedResponseError is an exception typeboto3.InvalidConfigError is an exception typeboto3.InfiniteLoopConfigError is an exception typeboto3.RefreshWithMFAUnsupportedError is an exception typeboto3.MD5UnavailableError is an exception typeboto3.MetadataRetrievalError is an exception typeboto3.UndefinedModelAttributeError is an exception typeboto3.MissingServiceIdError is an exception type

Just an update to the 'no exceptions on resources' problem as pointed to by @jarmod (do please feel free to update your answer if below seems applicable)

I have tested the below code and it runs fine. It uses 'resources' for doing things, but catches the client.exceptions - although it 'looks' somewhat wrong... it tests good, the exception classes are showing and matching when looked into using debugger at exception time...

It may not be applicable to all resources and clients, but works for data folders (aka s3 buckets).

lab_session = boto3.Session() c = lab_session.client('s3') #this client is only for exception catchingtry:    b = s3.Bucket(bucket)    b.delete()except c.exceptions.NoSuchBucket as e:    #ignoring no such bucket exceptions    logger.debug("Failed deleting bucket. Continuing. {}".format(e))except Exception as e:    #logging all the others as warning    logger.warning("Failed deleting bucket. Continuing. {}".format(e))

Hope this helps...