Claude/Code/Bedrock/How To/Get Around Security Token Timeout
Properties
- tags
- Diataxis/How To, Claude/Code, AWS/Bedrock
How To Handle Security Token Expiration Errors with Claude Code and AWS Bedrock
Problem
- When using Claude/Code with AWS/Bedrock, you may encounter the error:
- This occurs when AWS credentials (STS tokens, SSO sessions, etc.) have expired and Claude Code needs to refresh them.
Expected Behavior
- According to the official documentation, Claude Code should automatically detect expired credentials and run your configured
awsAuthRefreshcommand before retrying the request. - Detection happens in two ways:
- According to the official documentation, Claude Code should automatically detect expired credentials and run your configured
Configuration
Basic Setup
Command Types
awsAuthRefresh: For commands that modify the.awsdirectory (credentials, SSO cache, config files)- Output is shown to the user
- User input is NOT supported (see GitHub Issue #11264 for feature request to support interactive authentication)
- Suitable for browser-based authentication flows
- Limitation: Interactive authentication tools (like
gimme-aws-credswith password/MFA prompts) cannot be used withawsAuthRefreshcurrently
awsCredentialExport: For commands that output credentials directly as JSON- Output is captured silently (not shown)
- Must output JSON in this format:
- Example Commands:
- Using AWS STS assume-role with jq (most common):
- json
"awsCredentialExport": "aws sts assume-role --role-arn arn:aws:iam::123456789012:role/MyRole --role-session-name mysession | jq '{Credentials: .Credentials | {AccessKeyId, SecretAccessKey, SessionToken}}'" - Reference: AWS CLI Command Reference: assume-role
- Using AWS STS get-session-token with jq:
- json
"awsCredentialExport": "aws sts get-session-token | jq '{Credentials: .Credentials | {AccessKeyId, SecretAccessKey, SessionToken}}'" - Reference: AWS CLI Command Reference: get-session-token
- Using a custom script:
- json
"awsCredentialExport": "/path/to/generate_aws_grant.sh" - The script should output the JSON format shown above to stdout
- Reference: Claude Code Settings Documentation - See
awsCredentialExportsetting
- Using AWS CLI export-credentials (if your AWS CLI version supports it):
- json
"awsCredentialExport": "aws configure export-credentials --profile myprofile --format json | jq '{Credentials: {AccessKeyId: .AccessKeyId, SecretAccessKey: .SecretAccessKey, SessionToken: .SessionToken}}'" - Reference: AWS CLI Command Reference: configure export-credentials
- Using a wrapper script with environment variables:
- json
"awsCredentialExport": "bash -c 'aws sts assume-role --role-arn $AWS_ROLE_ARN --role-session-name $SESSION_NAME | jq \"{Credentials: .Credentials | {AccessKeyId, SecretAccessKey, SessionToken}}\"'" - References:
- AWS CLI Command Reference: assume-role
- jq Manual - For jq filter syntax
- Using AWS STS assume-role with jq (most common):
- Important Notes:
- The command must output only valid JSON to stdout (no error messages or other output)
- Use
jqto filter AWS STS output, as it includes additional fields (Expiration,AssumedRoleUser, etc.) that Claude Code doesn't need - Ensure
jqis available in Claude Code's PATH if your command uses it - For commands with pipes, you may need to wrap them in
bash -corsh -c - Test your command manually first to ensure it outputs the correct JSON format
Troubleshooting: When Automatic Refresh Doesn't Work
Issue: Command Not Triggering
- If you see the expired token error but no indication that
awsAuthRefreshran: - Workaround: Manually refresh credentials before using Claude Code
- If you see the expired token error but no indication that
Issue: Credentials Refreshed But Not Used (Known Bug)
- Symptom:
awsAuthRefreshruns successfully and credentials are updated in~/.aws/credentials, but Claude Code still fails with expired token errors - Cause: This is a known bug in Claude Code where the refreshed credentials aren't always picked up after
awsAuthRefreshcompletes - Workaround: Exit and resume Claude Code to force it to reload credentials
- Alternative Workaround: Use
awsCredentialExportinstead ofawsAuthRefresh - Note: The issue tracker includes a request for a
/refresh-credentialcommand to manually trigger credential reload without exiting
- Symptom:
Issue: Command Not Found
Issue: Interactive Commands
- If your refresh command requires user interaction (like password entry, MFA approval, Touch ID):
- Claude Code's
awsAuthRefreshshows output but doesn't support user input - This may cause the command to hang or fail
- Current Workaround: Use
awsCredentialExportinstead, or ensure your command can run non-interactively - Future Enhancement: GitHub Issue #11264 requests support for interactive
awsAuthRefreshto enable enterprise authentication flows (gimme-aws-creds, saml2aws, etc.) - Alternative: For now, manually refresh credentials in a separate terminal before using Claude Code
- Claude Code's
- If your refresh command requires user interaction (like password entry, MFA approval, Touch ID):
Issue: Environment Variables Not Available
Manual Refresh Workflow
Related Pages
- Claude/Code/Bedrock - Main Bedrock integration guide
- mise/Task/How To/invoke aws_okta_keyman from mise with a configuration that references a default AWS account - Example of setting up credential refresh scripts
- Claude/Code/Settings - Claude Code settings documentation
References
- Claude Code on Amazon Bedrock - Advanced Credential Configuration
- Claude Code Settings Documentation
- GitHub Issue #3823: CC not taking the updated credentials with awsAuthRefresh - Known bug where refreshed credentials aren't always used
- GitHub Issue #11264: Interactive awsAuthRefresh - Feature request to support interactive authentication flows (password/MFA prompts) within Claude Code