Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cli get error "The security token included in the request is invalid"

I did aws configure and test it worked before. But it didn't work today, I got the the error when tried to get hosted zone

 $ aws route53 list-hosted-zones

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

I deleted ~/.aws and did aws configure again, but I still got the same error. Could you please help? Thanks!

like image 385
Charles PHAM Avatar asked Oct 22 '16 23:10

Charles PHAM


People also ask

Could not get token Expiredtoken the security token included in the request is expired?

You must refresh the credentials before they expire. Another reason for expiration is using the incorrect time. A consistent and accurate time reference is crucial for many server tasks and processes. If your instance's date and time aren't set correctly, the AWS credentials are rejected.

What is an invalid security token?

If you're trying to reset your password and you receive an error citing an “invalid token” or asking you for your token, it's likely that the link you clicked on to reset your password has expired. For security reasons, passwords are never sent out across the Internet.

How do I refresh AWS CLI credentials?

If your credentials and config files contain a single profile, you can just delete the files to clear your AWS CLI credentials. The next time you run the aws configure command, the AWS CLI will automatically re-create them for you.

How do I get my AWS session token on AWS console?

The GetSessionToken operation must be called by using the long-term AWS security credentials of the AWS account root user or an IAM user. Credentials that are created by IAM users are valid for the duration that you specify.


2 Answers

After you enabled MFA, you will have to pass temporary credentials you received from executing
aws sts get-session-token on each future request.

With environment variables:

export AWS_ACCESS_KEY_ID=XXX
export AWS_SECRET_ACCESS_KEY=YYY
export AWS_DEFAULT_REGION=us-east-2
export AWS_SESSION_TOKEN=ZZZ

With named profiles:

[mfa]
aws_access_key_id = XXX
aws_secret_access_key = YYY
aws_session_token = ZZZ

The An error occurred (InvalidClientTokenId) ... error might occur again and again if you forget to remove the previous temporary credentials.

So, if you're using environment variables don't forget to unset all variables before new execution of aws sts get-session-token:

unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN

If you're using named profiles - don't forget to update the profile under .aws/credentials.

like image 154
RtmY Avatar answered Oct 21 '22 13:10

RtmY


Do you have MFA enabled on your account? You might have to run

aws sts get-session-token

Details for how to use MFA with the cli are documented here.

like image 31
Alex Nelson Avatar answered Oct 21 '22 14:10

Alex Nelson