How to Rotate API Keys Without Downtime: Step-by-Step Guide
API key rotation is one of the most critical security practices in modern software development, yet many teams avoid it because they fear causing outages. The truth is that rotating keys without downtime is straightforward once you understand the dual-key deployment pattern. This guide walks you through the complete process, from manual rotation to fully automated pipelines.
Why Regular Rotation Matters
Every API key has a window of vulnerability. The longer a key exists, the more likely it has been logged somewhere unexpected, committed to a repository, or captured in a debug session. Industry standards from NIST and SOC 2 compliance frameworks recommend rotating credentials every 30 to 90 days, with 60 days being the most common target for production systems.
Beyond compliance, regular rotation limits blast radius. If a key is compromised but rotated every 30 days, the attacker's access window is automatically capped. Compare this to a static key that has been active for two years, granting unlimited access until someone manually discovers the breach.
The Dual-Key Deployment Pattern
The core technique for zero-downtime rotation is dual-key deployment: your application accepts two valid keys simultaneously during a transition window. Here is how it works at each stage:
Step 1: Generate the New Key
Create a new API key in your provider's dashboard or via their API. At this point, both the old key and the new key are active and valid.
# AWS example: create a new access key for a service account
aws iam create-access-key --user-name my-service-account
# The response includes the new AccessKeyId and SecretAccessKey
# Both the old and new keys are now active simultaneously
Step 2: Deploy the New Key to All Consumers
Update your secrets store (environment variables, Vault, AWS Secrets Manager) with the new key. Roll out the change across all services that consume the key. During this phase, your provider accepts both keys, so there is zero risk of downtime.
# Update the secret in AWS Secrets Manager
aws secretsmanager update-secret \
--secret-id prod/api/stripe-key \
--secret-string '{"api_key":"sk_live_NEW_KEY_VALUE"}'
# Trigger a rolling deployment so all instances pick up the new value
kubectl rollout restart deployment/payment-service
Step 3: Verify the New Key Is Active Everywhere
This step is the most commonly skipped and the most important. Before deactivating the old key, confirm that every consumer has successfully switched to the new one.
# Check application logs for which key version is in use
# Look for successful API calls with the new key's fingerprint
aws cloudwatch logs filter-events \
--log-group-name /app/payment-service \
--filter-pattern '"NEW_KEY_PREFIX"' \
--start-time $(date -d '10 minutes ago' +%s000)
Step 4: Deactivate the Old Key
Once you have confirmed all traffic uses the new key, deactivate (do not delete) the old key. Wait 24 to 48 hours before permanent deletion in case a forgotten service still references it.
# Deactivate the old key (can be reactivated if needed)
aws iam update-access-key \
--user-name my-service-account \
--access-key-id AKIAOLD_KEY_ID \
--status Inactive
# Schedule permanent deletion after 48 hours
echo "Delete AKIAOLD_KEY_ID after $(date -d '+48 hours')" >> rotation-log.txt
Recommended Rotation Schedules
Not all keys deserve the same rotation frequency. Match the schedule to the risk level:
- 30-day rotation: Payment processors (Stripe, Braintree), cloud provider root keys, database credentials for production systems.
- 60-day rotation: Third-party SaaS integrations (Twilio, SendGrid), monitoring service keys, CI/CD pipeline tokens.
- 90-day rotation: Internal service-to-service keys within a VPC, development and staging environment keys, read-only analytics keys.
- Immediate rotation: Any key that may have been exposed in logs, repositories, error messages, or a team member departure.
Automating Rotation with AWS Secrets Manager
AWS Secrets Manager has built-in rotation support using Lambda functions. Here is a configuration that rotates a database password every 30 days:
# rotation-template.yaml (CloudFormation)
Resources:
MyDatabaseSecret:
Type: AWS::SecretsManager::Secret
Properties:
Name: prod/db/main-password
GenerateSecretString:
SecretStringTemplate: '{"username": "admin"}'
GenerateStringKey: password
PasswordLength: 32
ExcludeCharacters: '"@/\'
MyRotationSchedule:
Type: AWS::SecretsManager::RotationSchedule
Properties:
SecretId: !Ref MyDatabaseSecret
RotationLambdaARN: !GetAtt RotationFunction.Arn
RotationRules:
AutomaticallyAfterDays: 30
The Lambda function handles all four steps of the dual-key pattern: creating the new secret, setting it in the database, testing the new credentials, and marking the rotation as complete.
Automating Rotation with HashiCorp Vault
Vault takes a different approach with dynamic secrets. Instead of rotating a long-lived key, Vault generates short-lived credentials on demand. Each credential automatically expires, so there is nothing to rotate.
# Enable the AWS secrets engine
vault secrets enable aws
# Configure the root credentials Vault uses to generate dynamic keys
vault write aws/config/root \
access_key=AKIA_VAULT_ROOT \
secret_key=vault_root_secret \
region=us-east-1
# Create a role that generates keys with a 1-hour TTL
vault write aws/roles/my-service \
credential_type=iam_user \
policy_arns=arn:aws:iam::123456789:policy/MyServicePolicy \
default_ttl=1h \
max_ttl=4h
# Applications request fresh credentials as needed
vault read aws/creds/my-service
# Returns a unique access_key and secret_key valid for 1 hour
This model eliminates the rotation problem entirely. No key lives long enough to become a meaningful security risk.
Monitoring During Rotation
Even with automation, you need visibility into the rotation process. Set up alerts for these scenarios:
- Authentication failures spike: If error rates increase within 15 minutes of a rotation event, something did not pick up the new key. Trigger an automatic rollback by reactivating the old key.
- Old key still in use: After the deployment window closes, any request using the old key fingerprint indicates a missed consumer. Alert the team before deactivation.
- Rotation Lambda failures: If the automated rotation function fails, the secret stays on the old key. This is safe but means your rotation schedule is slipping.
- TTL expiration warnings: For Vault dynamic secrets, alert when a lease is within 10 minutes of expiration and has not been renewed.
The dual-key pattern makes zero-downtime rotation possible for any API key. Start with a 60-day manual rotation schedule, then automate with Vault or Secrets Manager once the process is proven. The goal is to make rotation so routine that it becomes invisible to your team.
Common Pitfalls to Avoid
- Deleting before deactivating. Always deactivate first and wait 48 hours. Deletion is permanent and you cannot recover if a forgotten service breaks.
- Rotating during peak traffic. Schedule rotations during low-traffic windows to minimize the blast radius of any issues.
- Skipping verification. The verification step between deploying the new key and deactivating the old one is non-negotiable. Automate it with health checks.
- Hardcoded key references. If any service reads keys from a config file instead of a secrets manager, that service will break on rotation. Audit all consumers first.
- No rotation runbook. Document every step, including rollback procedures. When rotation happens at 3 AM via automation, the on-call engineer needs clear instructions for any failure scenario.
API key rotation does not have to be a source of anxiety. With the dual-key pattern, sensible schedules, and automation through tools like Vault or AWS Secrets Manager, you can rotate keys as frequently as needed while maintaining 100% uptime. Start with one key, prove the process, then expand to every credential in your infrastructure.
Recommended Resources
- Real-World Cryptography — understand the cryptographic principles behind key rotation, key derivation, and forward secrecy.
- YubiKey 5 NFC — Hardware Security Key — protect your Vault, AWS, and API provider dashboards with hardware 2FA during rotation procedures.