Question:
I’d like my python script (connecting to aws) to use aws profiles from ~/.aws/credentials
So, I have ~/.aws/credentials file:
1 2 3 4 5 6 7 8 9 10 |
[default] aws_access_key_id = XXX aws_secret_access_key = AAA region = eu-west-1 [test] aws_access_key_id = WWW aws_secret_access_key = ZZZ region = eu-west-1 |
My python code
1 2 3 4 5 6 |
import boto.ec2 conn = boto.ec2.EC2Connection(profile_name='test') print conn print conn.get_all_instances() |
The output is
1 2 3 |
EC2Connection:ec2.us-east-1.amazonaws.com [] |
So, it doesn’t seem to take the profile from the config. The region is wrong.
1 2 |
env | grep -i aws <- returns nothing |
What am I missing?
Thank you.
===== UPDATE =====
I have a ~/.aws/config file too which looks like
1 2 3 4 5 6 7 8 9 |
⟩ cat config [default] output = text region = eu-west-1 [profile test] output = text region = eu-west-1 |
===== UPDATE =====
apparently, it takes connection details.. but not region. So, if I do
1 2 3 4 5 6 |
reg = next(x for x in boto.ec2.regions() if x.name=='eu-west-1') conn = boto.ec2.EC2Connection(profile_name='test', region=reg) print conn print conn.get_all_instances() |
I can get a list of the instances.
So, the problem is how to get the default ‘region’ from AWS config/credentials.
Answer:
The credentials
file is supposed to be used only for credentials. If you want to supply other attributes for your profiles you should do that in the ~/.aws/config
file. So, in your case you would create a config file containing:
1 2 3 4 5 6 |
[default] region = eu-west-1 [test] region = eu-west-1 |