boto3 client NoRegionError: You must specify a region error only sometimes

后端 未结 7 1653
误落风尘
误落风尘 2021-01-30 07:30

I have a boto3 client :

boto3.client(\'kms\')

But it happens on new machines, They open and close dynamically.

    if endpoint         


        
7条回答
  •  感情败类
    2021-01-30 08:15

    For Python 2 I have found that the boto3 library does not source the region from the ~/.aws/config if the region is defined in a different profile to default. So you have to define it in the session creation.

    session = boto3.Session(
        profile_name='NotDefault',
        region_name='ap-southeast-2'
    )
    
    print(session.available_profiles)
    
    client = session.client(
        'ec2'
    )
    

    Where my ~/.aws/config file looks like this:

    [default]
    region=ap-southeast-2
    
    [NotDefault]
    region=ap-southeast-2
    

    I do this because I use different profiles for different logins to AWS, Personal and Work.

提交回复
热议问题