How to resolve Boto3 botocore.exceptions.NoCredentialsError: Unable to locate credentials error? The botocore.exceptions.NoCredentialsError in the context of AWS SDK for Python (Boto3) indicates that the SDK is unable to locate valid AWS credentials. To resolve this issue, you can consider the following approaches:

Advertisements
botocore.exceptions.NoCredentialsError: Unable to locate credentials

Get AWS Access key and Secret Key

Below, I have explained many options to resolve this error, but first, you need to get the AWS Access Key and AWS Secret key from the AWS Management Console.

  1. Login to AWS Console: Log in to the AWS Management Console using your AWS account credentials.
  2. Navigate to IAM: Go to the IAM (Identity and Access Management) service.
  3. Access Users: In the left navigation pane, click on “Users.”
  4. Select User: Click on the user for whom you want to generate or retrieve the access key.
  5. Security Credentials Tab: Go to the “Security credentials” tab.
  6. Access Keys Section: Under the “Access keys” section, you can either create a new access key pair or view an existing one.

Keep these credentials secure and avoid sharing them unnecessarily. If you need to rotate or manage your access keys, you can do so in the IAM console.

Configure AWS Credentials Locally

Ensure you have AWS credentials configured on your local machine to resolve botocore.exceptions.NoCredentialsError: Unable to locate credentials. You can do this by using the aws configure command in the command line. aws configure is a command-line interface (CLI) command provided by the AWS Command Line Interface (AWS CLI). It is used to interactively set up AWS CLI configuration, including AWS access key ID, AWS secret access key, default region, and default output format.

To use this option, you must install AWS CLI on your system.


# aws configure
(.aws-examples-env) (genai) admin@naveens-MacBook-Pro s3_basics % aws configure
AWS Access Key ID [None]: <enter aws access key>
AWS Secret Access Key [None]: <enter aws secret key>
Default region name [None]: <enter default region>
Default output format [None]: <enter output format>

Once configured, these settings are stored in a local configuration file (~/.aws/config and ~/.aws/credentials), and the AWS CLI uses them for subsequent interactions with AWS services.

botocore.exceptions.NoCredentialsError: Unable to locate credentials

If you don’t have CLI installed, manually create ~/.aws/credentials file with the below content.


[default]
aws_access_key_id=<enter aws access key>
aws_secret_access_key=<enter aws secret key>

And manually create ~/.aws/config file with the below content.


[default]
region = <enter aws region>

Use Environment Variables to Resolve AWS NoCredentialsError

Alternatively, you can set the AWS credentials as environment variables to resolve botocore.exceptions.NoCredentialsError: Unable to locate credentials error. Ensure that the following environment variables are set:


# Set environment variables
EXPORT AWS_ACCESS_KEY_ID=<aws_access_key>
EXPORT AWS_SECRET_ACCESS_KEY=<aws_secret_access_key>
EXPORT AWS_DEFAULT_REGION=<aws_region>

Set these variables in your shell or in the script before making any AWS SDK calls. If you use Unix, the ideal place to set these values is in the ~/.profile file.

Using Environment Variables In Python

You can use environment variables in Python to set AWS credentials, and the boto3.Session class will automatically use them.


# Import os
import os

# Set environment variables
os.environ['AWS_ACCESS_KEY_ID'] = 'your_access_key'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'your_secret_key'

# Create session
session = boto3.Session()

Use Boto3 Session to Resolve AWS NoCredentialsError

To resolve botocore.exceptions.NoCredentialsError: Unable to locate credentials error, you can also create AWS Python SDK Boto3 session with credentials instead of relying on the default session. In Boto3, a session is an object that stores configuration state, including AWS access key ID, secret access key, and session token. The boto3.Session class is used to create a session, and it provides a way to customize and manage the configuration settings for AWS service clients.


# Import boto3
import boto3

# Create boto3 Session
session = boto3.Session(
    aws_access_key_id='your_access_key',
    aws_secret_access_key='your_secret_key',
    region_name='your_region'
)

IAM Roles and Instance Profiles

If you are running your code on an AWS resource like an EC2 instance, consider using IAM roles and instance profiles. Ensure that the instance has the necessary IAM role attached, and your code will automatically use the credentials associated with that role.

Check for Credential Provider Chain

Boto3 uses a credential provider chain to look for credentials in various sources. Ensure that the AWS SDK is able to find valid credentials through the available providers. The chain typically includes environment variables, AWS CLI configuration, instance profile, etc.

Update Boto3 and Botocore

Ensure that you are using the latest versions of Boto3 and Botocore. AWS frequently releases updates that may include bug fixes and improvements.bashCopy code


# Upgrade boto3
pip install --upgrade boto3 botocore

Use AWS CLI Commands Directly

If you are still facing issues, you can use AWS CLI commands directly in your script using subprocess to authenticate before running your Boto3 code.


# Use CLI Commands in Python Code
import subprocess

subprocess.run(['aws', 'configure', 'set', 'aws_access_key_id', 'your_access_key'])
subprocess.run(['aws', 'configure', 'set', 'aws_secret_access_key', 'your_secret_key'])
subprocess.run(['aws', 'configure', 'set', 'region', 'your_region'])

Debugging

Use the boto3.set_stream_logger() method to enable debug logs and get more information about what’s happening under the hood.


# Enable Debug to troubleshoot the error
import boto3 
import logging 
boto3.set_stream_logger(level=logging.DEBUG)

Conclusion

By applying one or a combination of these solutions, you should be able to resolve the Boto3 botocore.exceptions.NoCredentialsError: Unable to locate credentials and successfully authenticate with AWS services using Boto3.

Happy Learning !!

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium