PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

It seems PynamoDB does not use region set in environment variable

Open yongyili opened this issue 3 years ago • 5 comments

Hi, I have below simple model

class User(Model):
    class Meta:
        table_name = 'UserTable'

    UserId = UnicodeAttribute(hash_key=True)

And I try to get a user in my code:

User.get('my_id')

But I get the error The security token included in the request is invalid')

According to AWS document, I have tried to set environment viable like below:

os.environ['AWS_DEFAULT_REGION'] = 'cn-north-1'
os.environ['AWS_REGION'] = 'cn-north-1'

But the error persists.

So, I have to set the region like below:

class User(Model):
    class Meta:
        table_name = 'UserTable'
        region = os.environ.get(['AWS_DEFAULT_REGION', None)

    UserId = UnicodeAttribute(hash_key=True)

I have 3 questions:

  1. Why does PynamoDB not read environment variable automatically like boto3 does?
  2. Do I have to set region manually in the Meta class?
  3. Which region would PynamoDB use if I run my code in AWS Lambda?

yongyili avatar Apr 28 '22 10:04 yongyili

PynamoDB does get the region from the environment variables automatically. That's why inside a Lambda function it will query the database in the region AWS_REGION defined by Lambda here: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime

You are probably making a mistake in setting your environment variables outside the script that gets the user. I suggest we close this issue.

mstykow avatar May 18 '22 17:05 mstykow

@yongyili I am experiencing the same issue. It seems that pynamodb ignores both "AWS_DEFAULT_REGION" and "AWS_REGION" unless manually set

alex-janss avatar May 20 '22 17:05 alex-janss

I had the same issue and it drove me absolutely crazy. Needed to set the region argument manually

TheGrandmother avatar May 24 '22 09:05 TheGrandmother

from the docs: "By default, PynamoDB will connect to the us-east-1 region, but you can specify a different one." https://pynamodb.readthedocs.io/en/latest/low_level.html

alex-janss avatar May 24 '22 17:05 alex-janss

I think there was already an open issue for that and it should be fixed within the next major release.

See: https://github.com/pynamodb/PynamoDB/pull/1003

ospikovets avatar May 25 '22 11:05 ospikovets