pythonpython-3.xamazon-s3pipboto3

Boto3 getting Error "botocore.exceptions.NoCredentialsError: Unable to locate credentials"



import boto3
import os

os.environ['AWS_DEFAULT_REGION'] = "us-east-1"


def get_secret_value():
    
    """Gets the value of a secret.

    Version (if defined) is used to retrieve a particular version of
    the secret.

    """

    secrets_client = boto3.client("secretsmanager", region_name = "us-east-1")
    kwargs = {'SecretId': "DBName"}
   # if version is not None:
   #     kwargs['VersionStage'] = version
    response = secrets_client.get_secret_value(**kwargs)
    print(response)
    return response

get_secret_value()

This is the sample designed which helps in retrieving the secrets which provides me a fatal error of unable to locate credentials.


Solution

  • If you are running this code on an Amazon EC2 instance, then you should assign an IAM Role to the instance. Credentials will be automatically provided to your program via boto3.

    If you are running this code on your own computer, then you should run the AWS Command-Line Interface (CLI) aws configure command and provide your Access Key and Secret Key. It will store those credentials in the ~/.aws/credentials file and boto3 will retrieve them automatically.