203

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

Killesk
  • 2,734
  • 3
  • 22
  • 29
  • 1
    Possible duplicate of [Uploading SSL certificates TO IAM](http://stackoverflow.com/questions/29256178/uploading-ssl-certificates-to-iam) – Anthony Neace Jan 03 '16 at 23:31
  • 3
    Can you do other `aws iam` actions successfully? It sounds like you are using invalid credentials to access the service. – Michael - sqlbot Jan 03 '16 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 04 '16 at 03: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 – Vineeth Guna Jan 04 '16 at 04: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 04 '16 at 19:18
  • Hi Vineeth, My ec2 has 1 role ' aws-elasticbeanstalk-ec2-role' and I've assigned that role the following permissions -AMFullAccess - IAMReadOnlyAccess- IAMUserSSHKeys. No success I'm afraid – Killesk Jan 04 '16 at 19:21
  • http://stackoverflow.com/questions/29256178/uploading-ssl-certificates-to-iam – Killesk Jan 04 '16 at 19:48
  • Possible duplicate of [AWS : The config profile (MyName) could not be found](https://stackoverflow.com/questions/34134879/aws-the-config-profile-myname-could-not-be-found) – hestellezg May 07 '19 at 18:41
  • The fix for me was to just make sure I was setting the AWS_DEFAULT_REGION (in addition to AWS_ACCESS_KEY, and AWS_SECRET_KEY), which it doesn't look like you were doing – ajpieri Mar 22 '21 at 21:44
  • For those of you using nextflow, don't forget to change out your `profiles.awsbatch.accessKey` and `profiles.awsbatch.secretKey` if you created new key(s) in exchange for old one(s). – Brian Wiley Mar 17 '23 at 21:24

34 Answers34

95

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.

Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
Ryan
  • 1,142
  • 8
  • 6
  • To add to the above, the token expires. That's why re-creating it worked :) – Bruno Rodrigues Jun 22 '22 at 04: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! – Nicholas Reis Oct 27 '22 at 04: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 01 '23 at 14:38
86

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/

Prasanth Louis
  • 4,658
  • 2
  • 34
  • 47
  • 23
    What would you recommend if running `get-session-token` results in the same error (InvalidClientTokenId)? – png Jan 31 '20 at 00: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 '21 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 04 '22 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 '23 at 03: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 '23 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)

Prakash
  • 671
  • 5
  • 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. – Naveen Reddy Marthala Oct 31 '20 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`. – pacoverflow Feb 16 '22 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. – big_in_japan Feb 16 '23 at 12:16
46

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>>"
DuckMaestro
  • 15,232
  • 11
  • 67
  • 85
  • 5
    how do i get that token? – Nachokhan Feb 02 '22 at 16:33
  • 9
    @DuckMaestro just mentioning "<>" does not make any sense, you have to explain the process to get the session token as asked by Nachokhan – GKP Feb 14 '22 at 00: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 '22 at 00:05
  • more documentation here : https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html – GKP Feb 14 '22 at 00:11
  • Thanks , it helped. I had to manually specify the session token – Neethu Lalitha May 09 '22 at 19:09
  • This is the real answer. If you look at your C:/users/user/.aws/credentials file, you'll notice that after running this command, `aws_session_token` will have been added to the `default` profile. The `default` profile is what is configured when running `aws configure` – Prithvi Boinpally Jun 24 '23 at 03:14
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

HKTonyLee
  • 3,111
  • 23
  • 34
ewalel
  • 1,932
  • 20
  • 25
23

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
Pat Myron
  • 4,437
  • 2
  • 20
  • 39
  • 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. – Thiago Prochnow Jul 21 '21 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 04 '22 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 – Mitrakov Artem Oct 27 '22 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

Kunal Patil
  • 745
  • 1
  • 9
  • 18
  • 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 '20 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

code_bash
  • 701
  • 1
  • 5
  • 16
8

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=...
Federico
  • 1,636
  • 2
  • 22
  • 24
  • 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 06 '21 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()
Eric
  • 1,171
  • 2
  • 14
  • 27
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
    
user1020840
  • 51
  • 1
  • 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 '21 at 08:51
4

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 = ...
supernova
  • 1,762
  • 1
  • 14
  • 31
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

Abdeali Chandanwala
  • 8,449
  • 6
  • 31
  • 45
  • 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 02 '21 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.

shonky linux user
  • 6,131
  • 4
  • 46
  • 73
  • 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 '18 at 08:57
  • On Mac OSX using `awscli` from Homebrew it doesn't exist either. – schmijos Jan 26 '21 at 11:17
3

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

Dorian
  • 7,749
  • 4
  • 38
  • 57
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
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 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 '19 at 01: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 '21 at 13:38
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.

dillon.harless
  • 427
  • 4
  • 16
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.

zabaat
  • 103
  • 1
  • 6
1

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.

laurent
  • 88,262
  • 77
  • 290
  • 428
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

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.

Taran
  • 12,822
  • 3
  • 43
  • 47
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.

diginoise
  • 7,352
  • 2
  • 31
  • 39
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74
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!!.

the_dq
  • 1
  • 1
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.

CVname
  • 347
  • 3
  • 12
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.

Basil
  • 74
  • 5
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>
Francis Ade
  • 328
  • 3
  • 8
0

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.

Kilise
  • 1,051
  • 4
  • 15
  • 35
0

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

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 08 '22 at 20:35
0

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

0

In my case, I was getting this error trying to send emails with AWS SES using the SDK. I had to restart the application (.NET 6 in Docker with docker-compose) for the credentials to be reloaded by the application, so they must be cached once loaded.

Jeppe
  • 1,830
  • 3
  • 24
  • 33
0

Localstack specific.

I am using localstack and getting this error.

I had to do:

  const myService = AWS.[myService]({endpoint: [localstack_url]});
  myService.useAsUsual...
terary
  • 940
  • 13
  • 30
0

Delete cache files from folder and try to reconnect from visual studio

c:\users\.aws\sso\cache

enter image description here

rajquest
  • 535
  • 1
  • 5
  • 10