Skip to content

boto3.Session ignores AWS_PROFILE when there are other credentials set in the environment #4678

@primerano

Description

@primerano

Describe the bug

boto3.Session() looks at AWS_PROFILE but ignores it if there are credentials specified in the environment.

Using boto3.Session(profile_name=os.environ['AWS_PROFILE']) fixes the issue but this should not be necessary.

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

I expect boto3.Session() to use the credentials provided by the SSO profile specified via AWS_PROFILE instead of the inline credentials.

Current Behavior

If the following variables are set the AWS_PROFILE is not used even though boto3.Session().profile_name will show what is set in AWS_PROFILE

AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN

Reproduction Steps

If I set AWS_PROFILE=FOO, startup python and run

import boto3
session = boto3.Session()
print(session.profile_name)

It will print FOO and I will see the details of my FOO session in session.client('sts').get_caller_identity() as expect.

Note: the FOO profile is an AWS SSO profile.

Now if I paste in credentials for a different user

export AWS_ACCESS_KEY_ID="the key"
export AWS_SECRET_ACCESS_KEY="the secret"
export AWS_SESSION_TOKEN="the session"

and re-run

AWS_PROFILE=FOO python

and in python run

import boto3
session = boto3.Session()
print(session.profile_name)

it will print "FOO" but this profile will not be used by boto3. it uses the credentials from the 3 AWS variables instead.

This is confirmed by running session.client('sts').get_caller_identity()

Now, if I instead create the session with the profile name

session = boto3.Session(profile_name=os.environ['AWS_PROFILE'])

it will now use the profile instead of the AWS_ACCESS_KEY_ID/SECRET/SESSION.

Considering that boto3.Session().profile_name correctly picks up the name from AWS_PROFILE it would seem that it should then use those credentials.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

aws-cli/2.30.2, boto3/1.42.6 botocore/1.42.6

Environment details (OS name and version, etc.)

al2023 and ubuntu 22

Metadata

Metadata

Assignees

Labels

p3This is a minor priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions