Boto3 is the Amazon Web Services (AWS) Software Development Kit (SDK) for Python. It allows developers to write software that makes use of Amazon services like S3, EC2, and DynamoDB. Boto3 provides an easy-to-use, object-oriented API as well as low-level access to AWS services.
When using Boto3, you might encounter the ExpiredToken
error. This error typically manifests when you attempt to make a request to an AWS service, and the request fails with an error message stating that the security token is expired.
The error message usually looks like this:
{"Error": {"Code": "ExpiredToken", "Message": "The security token included in the request is expired."}}
The ExpiredToken
error occurs when the temporary security credentials used to authenticate your requests to AWS have expired. These credentials include an access key ID, a secret access key, and a security token. They are typically obtained through AWS Identity and Access Management (IAM) roles or AWS Security Token Service (STS).
Temporary security credentials are designed to be short-lived for security reasons. They usually expire after a set duration, which can be as short as 15 minutes or as long as 36 hours, depending on how they were configured.
To resolve the ExpiredToken
error, you need to refresh your credentials. Here are the steps to do so:
Determine how your application is obtaining AWS credentials. Common sources include:
~/.aws/credentials
)If you are using IAM roles or AWS STS, you need to obtain new temporary credentials. This can be done by re-invoking the process that initially provided the credentials. For example, if you are using the AWS CLI, you can run:
aws sts get-session-token
For more details, refer to the AWS CLI Command Reference.
Once you have refreshed your credentials, update your application to use the new access key ID, secret access key, and security token. If you are using environment variables, set them like this:
export AWS_ACCESS_KEY_ID=new_access_key_id
export AWS_SECRET_ACCESS_KEY=new_secret_access_key
export AWS_SESSION_TOKEN=new_session_token
For more information on managing AWS credentials and security tokens, check out the following resources:
Let Dr. Droid create custom investigation plans for your infrastructure.
Book Demo