How to specify credentials when connecting to boto3 S3?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
When you create a Boto3 S3 client, you usually do not pass raw credentials directly in code. Boto3 already knows how to search for credentials through AWS's provider chain, so the real question is which source you want it to use and when you should override the default behavior.
How Boto3 Finds Credentials
Boto3 looks for AWS credentials in a defined order, with some sources preferred over others. In practice, the most common choices are:
- An IAM role attached to the compute environment
- Environment variables
- A named profile in
~/.aws/credentials - Explicit credentials in code
You can often let Boto3 resolve credentials automatically:
That works when valid credentials already exist somewhere in the provider chain.
Option 1: Environment Variables
Environment variables are a simple way to supply credentials locally or in short-lived automation.
Then your Python code can stay minimal:
This is easy to set up, but it also makes accidental credential leakage easier if shells, logs, or process environments are not handled carefully.
Option 2: Shared AWS Credentials Profile
For local development, a named profile is usually cleaner than exporting keys over and over.
Example ~/.aws/credentials:
Python code:
Profiles are especially useful when you switch between accounts or roles regularly.
Option 3: Temporary Credentials
If you use STS or a federated login flow, you may also need a session token. In that case, include all three credential values.
Temporary credentials are common in secure environments because they expire automatically, which reduces the blast radius of leaked secrets.
Option 4: IAM Role on AWS Infrastructure
If your code runs on EC2, ECS, EKS, or Lambda, the best practice is usually to attach an IAM role and let Boto3 fetch credentials automatically.
No hardcoded secrets are needed, and credential rotation is handled by AWS. This is the preferred production path for most AWS-hosted workloads.
When Explicit Credentials in Code Make Sense
Passing credentials directly to boto3.client is usually a last resort. It can be acceptable for a short-lived local script or a controlled test, but it is not a good default for long-lived application code.
If you must do it, avoid committing secrets to source control and keep the values outside the code file itself, such as in a secret manager or temporary environment injection.
That keeps the code path explicit without normalizing insecure habits across the project.
Common Pitfalls
- Forgetting
region_namecan triggerNoRegionErroreven when the credentials are valid. - Using temporary credentials without
aws_session_tokencauses confusing authentication failures. - Mixing environment variables, profiles, and explicit session configuration can lead to "wrong account" problems when Boto3 chooses a different source than you expected.
- Hardcoding long-lived credentials into application code creates unnecessary security risk.
Summary
- Prefer the default AWS credential provider chain instead of hardcoding secrets.
- Use profiles or environment variables for local development.
- Use IAM roles for AWS-hosted production workloads.
- Include a session token when working with temporary credentials.
Related reading
- How to SSH into a Kubernetes Node or Server
- How to SSH into a Kubernetes Node or Server
- How to stop logging excessive ServiceBusReceiver.Receive Dependency logs to App Insights
- how to stop/pause a pod in kubernetes
- How to specify multiple types using type-hints
- How to specify nullable return type with type hints
- How to store AWS Cognito User Pool users in DB for instance DynamoDB?
- How to store user information with DynamoDB and Cognito using Facebook authentication with iOS SDK

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.