ankit tyagi
ankit tyagi

Reputation: 846

set custom boto configuration file other than default for boto3

I'm not getting any option to set custom configuration file location with boto3. I can use credentials from default location.

My use case is, there are different IAM users and have different set of credentials so each one should be able to use their own credentials without changing default configuration.

Any ideas?

Upvotes: 1

Views: 2839

Answers (3)

Viraj Wadate
Viraj Wadate

Reputation: 6183

Simply you can use python module configparser

Example will show to check status of RDS instance and store your aws credentials file anywhere it will work.

import configparser
import boto3

class CheckStatus():

    rds_instance_name = 'rds_instnace_name'
    aws_config_file = 'aws_config.ini'

    def __init__(self, aws_config):

        self.aws_config = aws_config
        self.client = boto3.client('rds', region_name=self.aws_config['default']['region'],
                                   aws_access_key_id=self.aws_config['default']['aws_access_key_id'],
                                   aws_secret_access_key=self.aws_config['default']['aws_secret_access_key'])

    def check_rds_status(self):
        current_status = None

        response = self.client.describe_db_instances(
            DBInstanceIdentifier=self.rds_instance_name)
        current_status = response['DBInstances'][0]['DBInstanceStatus']
        print(current_status)


def get_config():
    aws_config = configparser.ConfigParser()
    aws_config.read(CheckStatus.aws_config_file)
    return aws_config

obj = CheckStatus(get_config())

obj.check_rds_status()

Upvotes: 1

kameranis
kameranis

Reputation: 284

I believe this will help: http://boto.cloudhackers.com/en/latest/boto_config_tut.html#credentials

You can set different credentials for different users.

Upvotes: 1

Mani
Mani

Reputation: 473

This is eminently doable. You can achieve this by assigning some keyword arguments that are passed through the boto3.Session object, e.g.,

import boto3
session = boto3.Session(
    aws_access_key_id="AAAA",
    aws_secret_access_key="BBBBB",
    region_name="mordor"
)

Then just use your session object as normal:

s3_object = session.resource('s3') # or whatever

So all you'll need to do is fill some python data structure with the credentials you want (populated from your custom configuration file), and pass them into that boto3.Session object.

Upvotes: 0

Related Questions