Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the region of the current user from boto?

Problem:

I'm trying to get the region of the authenticated user from boto3.

Use case:

I'm working on adding cache to https://github.com/pmazurek/aws-fuzzy-finder. I would prefer to cache the result on per-region basis.

This package uses boto to get user authentication data (keys and the region). The problem is the region is never passed explicitly by the user, its being taken from one of the many murky places that boto reads so I don't really have a way of getting it.

I have tried searching through boto3 api and googling, but couldn't find anything like a get_region or get_user_data method. Is it possible?

like image 419
Piotr Mazurek Avatar asked May 29 '16 20:05

Piotr Mazurek


People also ask

What is Boto3 session ()?

The boto3. Session class, according to the docs, “ stores configuration state and allows you to create service clients and resources.” Most importantly it represents the configuration of an IAM identity (IAM user or assumed role) and AWS region, the two things you need to talk to an AWS service.

Is Boto and Boto3 same?

Boto versions include Boto, Boto3 and Botocore. Boto3 is the latest version of the SDK, providing support for Python versions 2.6. 5, 2.7 and 3.3. Boto3 includes several service-specific features to ease development.

How do I know what version of Boto 3 I have?

All AWS services and arguments are now available to your Lambda function. Tip: Use print(boto3. __version__) and print(botocore. __version__) in your function code to confirm the version of Boto 3 and Botocore.


2 Answers

You should be able to read the region_name from the session.Session object like

my_session = boto3.session.Session() my_region = my_session.region_name 

region_name is basically defined as session.get_config_variable('region')

like image 200
Frederic Henri Avatar answered Oct 11 '22 03:10

Frederic Henri


Another option, if you are working with a boto3 client, is:

import boto3 client = boto3.client('s3') # example client, could be any client.meta.region_name 
like image 27
Steven Miller Avatar answered Oct 11 '22 03:10

Steven Miller