PynamoDB
PynamoDB copied to clipboard
It seems PynamoDB does not use region set in environment variable
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:
- Why does PynamoDB not read environment variable automatically like boto3 does?
- Do I have to set region manually in the Meta class?
- Which region would PynamoDB use if I run my code in AWS Lambda?
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.
@yongyili I am experiencing the same issue. It seems that pynamodb ignores both "AWS_DEFAULT_REGION" and "AWS_REGION" unless manually set
I had the same issue and it drove me absolutely crazy. Needed to set the region argument manually
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
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