Bobcares

AWS error code ExpiredToken – How to resolve

by | Sep 29, 2021

Stuck with AWS error code ExpiredToken? We can help you.

All application API requests to Amazon Web Services (AWS) must cryptographically signed using credentials issued by AWS.

Here, at Bobcares, we assist our customers with several AWS queries as part of our AWS Support Services.

Today, let us see how our techs fix this.

How to resolve AWS error code ExpiredToken?

Typical error might look as shown below:

com.amazonaws.AmazonServiceException: The security token included in the request is expired (Service: AmazonSQS; Status Code: 403; Error Code: ExpiredToken; Request ID: 12a345b6-78cd-901e-fg23-45hi67890jkl)

If your application uses temporary credentials when creating an AWS client, then the credentials expire at the time interval specified during their creation.

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.

If your application is running on an Amazon EC2 instance, it’s a best practice to use an AWS Identity and Access Management (IAM) role assigned to the instance.

Using an IAM role allows the use of a default service constructor.

The default constructor client searches for credentials by using the default credentials provider chain, in the following order:

1. Firstly, in system environment variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
2. Next, in the Java system properties: aws.accessKeyId and aws.secretKey.
3. In the default credentials file (the location of this file varies by platform).
4. Then, in the instance profile credentials contained in the instance metadata associated with the IAM role for the EC2 instance.

Attaching an instance profile to your instance adds instance profile credentials to the default credentials provider chain.

If instance profile credentials are available, then the default client constructor creates an instance of the AWS SDK InstanceProfileCredentialsProvider class.

AWS uses this class to sign API requests with AWS credentials using temporary security credentials from Amazon EC2 instance metadata.

Today, let us see the steps followed by our Support Techs to resolve this error.

Use Amazon Time Sync Service or NTP sources

Configure the Amazon Time Sync Service, or another Network Time Protocol (NTP) source on your Amazon EC2 instance.

This makes sure that your Linux instance has a consistent and accurate time reference.

For more information, see Set the time for your Linux instance, or Set the time for a Windows instance.

Use custom temporary AWS credentials

Refresh temporary credentials five minutes before their expiration.

Use an IAM role assigned to an instance

Attach an instance profile to your instance.

Then, verify that no other credentials are specified in your code or on the instance.

The instance profile credentials are the last place the default credential provider chain searches for credentials.

If you have credentials located anywhere earlier in the search chain, then those credentials prevent the IAM from being used.

To see the AWS credentials for an IAM role that’s attached to an instance, run the following commands from a Linux shell or from Windows PowerShell (v3.0 or later).

Be sure to replace examplerole with the name of your IAM role.

Linux

Use the curl command to see AWS credentials:

$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/examplerole

This command returns output similar to the following:

{
"Code" : "Success",
"LastUpdated" : "2016-04-26T16:39:16Z",
"Type" : "AWS-HMAC",
"AccessKeyId" : "AKIAIOSFODNN7EXAMPLE",
"SecretAccessKey" : "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"Token" : "token",
"Expiration" : "2016-04-27T22:39:16Z"
}

Note: If running the preceding curl command returns a 404 error, check the following:

1. Firstly, use the following command to verify that HTTP proxy is disabled for the metadata IP address:

$ export NO_PROXY=169.254.169.254

2. Then, verify that the instance isn’t making multiple concurrent requests and running multiple sessions in parallel.

Multiple concurrent requests and multiple sessions running in parallel might cause throttling by the Instance Metadata Service (IMDS).

To mitigate this, use caching and retries with exponential backoff. As with any service, calls might fail occasionally.

Clients are expected to retry when this occurs. For more information, see Query throttling.

To implement retries, modify AWS_METADATA_SERVICE_NUM_ATTEMPTS. You can set options using environment variables, in the ~/.aws/config file, or in the user’s botocore session.

Example:

AWS_METADATA_SERVICE_TIMEOUT = 10
AWS_METADATA_SERVICE_NUM_ATTEMPTS = 5

3. Next if you’re running the curl test in a docker container, adjust the instance-metadata-options http-put-response-hop-limit:

$ aws ec2 modify-instance-metadata-options --instance-id $(curl 169.254.169.254/latest/meta-data/instance-id) --http-put-response-hop-limit 2 --http-endpoint enabled

For more information, see Add defense in depth against open firewalls, reverse proxies, and SSRF vulnerabilities with enhancements to the EC2 Instance Metadata Service.

4. Then, verify that the instance profile is properly attached to the instance.

Windows

Use the Invoke-RestMethod command to see AWS credentials:

PS C:\> Invoke-RestMethod http://169.254.169.254/latest/meta-data/iam/security-credentials/examplerole

This command returns output similar to the following:

Code : Success
LastUpdated : 2016-07-18T18:09:47Z
Type : AWS-HMAC
AccessKeyId : AKIAIOSFODNN7EXAMPLE
SecretAccessKey : wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Token : token
Expiration : 2016-04-27T22:39:16Z

Use these commands to check the latest temporary credentials for the instance.

These credentials automatically rotate or refresh approximately five minutes before the expiration of the assigned temporary credentials.

[Need help with the resolution? We’d be glad to assist you]

Conclusion

In short, we saw how our Support Techs fix AWS error code ExpiredToken for our customers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF