How to Rotate Claude API Keys with Secrets Manager and Lambda Without Getting Locked Out A developer has published a guide on rotating Claude API keys safely using AWS Secrets Manager and Lambda, addressing the common failure where serverless functions keep serving a stale cached secret and return 401 Unauthorized errors. The approach stores the key in Secrets Manager, uses a rotation Lambda to call PutSecretValue, and emits an EventBridge event named "ClaudeSecretRotated" so downstream services can invalidate their in-process caches. Developers rush to rotate API keys for security, only to discover their AI‑powered apps suddenly start throwing 401 errors. The hidden culprit? A stale token cached in your Node.js Lambda. This guide shows exactly how to rotate Claude secrets safely, so your code keeps talking to the model without a hiccup. Why rotate? Any credential that lets your code talk to an LLM large language model is a doorway to a costly service. If a key leaks, an attacker can run expensive prompts on your bill. Rotating the key regularly closes that door behind a potential intruder. Why is rotation tricky with serverless code? Serverless functions like AWS Lambda keep a small amount of memory alive between invocations. Many developers store a secret in a module‑level variable to avoid a network call on every request. That works—until you change the secret. The function continues to read the old value from its in‑process cache, and the LLM replies with 401 Unauthorized . Because the cache lives only inside the container, the problem disappears only when AWS discards the container a “cold start” . In plain English: Think of the cached secret as a sticky note on your desk. If you change the password, the note doesn’t automatically update; you have to throw it away and write a new one. What is Secrets Manager? AWS Secrets Manager is a fully managed vault that stores secrets like passwords, API keys, or database credentials and can rotate them automatically. It offers a simple API GetSecretValue , PutSecretValue to read and write secrets. aws secretsmanager create-secret \ --name prod/claude/api-key \ --description "Claude API key for production" \ --secret-string '{"apiKey":"YOUR INITIAL KEY"}' apiKey is the field that our code will look for. You could store additional metadata e.g., rotation schedule in the same JSON object. GetSecretValue call is billed. Calling it on every Lambda invocation can add up quickly. Tip: Store the secret in a dedicated “secrets” account and give each micro‑service read‑only access via cross‑account roles. This isolates billing and reduces blast radius. The rotation function does three things: PutSecretValue . npm install @aws-sdk/client-secrets-manager @aws-sdk/client-eventbridge js // rotationLambda.ts import { SecretsManagerClient, PutSecretValueCommand, } from "@aws-sdk/client-secrets-manager"; import { EventBridgeClient, PutEventsCommand, } from "@aws-sdk/client-eventbridge"; // The ARN of the secret we are rotating const SECRET ARN = process.env.CLAUDE SECRET ARN ; // Clients are created outside the handler so they are reused across invocations const smClient = new SecretsManagerClient {} ; const ebClient = new EventBridgeClient {} ; export const handler = async : Promise