When to use this runbook
- A security review asks "which Bedrock API keys exist and who owns them?"
- A long-term key is due for rotation, or one has leaked into a repo, ticket or chat.
- You want to stop teams creating keys that never expire.
Bedrock API keys come in two kinds:
| Short-term key | Long-term key | |
|---|---|---|
| Tied to | The IAM principal that generated it | An IAM user, as a service-specific credential |
| Lifetime | Up to 12 hours, or the session length if shorter | 1 to 36,600 days, or no expiry if you don't set one |
| How it's made | Console, or the aws-bedrock-token-generator library (pip, npm, Maven) | Console or aws iam create-service-specific-credential |
| Revoke | Remove permissions from the principal; the key expires on its own | Deactivate or delete the credential |
Both are sent as a bearer token, usually through the AWS_BEARER_TOKEN_BEDROCK environment variable. Long-term keys carry more risk, and AWS recommends short-term keys or normal IAM role credentials where you can use them. Treat every long-term key as a secret you will need to rotate.
Step 1: Inventory the long-term keys
List every Bedrock key in the account. --all-users can't be combined with --user-name:
aws iam list-service-specific-credentials \
--service-name bedrock.amazonaws.com \
--all-users \
--output table
For one user:
aws iam list-service-specific-credentials \
--user-name svc-bedrock-chatbot \
--service-name bedrock.amazonaws.com
Check these fields in the output:
ServiceSpecificCredentialId: the ID you use to deactivate, reset or delete.Status:ActiveorInactive.CreateDate: how old the key is.Expiration: only present when the key was created with an expiry. If there's noExpiration, the key never expires.
If your CLI rejects --all-users, it's older than the feature. Upgrade the CLI, or loop over users:
for u in $(aws iam list-users --query 'Users[].UserName' --output text); do
aws iam list-service-specific-credentials --user-name "$u" \
--service-name bedrock.amazonaws.com \
--query "ServiceSpecificCredentials[].[UserName,ServiceSpecificCredentialId,Status,CreateDate]" \
--output text
done
Then confirm what each key's user can actually do, because the key has the same Bedrock permissions as the user:
aws iam list-attached-user-policies --user-name svc-bedrock-chatbot
aws iam list-user-policies --user-name svc-bedrock-chatbot
Step 2: Rotate a key without downtime
Each IAM user can hold at most two service-specific credentials per service, so you can rotate with an overlap. If the user already has two, delete the inactive one first.
Create the new key with an expiry
aws iam create-service-specific-credential \
--user-name svc-bedrock-chatbot \
--service-name bedrock.amazonaws.com \
--credential-age-days 90
The key is in ServiceApiKeyValue in the response. It is only shown once. Write it straight into your secret store, not to a terminal log or a file:
aws iam create-service-specific-credential \
--user-name svc-bedrock-chatbot \
--service-name bedrock.amazonaws.com \
--credential-age-days 90 \
--query 'ServiceSpecificCredential.ServiceApiKeyValue' --output text \
| aws secretsmanager put-secret-value \
--secret-id prod/chatbot/bedrock-api-key \
--secret-string file:///dev/stdin
Switch the app and test
Restart or redeploy the app so it picks up the new secret. Then make a test call with the new key. The Converse endpoint takes the key as a bearer token:
export AWS_BEARER_TOKEN_BEDROCK="$(aws secretsmanager get-secret-value \
--secret-id prod/chatbot/bedrock-api-key --query SecretString --output text)"
curl -s -o /dev/null -w "%{http_code}\n" \
"https://bedrock-runtime.us-east-1.amazonaws.com/model/<model-or-inference-profile-id>/converse" \
-H "Authorization: Bearer $AWS_BEARER_TOKEN_BEDROCK" \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":[{"text":"ping"}]}],"inferenceConfig":{"maxTokens":5}}'
A 200 means the key works. A 403 means the key works but the user lacks permission for that model or Region.
Deactivate the old key, watch, then delete
Deactivate first. You can switch it back on if something you missed still uses it:
aws iam update-service-specific-credential \
--user-name svc-bedrock-chatbot \
--service-specific-credential-id <OLD_ID> \
--status Inactive
Watch the app's error rate and the Bedrock InvocationClientErrors metric for a day or one full business cycle. If something breaks, set --status Active again, find the caller, and move it to the new key. Then delete:
aws iam delete-service-specific-credential \
--user-name svc-bedrock-chatbot \
--service-specific-credential-id <OLD_ID>
Warning
reset-service-specific-credentialreplaces the key value in place. Every client using the old value fails at once. Use it only when you want an immediate cut-over, for example after a leak. For planned rotation, use the create, switch, deactivate, delete sequence above.
Step 3: Revoke a leaked key
A leaked long-term key works from anywhere until you stop it. Move fast:
- Deactivate it now with
update-service-specific-credential --status Inactive, as above. This takes seconds and you can reverse it. - Issue a replacement (Step 2) and update the legitimate app.
- Check what the key was allowed to do. It had the user's permissions: list the user's policies and remove anything beyond the Bedrock actions the app needs.
- Look for misuse. Compare
Invocations,InputTokenCountandOutputTokenCountin CloudWatch for the models the user can call against a normal day. If model invocation logging is on, filter the logs by the user's identity. Check CloudTrail for IAM changes made around the same time:
aws cloudtrail lookup-events --region us-east-1 \
--lookup-attributes AttributeKey=EventName,AttributeValue=CreateServiceSpecificCredential \
--max-results 20
- Delete the leaked key once the replacement is live.
Caution
Deleting the IAM user also deletes its keys, but it breaks every app using that user. Deactivate the single credential instead, unless the user itself is compromised.
A short-term key can't be revoked on its own. It expires within 12 hours. To cut it off sooner, remove or deny the Bedrock permissions of the principal that generated it, or deny bedrock:CallWithBearerToken for that principal.
Step 4: Restrict keys with policy
Three IAM controls cover most needs:
bedrock:CallWithBearerTokendecides whether a principal may use an API key at all.bedrock:bearerTokenType(SHORT_TERMorLONG_TERM) targets one kind of key.iam:ServiceSpecificCredentialServiceNameandiam:ServiceSpecificCredentialAgeDaysapply toiam:CreateServiceSpecificCredentialand control which service a credential is for and its maximum lifetime.
Block long-term keys across the organization
As an SCP, or as an identity policy on the users concerned:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyLongTermBedrockKeyUse",
"Effect": "Deny",
"Action": "bedrock:CallWithBearerToken",
"Resource": "*",
"Condition": { "StringEquals": { "bedrock:bearerTokenType": "LONG_TERM" } }
}
]
}
Allow long-term keys only with an expiry of 90 days or less
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyBedrockKeysOver90Days",
"Effect": "Deny",
"Action": "iam:CreateServiceSpecificCredential",
"Resource": "*",
"Condition": {
"StringEquals": { "iam:ServiceSpecificCredentialServiceName": "bedrock.amazonaws.com" },
"NumericGreaterThan": { "iam:ServiceSpecificCredentialAgeDays": "90" }
}
}
]
}
Test the deny with a non-production user before you attach it at the organization root. A create request with no --credential-age-days makes a key that never expires. Check that your policy also blocks that case. If it doesn't, add a Null condition on iam:ServiceSpecificCredentialAgeDays (names and evaluation details can change; check the Bedrock API key permissions page).
Tip
If the app runs on AWS (EC2, ECS, EKS, Lambda), it doesn't need an API key. Give it an IAM role, call Bedrock with the SDK, and you have nothing to rotate.
Prevent it next time
- Inventory Bedrock keys weekly with
list-service-specific-credentials --all-usersand alert on any key with noExpirationor older than your rotation period. - One IAM user per app, with a policy that allows only the Bedrock actions and models that app needs.
- Store keys in Secrets Manager. Never use
.envfiles in repos, and turn on secret scanning. - Enforce a maximum key age with
iam:ServiceSpecificCredentialAgeDays, and block long-term keys entirely in accounts that don't need them. - Rehearse the leak procedure (Step 3) once a quarter on a test user, so on-call knows that deactivate is the first move.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get