bedrock-env
Generate Amazon Bedrock short-term API keys from your AWS SSO profile, and keep long-running processes (dev servers, agents, ...) supplied with a fresh key.
# One-off: set AWS_BEARER_TOKEN_BEDROCK in the current shell
eval "$(bedrock-env export --profile my-sso-profile)"
# Long-running: run a command, and restart it with a new key before the old one expires
bedrock-env run --profile my-sso-profile -- uv run uvicorn app:app --reload
Why
A short-term Bedrock API key is a SigV4 presigned URL signed with your current AWS credentials. It is generated locally (no API call), and it stops working at whichever comes first:
- the requested lifetime (max 12 hours), or
- the expiry of the credentials that signed it.
With an SSO profile the second limit usually wins: role credentials last as long as the permission set's session duration (often 1 hour). Exporting a key by hand means your dev server starts failing with 403 an hour later.
A running process's environment cannot be changed from outside, so bedrock-env run does the next best thing: it supervises the command and restarts it with a new key shortly before the current one expires.
Install
uv tool install bedrock-env
# or: pipx install bedrock-env
Requires Python 3.10+, macOS or Linux.
Usage
bedrock-env export
Prints the key so you can load it into your shell.
eval "$(bedrock-env export --profile my-sso-profile)" # bash / zsh
bedrock-env export --profile my-sso-profile --format fish | source
bedrock-env export --format dotenv > .env.bedrock
bedrock-env export --format json # {"token", "region", "expires_at"}
The expiry is printed to stderr.
bedrock-env run -- COMMAND...
Runs COMMAND with AWS_BEARER_TOKEN_BEDROCK (and AWS_REGION, if unset) in its environment. Shortly before the key expires it:
- generates a new key,
- sends
SIGTERMto the command's process group (SIGKILLafter--stop-timeout), - starts the command again with the new key.
SIGINT / SIGTERM / SIGHUP sent to bedrock-env are forwarded to the command. If the command exits on its own, bedrock-env exits with the same status and does not restart it.
| Option | Default | |
|---|---|---|
--profile |
AWS_PROFILE / default chain |
AWS profile to sign with |
--region |
AWS_REGION / profile region |
Region the key is valid for |
--duration |
12 |
Requested lifetime in hours (capped by the credentials' expiry) |
--margin |
300 |
Refresh this many seconds before expiry |
--retry-interval |
60 |
Wait between failed refresh attempts |
--stop-timeout |
10 |
Seconds between SIGTERM and SIGKILL on restart |
--auto-login |
off | Run aws sso login when the SSO session has expired |
When the SSO session expires
The SSO session itself (aws sso login) also has a lifetime. Once it expires no new role credentials can be obtained:
bedrock-env runkeeps the current process running (its key may still be valid for a few minutes), logsrun aws sso login, and retries every--retry-interval. As soon as you log in from another terminal, the next retry picks up the new session.- With
--auto-login, it runsaws sso login --profile ...itself, which opens a browser.
Caveats
- Restarts are not zero-downtime. Requests in flight during a restart fail. With SSO this happens about once per permission set session duration.
- The key is bound to a single region.
- Only processes started by
bedrock-env runget the new key. Shells where you ranexportneed to run it again. - Windows is not supported (process groups and POSIX signals are used).
If your application uses an AWS SDK directly, you may not need an API key at all: the SDK can sign requests with your SSO profile (SigV4) and refreshes credentials itself. API keys are mainly useful for tools and SDKs that only accept a bearer token.
Development
uv sync
uv run pytest
License
MIT
Metadata
Release files for bedrock-env 0.1.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 | Provenance | Actions |
|---|---|---|---|---|
| bedrock_env-0.1.0.tar.gz | 7.9 kB | Without provenance | Details for bedrock_env-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bedrock_env-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for bedrock_env-0.1.0-py3-none-any.whl |
Total release size: 18.3 kB
Release files / bedrock_env-0.1.0.tar.gz
| Download URL | bedrock_env-0.1.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9bf7bba86bd3be36934825e43e9554f2440390ff4be12f8a938d50a0676633c5
|
|
BLAKE2b-256 checksum How to use checksums |
e38045a449a825a16344470d2749bbc4b38ce0f35688c737d982c9402b6c1a28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Release files / bedrock_env-0.1.0-py3-none-any.whl
| Download URL | bedrock_env-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2eb79608fc6f94075ab8bd7bb2e77d19708ac5461c297374fe5c13f43a520d38
|
|
BLAKE2b-256 checksum How to use checksums |
c1cc135af328d7cc95f3c395c6d47d5b31c361e1c9db88beb87776ae7451e632
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Security for bedrock-env 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.