How to handle errors with boto3?

后端 未结 10 886
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 04:02

I am trying to figure how to do proper error handling with boto3.

I am trying to create an IAM user:

def create_user(username, iam_conn):
    try:
           


        
10条回答
  •  情话喂你
    2020-12-02 04:43

    • Only one import needed.
    • No if statement needed.
    • Use the client built-in exception as intended.

    Ex:

    from boto3 import client
    
    cli = client('iam')
    try:
        cli.create_user(
            UserName = 'Brian'
        )
    except cli.exceptions.EntityAlreadyExistsException:
        pass
    

    a CloudWatch example:

    cli = client('logs')
    try:
        cli.create_log_group(
            logGroupName = 'MyLogGroup'
        )
    except cli.exceptions.ResourceAlreadyExistsException:
        pass
    

提交回复
热议问题