标签归档:aws-kms

boto3客户端NoRegionError:仅在某些情况下必须指定区域错误

问题:boto3客户端NoRegionError:仅在某些情况下必须指定区域错误

我有一个boto3客户程序:

boto3.client('kms')

但是它发生在新机器上,它们动态地打开和关闭。

    if endpoint is None:
        if region_name is None:
            # Raise a more specific error message that will give
            # better guidance to the user what needs to happen.
            raise NoRegionError()

为什么会这样呢?为什么只有部分时间呢?

I have a boto3 client :

boto3.client('kms')

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

    if endpoint is None:
        if region_name is None:
            # Raise a more specific error message that will give
            # better guidance to the user what needs to happen.
            raise NoRegionError()

Why is this happening? and why only part of the time?


回答 0

您必须以一种或另一种方式告诉boto3您希望在哪个区域kms创建客户端。可以使用region_name参数明确地完成此操作,如下所示:

kms = boto3.client('kms', region_name='us-west-2')

或者,您可以在~/.aws/config文件中拥有一个与个人资料相关联的默认区域,如下所示:

[default]
region=us-west-2

或者您可以使用如下环境变量:

export AWS_DEFAULT_REGION=us-west-2

但您确实需要告诉boto3使用哪个区域。

One way or another you must tell boto3 in which region you wish the kms client to be created. This could be done explicitly using the region_name parameter as in:

kms = boto3.client('kms', region_name='us-west-2')

or you can have a default region associated with your profile in your ~/.aws/config file as in:

[default]
region=us-west-2

or you can use an environment variable as in:

export AWS_DEFAULT_REGION=us-west-2

but you do need to tell boto3 which region to use.


回答 1

os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'

就我而言,敏感性很重要。

os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'

In my case sensitivity mattered.


回答 2

我相信默认情况下,boto会选择aws cli中设置的区域。您可以运行#aws configure命令,然后按Enter键(它显示您在aws cli中使用区域设置的凭据)两次以确认您的区域。

I believe, by default, boto picks the region which is set in aws cli. You can run command #aws configure and press enter (it shows what creds you have set in aws cli with region)twice to confirm your region.


回答 3

您还可以在脚本本身中设置环境变量,而不是传递region_name参数

os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'

区分大小写可能很重要。

you can also set environment variables in the script itself, rather than passing region_name parameter

os.environ['AWS_DEFAULT_REGION'] = 'your_region_name'

case sensitivity may matter.


回答 4

对于Python 2,我发现,~/.aws/config如果在默认的其他配置文件中定义了区域,则boto3库不会从中获取区域。因此,您必须在会话创建中对其进行定义。

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

print(session.available_profiles)

client = session.client(
    'ec2'
)

我的~/.aws/config文件如下所示:

[default]
region=ap-southeast-2

[NotDefault]
region=ap-southeast-2

之所以这样做,是因为我将不同的配置文件用于对AWS,Personal和Work的不同登录。

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.


回答 5

对于那些使用CloudFormation模板的人。您可以AWS_DEFAULT_REGION使用UserData和设置环境变量AWS::Region。例如,

MyInstance1:
    Type: AWS::EC2::Instance                
    Properties:                           
        ImageId: ami-04b9e92b5572fa0d1 #ubuntu
        InstanceType: t2.micro
        UserData: 
            Fn::Base64: !Sub |
                    #!/bin/bash -x

                    echo "export AWS_DEFAULT_REGION=${AWS::Region}" >> /etc/profile

For those using CloudFormation template. You can set AWS_DEFAULT_REGION environment variable using UserData and AWS::Region. For example,

MyInstance1:
    Type: AWS::EC2::Instance                
    Properties:                           
        ImageId: ami-04b9e92b5572fa0d1 #ubuntu
        InstanceType: t2.micro
        UserData: 
            Fn::Base64: !Sub |
                    #!/bin/bash -x

                    echo "export AWS_DEFAULT_REGION=${AWS::Region}" >> /etc/profile