I am developing AWS DynamoDb tables in Pycharm. For this I have created a virtual environment with Python 3.6 and installed required libraries like boto3. I have also set my AWS credentials using AWS CLI tool in ~/.aws/credentials file.
Problem is when I simply run the code, it works like a charm and is able to read the credentials file. However, when I select to run the code in "Python console", I get the error that credentials have expired. It appears to me that somehow "Python console" is unable to access the ~/.aws/credentials file and is looking somewhere else for credentials. Or boto3 is not accessing the credentials file from ~/.aws/credentials when I select code to run in python console.
Can someone guide me as how to set up credentials in Python console so that I can run the code interactively.
Thanks,
I struggled with the same problem using Pycharm in Intellij.
Boto3 couldn't locate the credentials file and I didn't have a default profile set.
> os.environ["AWS_SHARED_CREDENTIALS_FILE"]
None
> os.environ["AWS_DEFAULT_PROFILE"]
None
Solution, I set the variables explicitly
> import boto3
> import os
> os.environ["AWS_SHARED_CREDENTIALS_FILE"] = "<full-path>/.aws/credentials"
> os.environ["AWS_DEFAULT_PROFILE"] = "<profile-name>"
> boto3.client('sts').get_caller_identity().get('Account')
1234567890
From Credentials — Boto 3 Docs 1.9.61 documentation:
The mechanism in which boto3 looks for credentials is to search through a list of possible locations and stop as soon as it finds credentials. The order in which Boto3 searches for credentials is:
- Passing credentials as parameters in the
boto.client()
method- Passing credentials as parameters when creating a Session object
- Environment variables
- Shared credential file (
~/.aws/credentials
)- AWS config file (
~/.aws/config
)- Assume Role provider
- Boto2 config file (
/etc/boto.cfg
and~/.boto
)- Instance metadata service on an Amazon EC2 instance that has an IAM role configured.
Therefore, if it isn't using the credentials file, it is probably getting credentials from Environment Variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With