boto3
AWS S3
credentials
Python
cloud computing

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.

Practice system design

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:

bash
pip install boto3

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 &#126;/.aws/credentials on Unix-based systems or C:\Users\<YourUsername>\.aws\credentials on Windows. Here is an example of how this file might look:

 
[default]
aws_access_key_id = YOUR_ACCESS_KEY_ID
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY

You can use multiple profiles and specify the profile to use in your boto3 script:

ini
1[default]
2aws_access_key_id = YOUR_DEFAULT_ACCESS_KEY_ID
3aws_secret_access_key = YOUR_DEFAULT_SECRET_ACCESS_KEY
4
5[development]
6aws_access_key_id = YOUR_DEV_ACCESS_KEY_ID
7aws_secret_access_key = YOUR_DEV_SECRET_ACCESS_KEY

In your Python script, specify the profile like this:

python
1import boto3
2
3session = boto3.Session(profile_name='development')
4s3 = session.resource('s3')

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

bash
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY

Windows

cmd
set AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
set AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY

Boto3 automatically picks up these credentials:

python
import boto3

s3 = boto3.client('s3')

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:

python
1import boto3
2
3s3 = boto3.client(
4    's3',
5    aws_access_key_id='YOUR_ACCESS_KEY_ID',
6    aws_secret_access_key='YOUR_SECRET_ACCESS_KEY'
7)

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:

python
import boto3

s3 = boto3.resource('s3')

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:

MethodDescriptionUse Case
Credentials FileUses &#126;/.aws/credentials with profiles. Manages multiple profiles.Multi-profile setups
Environment VariablesSets environment variables for credentials.Highly secure automation environments
Hard-Coding in CodeSpecifies credentials directly in the code.Quick tests (discouraged for production)
IAM Roles for EC2Assigns 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.