Token Generator for AWS External Anthropic (Python)
A lightweight library for generating short-term bearer tokens for AWS External Anthropic API authentication.
Installation
pip install token-generator-for-aws-external-anthropic
Quick Start
Token duration can be customized (1 second to 12 hours). The actual token lifetime will be:
min(specified duration, credentials expiry, 12 hours). Default is 12 hours.
Usage 1 — Using Default Credentials and Region
from token_generator_for_aws_external_anthropic import TokenGenerator
# Credentials and region resolved from default chain (env vars, config files, IAM roles, etc.)
generator = TokenGenerator()
token = generator.get_token()
Usage 2 — Using Custom Configuration
This example uses a profile with assume role configured. You can use any credential configuration supported by boto3. See the boto3 credentials guide for all options including assume role, SSO, environment variables, and more.
import boto3
from datetime import timedelta
from token_generator_for_aws_external_anthropic import TokenGenerator
# Profile with assume-role configured in ~/.aws/config
session = boto3.Session(profile_name="my-role-profile")
generator = TokenGenerator(session=session, region="us-east-1")
token = generator.get_token(expiry=timedelta(hours=1))
Usage 3 — Using Static One-Shot
Pass credentials, region, and expiry directly. No instance needed.
from datetime import timedelta
from botocore.credentials import Credentials
from token_generator_for_aws_external_anthropic import get_token
credentials = Credentials(
access_key="YOUR_ACCESS_KEY_ID",
secret_key="YOUR_SECRET_ACCESS_KEY",
token="YOUR_SESSION_TOKEN",
)
token = get_token(credentials=credentials, region="us-west-2", expiry=timedelta(hours=6))
Token Format
The generated token has the format:
aws-external-anthropic-api-key-<base64-encoded-payload>
The payload is a Base64-encoded SigV4 presigned URL scoped to the aws-external-anthropic service. The token can be decoded for debugging purposes but should be treated as an opaque string in production.
Requirements
- Python: 3.8 or later
- boto3: 1.33.0 or later
- botocore: 1.33.0 or later
Security Considerations
- Token Expiration: Tokens are short-lived with a maximum lifetime of 12 hours. The actual expiry is
min(specified duration, credentials expiry, 12 hours). Use the shortest practical duration for your use case. - Secure Storage: Do not log or store tokens in plain text. Treat them as sensitive credentials.
- No Embedded Credentials: No long-term credentials are embedded in the token. The token contains a SigV4 presigned URL, not the signing keys themselves.
- Credential Management: Use IAM roles or temporary credentials instead of long-term access keys where possible.
- Network Security: Always transmit tokens over HTTPS.
- Least Privilege: Scope IAM permissions to the minimum required for your use case.
- Region Scoping: Tokens are scoped to a specific AWS region and cannot be used across regions.
Development
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=token_generator_for_aws_external_anthropic
# Format code
ruff format .
# Lint
ruff check .
Contributing
See CONTRIBUTING for more information.
License
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.
Changelog
See CHANGELOG for release history.
Metadata
Release files for token-generator-for-aws-external-anthropic 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| token_generator_for_aws_external_anthropic-1.0.0.tar.gz | 16.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| token_generator_for_aws_external_anthropic-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.1 kB
Release files / token_generator_for_aws_external_anthropic-1.0.0.tar.gz
| Download URL | token_generator_for_aws_external_anthropic-1.0.0.tar.gz |
|---|---|
| Size | 16.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f240f13489b3339b17303ed3f966ec9c8ca5f61e4975700814ef58c3bc448c5d
|
|
BLAKE2b-256 checksum How to use checksums |
33ba908d351cfb66f045a6f278c7f7cabfd15ab5715cf5cdcb0c693620aee51e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.4
|
Release files / token_generator_for_aws_external_anthropic-1.0.0-py3-none-any.whl
| Download URL | token_generator_for_aws_external_anthropic-1.0.0-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f61008a73d043bb695bc6ec6b57741e1c8a09791198e53303fc09e42b3130d1e
|
|
BLAKE2b-256 checksum How to use checksums |
d18baba0daf4480fb7b251fb4911cfda452e80d5f67f40426c2f5423ee115144
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.4
|