如果您使用的是客户端,则可以捕获如下异常:
import boto3def exists(role_name): client = boto3.client('iam') try: client.get_role(RoleName='foo') return True except client.exceptions.NoSuchEntityException: return False
如果您使用的是客户端,则可以捕获如下异常:
import boto3def exists(role_name): client = boto3.client('iam') try: client.get_role(RoleName='foo') return True except client.exceptions.NoSuchEntityException: return False