How can I resolve the error "The security token included in the request is invalid" when running aws
I cd into the directory where all the pem/key files are and run the following:
aws iam upload-server-certificate --server-certificate-name certificate_name --certificate-body file://webservercertificate.pem --private-key file://server.key --certificate-chain file://certificate_chain_file.pem I get the following error:
A client error (InvalidClientTokenId) occurred when calling the UploadServerCertificate operation: The security token included in the request is invalid.
I have 1 'user' in 'users'. That user has been assigned the following permissions:
IAMFullAccess IAMReadOnlyAccess IAMUserSSHKeys I've downloaded the credentials for this user and put them into my user variables
AWS_ACCESS_KEY **** AWS_SECRET_KEY **** I have 1 role on my elastic beanstalk aws-elasticbeanstalk-ec2-role
31 Answers
1 2NextIf you're using the CLI with MFA, you have to set the session token in addition to setting the access and secret keys. Please refer to this article:
5Try to go to the security credentials on your account page: Click on your name in the top right corner -> My security credentials
Then generate access keys over there and use those access keys in your credentials file (aws configure)
2I had the same error, even after re-running aws configure, and inputting a new AWS_ACESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
What fixed it for me was to delete my ~/.aws/credentials file and re-run aws configure.
It seems that my ~/.aws/credentials file had an additional value: aws_session_token which was causing the error. After deleting and re-creating the ~/.aws/configure using the command aws configure, there is now only values for aws_access_key_id and aws_secret_access_key.
In my case, there were two different 'AWS_SECRET_ACCESS_KEY' and 'AWS_ACCESS_KEY_ID' values set one through the Windows environment variable and one through the command line.
So, update these two and the default_region using a command line
> aws configure Press enter and follow the steps to fill the correct AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION
> aws sts get-caller-identity should return the new set credentials
If you have been given a Session Token also, then you need to manually set it after configure:
aws configure set aws_session_token "<<your session token>>" 5If switching from using temporary IAM role credentials to using IAM user credentials, don't forget to ensure AWS_SESSION_TOKEN, which is only used for temporary credentials, no longer has a value:
unset AWS_SESSION_TOKEN # unset the environment variable 2Try to export the correct profile i.e. $ export AWS_PROFILE="default" If you only have a default profile make sure the keys are correct and rerun aws configure
I had to specify the AWS profile to use --profile default explicitly to get rid of this error while running AWS CLI commands. I could not understand though that why it did not pick up this profile automatically as there was only [dafault] profile present in my aws config and credentials file.
I hope this helps.
Cheers, Kunal
1This happened to me when using java sdk. The problem was for me was i wasnt using the session token from assumed role.
Working code example ( in kotlin )
val identityUserPoolProviderClient = AWSCognitoIdentityProviderClientBuilder .standard() .withCredentials(AWSStaticCredentialsProvider(BasicSessionCredentials("accessKeyId", ""secretAccessKey, "sessionToken"))) .build() 0$ aws configure and use the new key and secret.Run the command again:
serverless invoke local --function create --path mocks/create-event.json Similar to Pat's response, check your environment variables. Particularly AWS_SESSION_TOKEN AND AWS_SECURITY_TOKEN
Try unsetting them: unset VAR_NAME
To see what variables are set try env | grep AWS and expect something like:
AWS_REGION=ap-southeast-2 AWS_PAGER= AWS_SECRET_ACCESS_KEY=... AWS_ACCESS_KEY_ID=... AWS_SESSION_TOKEN=... AWS_SECURITY_TOKEN=... 1This can also happen when you disabled MFA. There will be an old long term entry in the AWS credentials.
Edit the file manually with editor of choice, here using vi (please backup before):
vi ~/.aws/credentials Then remove the [default-long-term] section. As result in a minimal setup there should be one section [default] left with the actual credentials.
[default-long-term] aws_access_key_id = ... aws_secret_access_key = ... aws_mfa_device = ... You are somehow using wrong AWS Credentials (AccessKey and SecretKey) of AWS Account. So make sure they are correct else you need to create new and use them - in that case may be @Prakash answer is good for you
1I had the same error but was caused by a different issue.
The credentials were changed on AWS but I was still using a cached MFA session token for the config profile.
There is a cache file for each profile under ~/.aws/cli/cache/ containing the session token.
Remove the cache file, reissue the command and enter a new MFA token and its good to go.
2thank you DuckMaestro, I solved my problems with your suggestion.
I configured like this.
# aws configure but, I met error this.
# aws iam list-users An error occurred (InvalidClientTokenId) when calling the ListUsers operation: The security token included in the request is invalid. # aws sts get-caller-identity An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid. and I did like this with your suggestion.
# aws configure set aws_session_token "<<your session token>>" I solved it !! thanks.
I thought you could avoid it by just passing the --no-sign-request param, like so:
aws --region us-west-2 --no-sign-request --endpoint-url= \ s3 mb s3://mytestbucket 2I had the access key and secret key mixed up :)
In my situation, the problem was due to running powershell as an admin, so it was looking for the aws credentials in the root of my admin user. There's probably a better way to resolve this, but what worked quickly for me was recreating my .aws folder in the root of my admin user.
In my case we use both AWS CN and COM, even though I have valid keys and config/credential files and even specify the exports and --profile in the command I get this error.
To fix: ERROR: NotAuthorizedError - Operation Denied. The security token included in the request is invalid.
I add --region to the command as well.
I was able to use AWS cli fully authenticated, so for me the issue was within terraform for sure. I tried all the steps above with no success. A reboot fixed it for me, there must be some a cache somewhere in terraform that was causing this issue.
This is weird, but in my case whenever I wanted to retype the access id and the key by typing aws configure.
Adding the id access end up always with a mess in the access id entry in the file located ~/.aws/credentials(see the picture)
I have removed this mess and left only the access id. And the error resolved.
I had similar issue when I was deploying my django application over elastic Beanstalk and what I found is when I was trying various methods somehow one eb-cli profile got created in config file in ~/.aws/ folder so once I got rid of that everything worked fine!!.
I had a similar issue for uploading a certificate using the cli. I needed to use a programmatic access from a newly created iam user (with its own keys). The MFA that I used to authenticate myself to the AWS console (web) in my AWS account was interfering when using the aws configure command with the new iam user credentials for programmatic access. In the new credentials file (created from the aws configure command) the session token from the MFA log was somehow persisted. Deleting manually from the credentials file the session token helped in my case.
After so much research I found out that my AWS account was suspended due to payment.
So, kindly confirm your account is not suspended.
For anyone who is getting this error when using AWS SDK on the Cloud9 editor, the problem could be due to the AWS-managed temporary credentials which might restrict what the identity (the AWS-managed temporary credentials which AWS sets for you) can do.
If you run cat ~/.aws/credentials you will see a profile already created for you, and this also lists the session token. Note: this is managed by AWS and cannot be modified/deleted.
A get-around is turning off the AWS-managed temporary credentials. Here is some info on how these temporary credentials are managed and how you can turn them off: . Once you discontinue using the AWS-managed credentials, and re-run the above command (cat ~/.aws/credentials), you will notice that the file is empty. Now, you must set a profile manually and use this profile for AWS SDK/CLI.
To configure a profile, you'll need to run: aws configure <name-of-the-profile>. If you do not pass a name for the profile, it will default to default (and subsequently override this default profile). With the profile set, use this profile with AWS SDK/CLI/API.
If that can help anyone, I had the same problem and eventually I found that it's because my AWS profile region was eu-south-1. By setting it to us-west-2 it worked.
Doesn't make any sense to me, but it seems it's something to check if you're having this problem.
Had similar issue where i had to re-configure my aws
what fixed this for me was resetting environment variables
export AWS_ACCESS_KEY=<key> export AWS_SECRET_ACCESS_KEY=<key> In my case I automatically created Api keys, then used them directly using Assume role. They didn't work when using sts assume-role.
I did a sleep for around 10 seconds after the api keys was created. That solved the problem for me.
In my situation, this error occurs due to wrong AWS credentials. Try to verify before retrying.
1opened my ~/.aws/credentials file and saw that the secret key was interchanged with my Access ID strangely, switching it solved the problem
1 2NextncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoam1taGeAcoSOoaawZZOWu261jKucrKecq7JuwMeeZJ6qoqS%2FbsDHnmSsnZOqv6rA2GarqKOVo3qqusKlrJ2dlGK2r3nToZxmqpWmwqa%2F02agrGWZo8OiuA%3D%3D