224

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

10
  • 1
    Possible duplicate of Uploading SSL certificates TO IAM Jan 3, 2016 at 23:31
  • 3
    Can you do other aws iam actions successfully? It sounds like you are using invalid credentials to access the service. Jan 3, 2016 at 23:34
  • 2
    I do not think your error has anything to do with certs. Your credentials are invalid or do not have privileges.Can you run aws iam list-users and see if you get an output or same error?
    – helloV
    Jan 4, 2016 at 3:06
  • 1
    Are you running this command from ec2 instance which has roles? If yes make sure the role policy has IAM permissions to upload the certificate. From the error the token is invalid, the token is generated when you are using IAM roles or using AWS STS service Jan 4, 2016 at 4:46
  • Hi HelloV, nope, when I run your command I get 'A client error (InvalidClientTokenId) occurred when calling the ListUsers operat ion: The security token included in the request is invalid.'
    – Killesk
    Jan 4, 2016 at 19:18

37 Answers 37

130

I had the same error, even after re-running aws configure, and inputting a new AWS_ACCESS_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.

3
  • To add to the above, the token expires. That's why re-creating it worked :) Jun 22, 2022 at 4:36
  • Removing the aws_session_token did the trick, thanks! I'm not sure why the token stay there if it's invalid. Anyways, thank you so much! Oct 27, 2022 at 4:54
  • Kept getting this error, but turns out I had an outdated (and deleted) key ID and access key set as environment variables, taking precedence over the ~/.aws/credentials creds
    – Emile Pels
    Apr 1, 2023 at 14:38
99

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/

6
  • 23
    What would you recommend if running get-session-token results in the same error (InvalidClientTokenId)?
    – png
    Jan 31, 2020 at 0:47
  • @PrasanthLouis and @png, Do I have to update my \.aws\credential with the generated keys? According to the web link, it looks like updating by command line input...
    – Cloud Cho
    Jul 21, 2021 at 16:48
  • 8
    get-session-token was failing for me because I still had the environment variables AWS_SESSION_TOKEN and AWS_SECURITY_TOKEN set. These should be unset first or AWS will try to use them implicitly and fail because they're invalid.
    – theberzi
    Jan 4, 2022 at 14:27
  • had to do the set AWS_ACCESS_KEY_ID= set AWS_SECRET_ACCESS_KEY= set AWS_SESSION_TOKEN= then aws configure again. now it is working again. Thank you
    – yasbars
    Feb 14, 2023 at 3:50
  • Just had a colleague lose 4 hours on this by accidentally creating the user in "IAM Identity Center" instead of "IAM Management Console" (which, aside from having similar names, look very similar as well). Make sure you use the latter for CLI access!
    – Ruslan
    Jun 15, 2023 at 22:07
67

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)

3
  • 1
    I am using circleCI to run task in AWS ECS. I got this error and this seems to work. but, why aren't access key id and secret access key of a user with programatic access and just enough permissions enough to deploy to AWS ECS. Oct 31, 2020 at 11:27
  • 3
    I do not see My security credentials when clicking on my username on the top right corner. All I see are Account, Organization, Service Quotas, Billing Dashboard, Switch Role, Signout. Feb 16, 2022 at 16:18
  • 1
    Upon checking my access key (through security credentials as per this answer) I saw it was somehow deactivated. After activating it back the error was resolved for me. Feb 16, 2023 at 12:16
57

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>>"
6
  • 8
    how do i get that token?
    – Nachokhan
    Feb 2, 2022 at 16:33
  • 13
    @DuckMaestro just mentioning "<<your session token>>" does not make any sense, you have to explain the process to get the session token as asked by Nachokhan
    – GKP
    Feb 14, 2022 at 0:04
  • 1
    @Nachokhan you can go to your .aws directory (in mac it's ~/.aws) and do a ls -ltrh , you can see a file called "credentials" in that file you will get the aws_session_token. Example aws_access_key_id = XXXXXXXXXXXXX aws_secret_access_key = XXXXXXXXXXXXX aws_session_token = XXXXXXXXXXXXX aws_security_token = XXXXXXXXXXXXX x_principal_arn = arn:aws:sts::XXXXXXXXXXXXX:assumed-role/XXXXXXXXXXXXX/gourabp@XXXXXXXXXXXXX
    – GKP
    Feb 14, 2022 at 0:05
  • more documentation here : docs.aws.amazon.com/sdkref/latest/guide/file-location.html
    – GKP
    Feb 14, 2022 at 0:11
  • Thanks , it helped. I had to manually specify the session token May 9, 2022 at 19:09
29

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

1
  • Thank you removing the environment variables worked for me.
    – Amr Ahmed
    Feb 21, 2023 at 20:44
25

If switching from temporary IAM role credentials to IAM user credentials, ensure AWS_SESSION_TOKEN, which is only used for temporary credentials, is no longer set:

unset AWS_SESSION_TOKEN # unset environment variable
3
  • 1
    That did it for me. But also I had to manually update my ~/.aws/credentials file. For some reason it wasent updating with new credentials values. Jul 21, 2021 at 21:54
  • That line of thinking did it for me too - the token that had somehow snuck into ~/.aws/credentials wasn't needed. Only after removing the file, logging in again worked. Presumably just clearing the token from the file would've done it too.
    – insideClaw
    Jan 4, 2022 at 14:49
  • Works for me. I had export AWS_ACCESS_KEY_ID=test and export AWS_SECRET_ACCESS_KEY=test in my zshrc file, which caused an issue Oct 27, 2022 at 10:57
18

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

1
  • 1
    I have multiple profile in ~/.aws/config file. The error go away when I run aws iam list-server-certificates --profile another-profile
    – Wild Teddy
    Dec 10, 2020 at 11:02
13

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

9

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=...
1
  • To return only environment variables that start with "AWS", you can use the caret symbol, e.g. env | grep ^AWS. Most (if not all) "AWS" environment variables start with AWS, so this can help filter out other rubbish that clutters your console.
    – devklick
    Sep 6, 2021 at 20:54
6

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()
0
5
  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
    
1
  • After enabling MFA, creating new access key and using it with aws configure was the solution. Then I could get "aws sts get-caller-identity" containing MFA arn and "aws sts get-session-token --serial-number XXX --token-code YYY" was successful
    – mpiliszcz
    Feb 18, 2021 at 8:51
5

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

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.

3

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

1
  • I was using expired credentials. When I looked in the credentials of the IAM console, I could not find any active Access Key ID and Secret Access Key, as they had expired. So I generated new credentials and saved them in my .aws/credentials file and the error is gone.
    – harshainfo
    Jun 2, 2021 at 15:00
3

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.

2
  • 3
    That folder does not exist for me, running Ubuntu 18.04 and aws-cli/1.15.11 Python/3.5.2 Linux/4.15.0-20-generic botocore/1.10.11
    – Purefan
    May 21, 2018 at 8:57
  • On Mac OSX using awscli from Homebrew it doesn't exist either.
    – schmijos
    Jan 26, 2021 at 11:17
3

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

2

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
2
  • 1
    Adding the --no-sign-request option just returned a "missing Authentication Token" message. So it appears, at least in my case, that the token is needed and just turning it off won't do.
    – havoc1
    Jan 29, 2019 at 1:48
  • thanks ! you are the only one mentioning this --endpoint flag, which helped a lot with my local configuration with a docker container :P cheers !!
    – R13mus
    Mar 15, 2021 at 13:38
2

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.

2

After updating the AWS_ACCESS_KEY & AWS_SECRET_ACCESS_KEY environment variables do not forget to restart the session in your terminal in order for it to use the updated values.

1

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.

1

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.

1

opened my ~/.aws/credentials file and saw that the secret key was interchanged with my Access ID strangely, switching it solved the problem

1
  • This was not my problem, but it led me to the right answer. My access key is inactive. Thanks! Oct 13, 2023 at 19:07
1

A little late to the game here, but this may be helpful for someone. Using Windows. I had switched from one account to another. Before working in the first account, I ran

SET AWS_ACCESS_KEY_ID=ABCDE....
SET AWS_SECRET_ACCESS_KEY=12345...
SET AWS_SESSION_TOKEN=a1b2c3...

When I switched to the second account, I ran the following. This second account did not require a session token:

SET AWS_ACCESS_KEY_ID=FGHIJ....
SET AWS_SECRET_ACCESS_KEY=67890...

When I then tried to connect I recived the error:

An error occurred (InvalidClientTokenId) when calling the GetCallerIdentity operation: The security token included in the request is invalid

I cleared the session toke variable by running set AWS_SESSION_TOKEN= and then I was able to authenticate. Note that there is no space before the equals sign.

1
  • unset AWS_SESSION_TOKEN should do it too
    – Noam Manos
    Jun 8, 2023 at 17:42
1

In my case I had triple checked the .aws/credentials file, environment variables, command line arguments, project config etc. but some old credentials were still being found somewhere.

In the end it was an old auth token header configured in Postman which I was using to call the API.

0

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.

0

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.

0

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

0

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.

0

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

So, kindly confirm your account is not suspended.

0

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>

Not the answer you're looking for? Browse other questions tagged or ask your own question.