Home > Mobile >  The security token included in the request is invalid. When no profile specified
The security token included in the request is invalid. When no profile specified

Time:06-11

When running this:

session = boto3.session.Session()
client = session.client(service_name="secretsmanager", region_name='region')
secret_response = client.get_secret_value(SecretId='secret_name')
print(secret_response)

This is the error that I get: botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the GetSecretValue operation: The security token included in the request is invalid.

But when I run the exact same code but specify my profile like this:

session = boto3.session.Session(profile_name='default')

it works.

boto3 version: 1.27.6

CodePudding user response:

You probably have mulitple profiles in aws credentials. Whats baffling is that even if you use boto3.session.Session() without specifying a profile, it's supposed to use the default profile.

However, try to remove aws credentials first by running rm ~/.aws/credentials. After that, run aws configure again and follow the prompt to add your credentials properly. This should solve the problem.

CodePudding user response:

I'd actually just make sure you aren't setting your AWS_PROFILE env variable any where else, this is a common issue people have. I use this bash function whenever I need to clear all AWS related environment variables.

awskill () {
    unset AWS_DEFAULT_PROFILE
    unset AWS_ACCESS_KEY_ID
    unset AWS_SECRET_ACCESS_KEY
    unset AWS_SECRET_KEY
    unset AWS_SECURITY_TOKEN
    unset AWS_SESSION_TOKEN
}
  • Related