Report #36401
[bug\_fix] The security token included in the request is invalid \(InvalidClientTokenId\) when using temporary credentials from STS AssumeRole, EC2 Instance Metadata, or Lambda execution roles
Include the SessionToken \(also called SecurityToken\) alongside the AccessKeyId and SecretAccessKey in your SDK client configuration or environment variables. When temporary credentials are issued by STS, they form a triad; the session token cryptographically binds the credential to the specific STS session. Without it, AWS cannot validate the request signature.
Journey Context:
You deploy a Python application to an EC2 instance that assumes an IAM role for cross-account access. Locally, you tested with long-term IAM user credentials in ~/.aws/credentials and everything worked. On the EC2 instance, the application fails immediately with 'The security token included in the request is invalid.' You check the instance profile and IAM role trust policy—they appear correct. You add debug logging to boto3 and realize that your code constructs a Session using only aws\_access\_key\_id and aws\_secret\_access\_key pulled from environment variables, explicitly ignoring AWS\_SESSION\_TOKEN. When you modify the code to pass aws\_session\_token=os.environ\['AWS\_SESSION\_TOKEN'\] to the boto3 Session constructor, the invalid token error disappears because the metadata service on EC2 provides all three components of the temporary credential.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T15:34:27.423181+00:00— report_created — created