. Advertisement .
..3..
. Advertisement .
..4..
You can encounter the error: “The Security Token included in the Request is Invalid” while working on the Java applications. Keep calm and follow this article to know how to fix this error naturally and rapidly. Let’s join us for useful information below.
When will the error: “The Security Token included in the Request is Invalid” happen?
This error will occur when attempting to access the AWS service through the API endpoint. Besides, as for other cases, the credential is invalid or eliminated.
Suppose your Java apps run the AWS SDK and encounter to get the error message like the below:
The security token included in the request is invalid
The root causes of this error are below:
- Temporary credentials run in your app and are invalidly assigned for their creation.
- The instance’s time and date are incorrect. Thus, it leads to a rejection of AWS credentials.
How to troubleshoot this error?
Solution 1: Apply custom temporary AWS credentials
First, you need to refresh temporary credentials within minutes before the credentials are invalid.
Then, you run the Java program, and this error will be tackled successfully.
Solution 2: Implement the Amazon Time Sync mode
The second solution to this error is to use NTP (Network Time Protocol) sources or the Amazon Time Sync mode. It ensures that the instance brings a precise and consistent time source.
Finally, when you run your Java program, the error message: “The Security Token included in the Request is Invalid” is deleted.
Solution 3: Use the IAM role
The first step is attaching the instance profile. Next, you make sure that no credentials are assigned to the instance. Then, to check the credentials with the IAM role, which is attached to the instance, you have to run the commands from your operating system.
Remember to substitute ‘examplerole’ with the IAM role’s name.
1. Windows
Let’s take the ‘Invoke-RestMethod’ command below:
PS C:\> Invoke-RestMethod http://169.254.169.254/latest/meta-data/iam/security-credentials/examplerole
At the moment, the command will return the result.
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
Afterward, these credentials will rotate or refresh automatically some minutes before the specified temp credentials’ expiration.
2. Linux
When it comes to the Linux operating system, you should apply the ‘curl’ command to check AWS credentials:
$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/examplerole
Now, the command will return the output like below.
{
"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"
}
However, if you face the 404 error when you run the initial ‘curl’ command, please do some steps below.
- Step 1: Use the command below.
$ export NO_PROXY=169.254.169.254
- Step 2: In this case, the instance is not creating a wide range of similar requests. They can lead to throttling by IMDS.
Then, modify AWS_METADATA_SERVICE_NUM_ATTEMPTS like below. Let’s set options by applying the variables below.
AWS_METADATA_SERVICE_TIMEOUT = 10
AWS_METADATA_SERVICE_NUM_ATTEMPTS = 5
- Step 3: You have to adjust the options below in case you run the curl test in the docker container:
$ 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
- Step 4: Lastly, check the instance profile carefully.
Conclusion
That’s all about the error: “The Security Token included in the Request is Invalid” and essential approaches. Finally, feel free to keep in contact with ITtutoria if possible. We are always willing to help you. Thank you for your reading!
Read more:
→ Tips On Fixing The Error “The token ‘&&’ is not a valid statement separator in this version”
Leave a comment