Open In App

How to Choose an AWS Profile When using Boto3 to Connect to CloudFront

Cloudfront is an AWS Service that is used to reduce latency for delivering both static and dynamic content, When a user requests content that you're serving with CloudFront, the request is routed to the edge location that provides the lowest latency (time delay), so that content is delivered with the best possible performance.

If the content is already in the edge location with the lowest latency, CloudFront delivers it immediately. If the content is not in that edge location, CloudFront retrieves it from an origin that you've defined—such as an Amazon S3 bucket, a Media Package channel, or an HTTP server (for example, a web server)—that you have identified as the source for the definitive version of your content.

To work with Cloudfront using the SDKs, we can use the Boto3 package in Python, If you've got multiple AWS profiles, you might struggle to use a specific profile while connecting, In this article, we'll demonstrate how to choose a specific AWS profile while connecting with Cloudfront.

Configure AWS Credentials

To configure AWS profiles, we can use AWS cli, Follow the below steps to set up AWS profiles, and repeat the steps if you wanna add more profiles.

Step 1: Login to AWS Console

Step 2: Click on your username at the top right corner and click on Security Credentials

Step 3: Under Access Keys, click on Create Access Key. Access Choose Command Line Interface (CLI). Add some description for it then Click on Create

Step 4: Either copy the access key ID and Secret access key displayed on the screen or download the CSV file.

aws configure --profile <profile-name>

For example:

aws configure --profile dillip-tech
aws_configure_with_profiile-(1)

Configuring AWS Profile with custom name

Connect To Cloudfront Using Specific AWS Profile

While connecting to cloudfront using boto3, there are various methods to use specific AWS profile:

  1. Specifying profile in Session
  2. Setting default profile in Session
  3. Setting environment variable
  4. Using the default profile

Let's get into the each method in-detail with practical example.

Method 1: Specifying profile in Session

We can specify the aws profile using the profile_name argument in Session class as mentioned in the below snippet, this will allow us to connect to cloudfront using that specific profile.

import boto3

session = boto3.Session(profile_name='dillip-tech')

cloudfront_client = session.client('cloudfront')

# Do something with cloudfront_client here

Method 2: Setting Default Profile In Session

We can set the specific aws profile as default for AWS sessions using the setup_default_session method in boto3 and sessions or clients initiated after setting default profile will utilize the mentioned aws profile, you can observe the same the sample in the below snippet, this will allow us to connect to cloudfront using that specific profile.

import boto3

boto3.setup_default_session(profile_name='dev')

session = boto3.Session()

cloudfront_client = session.client("cloudfront")

# Do something with cloudfront here

Method 3: Setting Environment Variable

This will work Just like the above setup_default_session, but we need not mention it using any method instead we can set the default profile to use using the environment variable i.e AWS_PROFILE

export AWS_PROFILE=my-profile

And now the session or client initialized will use the my-profile as the aws profile for connecting, we need not mention aws profile anywhere.

import boto3

cloudfront_client = boto3.client("cloudfront")

# Do something with cloudfront_client

Method 4: Using The Default Profile

By default boto3 will try to initialize the client or session using the profile with name default, you can set the default profile by not specifying any profile while configuring or providing name as default

aws configure
aws-default-profile-configure-(1)

Configure Default AWS profile

Now you can initialize clients, which will use default profile if you didn't specify AWS profile explicitly.

import boto3

cloudfront_client = boto3.client("cloudfront")

# Do something with cloudfront_client

We've leant how to connect with cloudfront using specific profile with various possible methods, now you can leverage the cloudfront functionalities using the boto3, and deploy your files with ease, and you can work with different environments or projects using this profile specific connections.

AWS profile when using boto3 to connect to CloudFront - FAQ's

What happens if I don't use a specific profile and have multiple profiles configured?

Boto3 might not use the intended credentials. It might use the default profile ("default") as mentioned in the article, and if you don't have a profile with default as profile name you'll get an error saying no profile with default and you've to specify profile name in that case.

What is the benefit of using a specific profile when connecting to CloudFront?

If you have multiple AWS accounts or projects, using specific profiles helps you isolate credentials and manage permissions for each environment. This ensures you're using the correct access keys for the intended CloudFront resources.

Is there a way to see which profile is currently being used by boto3?

There isn't a direct method within boto3 to retrieve the active profile. However, you can check the environment variable AWS_PROFILE (this will work if you've configured) or use the botocore.credentials module to inspect the credential provider chain and identify the profile used.

Article Tags :