Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using aws profiles in python2 boto

I'd like my python script (connecting to aws) to use aws profiles from ~/.aws/credentials

So, I have ~/.aws/credentials file:

[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

import boto.ec2

conn = boto.ec2.EC2Connection(profile_name='test')
print conn
print conn.get_all_instances()

The output is

EC2Connection:ec2.us-east-1.amazonaws.com
[]

So, it doesn't seem to take the profile from the config. The region is wrong.

env | grep -i aws <- returns nothing

What am I missing?

Thank you.

===== UPDATE =====

I have a ~/.aws/config file too which looks like

⟩ 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

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.

like image 750
kikulikov Avatar asked May 19 '15 15:05

kikulikov


1 Answers

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:

[default]
region = eu-west-1

[test]
region = eu-west-1
like image 138
garnaat Avatar answered Sep 27 '22 22:09

garnaat