How does boto3 find credentials?
BeginnerConceptboto3 & AWS automation
Answer
The credential provider chain, in order (simplified):
- Explicit parameters to
Session/client(avoid). - Environment variables
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN. - Web identity token (
AWS_WEB_IDENTITY_TOKEN_FILE+AWS_ROLE_ARN), used by IRSA and some CI setups. - Shared credentials file
~/.aws/credentials, then~/.aws/configprofiles (including SSO androle_arn/source_profileassume-role). - Container credentials (ECS task role, EKS Pod Identity via
AWS_CONTAINER_CREDENTIALS_FULL_URI). - EC2 instance metadata (IMDSv2) instance profile.
AWS_PROFILE selects the profile. The first source found wins, which is why a stale exported key overrides your SSO profile.
More on boto3 & AWS automation
What is the difference between a boto3 Session, client and resource?BeginnerHow does boto3 decide the region, and what is the gotcha with `AWS_REGION`?AdvancedWhy do you need paginators, and how do you use them?BeginnerWhat is JMESPath `search()` on paginators?AdvancedHow do you handle errors from boto3?BeginnerWhat are waiters, and why use them instead of `sleep` loops?Intermediate