How can I resolve the error "The security token included in the request is invalid" when running aws iam upload-server-certificate?

Amazon Web-ServicesAmazon IamAws Cli

Amazon Web-Services Problem Overview


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

Amazon Web-Services Solutions


Solution 1 - Amazon Web-Services

If 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: https://aws.amazon.com/premiumsupport/knowledge-center/authenticate-mfa-cli/

Solution 2 - Amazon Web-Services

Try 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)

Solution 3 - Amazon Web-Services

I 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.

Solution 4 - Amazon Web-Services

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_ID AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION

> aws sts get-caller-identity

should return the new set credentials

Solution 5 - Amazon Web-Services

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>>"

Solution 6 - Amazon Web-Services

If 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

Solution 7 - Amazon Web-Services

Try 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

Solution 8 - Amazon Web-Services

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

Solution 9 - Amazon Web-Services

This 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()

Solution 10 - Amazon Web-Services

  1. Click on your username in the top nav, My Security Credentials

  2. Click on Access Key Tab, Create New, copy the key and secret.

  3. From the terminal run $ aws configure and use the new key and secret.

  4. Run the command again:

     serverless invoke local --function create --path mocks/create-event.json
    

Solution 11 - Amazon Web-Services

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=...

Solution 12 - Amazon Web-Services

This 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 = ...

Solution 13 - Amazon Web-Services

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

Solution 14 - Amazon Web-Services

I 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.

Solution 15 - Amazon Web-Services

thank 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.

Solution 16 - Amazon Web-Services

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=http://192.168.99.100:4572 \
 s3 mb s3://mytestbucket

Solution 17 - Amazon Web-Services

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.

Solution 18 - Amazon Web-Services

I had the access key and secret key mixed up :)

Solution 19 - Amazon Web-Services

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.

Solution 20 - Amazon Web-Services

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.

Solution 21 - Amazon Web-Services

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) The messed access id

I have removed this mess and left only the access id. And the error resolved.

Solution 22 - Amazon Web-Services

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!!.

Solution 23 - Amazon Web-Services

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.

Solution 24 - Amazon Web-Services

After so much research I found out that my AWS account was suspended due to payment.

So, kindly confirm your account is not suspended.

Solution 25 - Amazon Web-Services

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: https://docs.aws.amazon.com/cloud9/latest/user-guide/security-iam.html#auth-and-access-control-temporary-managed-credentials. 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.

Solution 26 - Amazon Web-Services

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.

Solution 27 - Amazon Web-Services

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>

Solution 28 - Amazon Web-Services

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.

Solution 29 - Amazon Web-Services

In my situation, this error occurs due to wrong AWS credentials. Try to verify before retrying.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKilleskView Question on Stackoverflow
Solution 1 - Amazon Web-ServicesPrasanth LouisView Answer on Stackoverflow
Solution 2 - Amazon Web-ServicesPrakashView Answer on Stackoverflow
Solution 3 - Amazon Web-ServicesRyanView Answer on Stackoverflow
Solution 4 - Amazon Web-ServicesewalelView Answer on Stackoverflow
Solution 5 - Amazon Web-ServicesDuckMaestroView Answer on Stackoverflow
Solution 6 - Amazon Web-ServicesPat MyronView Answer on Stackoverflow
Solution 7 - Amazon Web-Servicescode_bashView Answer on Stackoverflow
Solution 8 - Amazon Web-ServicesKunal PatilView Answer on Stackoverflow
Solution 9 - Amazon Web-ServicesEricView Answer on Stackoverflow
Solution 10 - Amazon Web-Servicesuser1020840View Answer on Stackoverflow
Solution 11 - Amazon Web-ServicesFedericoView Answer on Stackoverflow
Solution 12 - Amazon Web-ServicessupernovaView Answer on Stackoverflow
Solution 13 - Amazon Web-ServicesAbdeali ChandanwalaView Answer on Stackoverflow
Solution 14 - Amazon Web-Servicesshonky linux userView Answer on Stackoverflow
Solution 15 - Amazon Web-ServicesSoohee MoonView Answer on Stackoverflow
Solution 16 - Amazon Web-ServicesdjangofanView Answer on Stackoverflow
Solution 17 - Amazon Web-Servicesdillon.harlessView Answer on Stackoverflow
Solution 18 - Amazon Web-ServicesDorianView Answer on Stackoverflow
Solution 19 - Amazon Web-ServiceszabaatView Answer on Stackoverflow
Solution 20 - Amazon Web-ServicesjoelradonView Answer on Stackoverflow
Solution 21 - Amazon Web-ServicesDINA TAKLITView Answer on Stackoverflow
Solution 22 - Amazon Web-Servicesthe_dqView Answer on Stackoverflow
Solution 23 - Amazon Web-ServicesCVnameView Answer on Stackoverflow
Solution 24 - Amazon Web-ServicesBasilView Answer on Stackoverflow
Solution 25 - Amazon Web-ServicesShruti ChaturvediView Answer on Stackoverflow
Solution 26 - Amazon Web-ServiceslaurentView Answer on Stackoverflow
Solution 27 - Amazon Web-ServicesFrancis AdeView Answer on Stackoverflow
Solution 28 - Amazon Web-ServicesKiliseView Answer on Stackoverflow
Solution 29 - Amazon Web-ServicesAkshat GoyalView Answer on Stackoverflow