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 working with Amazon Web Services (AWS) Simple Storage Service (S3) using the Python SDK boto3, specifying credentials correctly is crucial for securely connecting to and interacting with S3 resources. Credentials need to be configured to authenticate requests to AWS services, enabling seamless integration and access control. In this article, we will explore various methods for specifying credentials when connecting to S3 using boto3, ensuring a secure and efficient setup.
Setting Up Boto3
Installation
First, ensure that boto3 is installed in your Python environment. If it is not installed, you can do so using pip:
Background on AWS Credentials
AWS credentials typically consist of an Access Key ID and a Secret Access Key. These credentials can be obtained from the AWS Management Console. Properly managing these credentials is essential to maintaining security and access control in your AWS environment.
Method 1: Using AWS Credentials File
The AWS credentials file is a common way to store credentials for AWS SDKs, including boto3. This file is typically located in your home directory at ~/.aws/credentials on Unix-based systems or C:\Users\<YourUsername>\.aws\credentials on Windows. Here is an example of how this file might look:
You can use multiple profiles and specify the profile to use in your boto3 script:
In your Python script, specify the profile like this:
Method 2: Environment Variables
Environment variables offer another way to store AWS credentials, reducing the need to embed them directly in your code. Set the environment variables in your operating system:
Unix-based Systems
Windows
Boto3 automatically picks up these credentials:
Method 3: Directly in Code
Embedding credentials directly in code is not recommended due to security concerns, but it can be done for quick tests or demonstrations. Here’s an example:
Method 4: IAM Roles for EC2
When running applications on AWS EC2 instances, you can assign an IAM role to your instance. These roles assign temporary credentials to the applications running on the EC2 instance, eliminating the need to manage long-term credentials.
- Step 1: Create an IAM role with the necessary permissions.
- Step 2: Attach the role to your EC2 instance.
Boto3 automatically utilizes these temporary credentials:
Security Best Practices
- Use IAM Roles: Prefer IAM roles for applications running within AWS infrastructure to manage short-lived credentials.
- Do Not Hard Code Credentials: Avoid including AWS credentials directly in your source code repositories.
- Rotate Keys Regularly: Regularly rotate your access keys to minimize security risks.
- Scope Permissions: Use least privilege principle by scoping IAM policies strictly to the resources and actions needed.
Summary Table
Here's a succinct table summarizing the methods for specifying credentials:
| Method | Description | Use Case |
| Credentials File | Uses ~/.aws/credentials with profiles.
Manages multiple profiles. | Multi-profile setups |
| Environment Variables | Sets environment variables for credentials. | Highly secure automation environments |
| Hard-Coding in Code | Specifies credentials directly in the code. | Quick tests (discouraged for production) |
| IAM Roles for EC2 | Assigns roles to EC2 with temporary credentials. | Applications running on EC2 instances |
Conclusion
Specifying credentials when connecting to AWS S3 using boto3 involves selecting the right method based on your security and infrastructure requirements. Whether you choose environment variables, an AWS credentials file, or IAM roles, make sure to adhere to best practices for security and resource management. By effectively managing your AWS credentials, you ensure secure and authorized access to your S3 resources while mitigating potential security risks.
Related reading
- How to specify credentials when connecting to boto3 S3?
- 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 specify multiple types using type-hints
- How to specify nullable return type with type hints
- how to stop/pause a pod in kubernetes
- How to store AWS Cognito User Pool users in DB for instance DynamoDB?

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.