Description
Describe the bug
We use the sidekiq-cloudwatchmetrics gem to send Sidekiq-related metrics to CloudWatch. Occasionally, we encounter the following error:
Aws::CloudWatch::Errors::ExpiredToken: The security token included in the request is expired
Upon investigation, we found that the gem initializes an Aws::CloudWatch::Client
, which uses Aws::InstanceProfileCredentials
. These credentials are supposed to auto-refresh before expiration. However, the error indicates that the token is not being refreshed in time, causing the metrics to fail. Once the error starts, it repeats, and the metrics fail to recover.
Regression Issue
- Select this option if this issue appears to be a regression.
Expected Behavior
The Aws::InstanceProfileCredentials
should automatically refresh the security token before expiration (approximately 5 minutes before expiry) and ensure that the put_metric_data API call succeeds without errors. Metrics should continue to be sent to CloudWatch without interruption.
Current Behavior
The Aws::CloudWatch::Client
raises Aws::CloudWatch::Errors::ExpiredToken
errors when attempting to send metrics.
Once this error occurs:
- The metrics stop being sent to CloudWatch.
- The error persists until the process is restarted or manually handled.
- The auto-refresh mechanism in
Aws::InstanceProfileCredentials
does not appear to trigger correctly in this context.
Reproduction Steps
Configure the sidekiq-cloudwatchmetrics gem in a Sidekiq setup that uses an IAM role (e.g., on EC2 or ECS).
You can find the configuration I used here:
Allow the process to run for an extended period, relying on auto-refreshable credentials from Aws::InstanceProfileCredentials
.
Observe that after some time, the following error may appear:
Aws::CloudWatch::Errors::ExpiredToken: The security token included in the request is expired
Possible Solution
No response
Additional Information/Context
Here are some useful links for more context regarding my problem:
- The issue I opened in the
sidekiq-cloudwatchmetrics
repo Reoccurring Aws::CloudWatch::Errors::ExpiredToken errors sj26/sidekiq-cloudwatchmetrics#48 - The PR I opened to fix the problem in the
sidekiq-cloudwatchmetrics
repo https://github.com/sj26/sidekiq-cloudwatchmetrics/pull/49/files
Gem name ('aws-sdk', 'aws-sdk-resources' or service gems like 'aws-sdk-s3') and its version
aws-sdk-cloudwatch
Environment details (Version of Ruby, OS environment)
Ruby - 3.2.5
, OS - Debian GNU/Linux 12 (bookworm)
, Docker image - ruby:3.2.5-slim
, sidekiq-cloudwatchmetrics - 2.6.0