How to choose an AWS profile when using boto3 to connect to CloudFront
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with AWS services using Boto3, the Python SDK for AWS, you may encounter scenarios where multiple AWS accounts are in use. Each account may require specific configurations, often stored in different profiles. This article will guide you through choosing or specifying an AWS profile when connecting to AWS CloudFront or any other service using Boto3.
Boto3 and AWS Profiles
Boto3 provides the flexibility to manage multiple AWS account credentials using AWS profiles. Profiles are stored in the AWS credentials file, typically located at ~/.aws/credentials on Unix-based systems or C:\Users\<Username>\.aws\credentials on Windows.
Setting Up AWS Profiles
You can define multiple profiles in the credentials file. Each profile contains a set of credentials like the Access Key ID and Secret Access Key. Here's an example of how to set it up:
- Default Profile: The
defaultprofile is the one Boto3 uses if no other profile is specified. - Named Profiles: Additional profiles that can be used by specifying their name in your code or environment.
Specifying a Profile in Boto3
When using Boto3, you can specify which profile to use with three main methods:
- Environment Variables: You can set the
AWS_PROFILEenvironment variable to select the desired profile:
After setting this variable, any Boto3 code you run will use the credentials specified in the development profile by default, unless you explicitly specify another profile.
- Session Object: Within your Python code, you can select a profile by creating a Boto3 session object with the desired profile:
In this example, the session object is explicitly using the production profile, ensuring the correct credentials are utilized.
- Boto3 Configuration: You can also use a
configobject in your code, which allows more granular configuration:
Here, in addition to specifying the profile, the configuration includes other settings like region_name.
Example: Connecting to CloudFront
To demonstrate the use of AWS profiles with Boto3, let's walk through an example script that connects to AWS CloudFront and lists your CloudFront distributions:
Simply substitute 'development' in Session(profile_name='development') with any other profile name as needed.
Considerations
Here are some considerations when choosing an AWS profile in Boto3:
- Environment-Specific Configurations: Use profiles to separate configurations for different environments (e.g., development, production).
- Credential Rotation: Regularly update and remove outdated credentials across all your profiles.
- A Shared Config File: Optionally, use a shared config file at
~/.aws/configfor non-credential config values, such as default region.
Summary Table
| Method | Description | Usage Scenario |
| Environment Variables | Set global variables like AWS_PROFILE to the desired profile.
Commonly used in single-profile setup or scripts. | Testing scripts with different profiles quickly. |
| Session Object | Create a session object with a specific profile in your code. | When building applications with distinct AWS account needs. |
| Boto3 Configuration | Include additional configuration like region and version along with the profile. | Fine-grained control over AWS service interactions. |
In conclusion, managing AWS profiles within Boto3 is straightforward and provides much-needed flexibility when dealing with multiple AWS accounts. It enhances your ability to securely and efficiently manage various environments and services, such as CloudFront.

