With code like the snippet below, we can catch AWS exceptions:
from aws_utils import make_session
session = make_session()
cf = session.resource(\"iam\")
ro
If you're using the client you can catch the exceptions like this:
import boto3
def exists(role_name):
client = boto3.client('iam')
try:
client.get_role(RoleName='foo')
return True
except client.exceptions.NoSuchEntityException:
return False
try: something except client.exceptions.NoSuchEntityException: something
This worked for me
If you're using the resource you can catch the exceptions like this:
cf = session.resource("iam")
role = cf.Role("foo")
try:
role.load()
except cf.meta.client.exceptions.NoSuchEntityException:
# ignore the target exception
pass
This combines the earlier answer with the simple trick of using .meta.client
to get from the higher-level resource to the lower-level client.