Rotate KMS keys
This guide will walk you through how to rotate KMS keys on Luna.
The KMS keys rotation process should be performed once a year in line with what is outlined in our policies.
Requirements
- AWS Admin access (All Accounts)
Auth KMS Key
This key is used to sign and verify users against OKTA / Cognito.
Creating the new key
In infrastructure/kms/application.yml:
- Define a new
AuthKmsKeyMmmYYYYresource - Update the
AuthKmsAliasto point to the new key - Deploy to Staging and Production
Where MmmYYYY is the Month and Year e.g. Jun2026
Rotating
- Get the new key's Public Key from the AWS Console — navigate to the Key > Public Key
- Update
packages/auth/src/public-keys.tsin theluna-authpackage with the new public key value - Publish the
luna-authpackage with a new Major Version - Update
luna-middlewaresto use the newluna-authversion and publish with a new Major Version - Update all services that use
luna-authandluna-middlewaresto the new major versions (~36 services) — doing this across several MRs is easier for GitLab pipelines - Update the
authservice to use the new key ID inservices/auth/application.ymlin theKMSAuthKeymapping - Deploy all services to Staging and Production
Note: Lower environments will also need updating if the old Auth Key gets marked for deletion.
Cleanup
Remove the old AuthKmsKeyMmmYYYY resource from infrastructure/kms/application.yml and deploy.
ANDi KMS Key
This key is used to encrypt and decrypt sensitive ANDi data such as bank details, shares, and reports.
Rotation requires decrypting existing data with the old key and re-encrypting with the new key via Janitor Key Rotation Messages.
CloudFormation export constraint: You cannot update the value of an existing CF export while other stacks import it. Each rotation therefore adds a new date-stamped export (
EncryptKmsKeyMmmYYYYId) for services to migrate to. The old export is cleaned up once all stacks have moved off it.
Step 1 — IaC: Add New Key (MR 1)
In infrastructure/kms/application.yml:
- Add the new
AndiEncryptKmsKeyMmmYYYYresource withDeletionPolicy: RetainandUpdateReplacePolicy: Retain - Ensure the existing key resource also has
DeletionPolicy: RetainandUpdateReplacePolicy: Retain - Add a new date-stamped export for the new key:yaml
EncryptKmsKeyMmmYYYYId: Export: Name: !Sub ${AWS::StackName}-EncryptKmsKeyMmmYYYYId Value: !If [ ShouldDeployKmsKeys, !GetAtt AndiEncryptKmsKeyMmmYYYY.KeyId, 'Dummy EncryptKmsKeyMmmYYYYId' ] Description: Encrypt KMS Key ID (Mmm YYYY) - Add a temporary
EncryptKmsKeyNewIdexport pointing to the new key (used by re-encryption subscriber IAM during rotation):yamlEncryptKmsKeyNewId: Export: Name: !Sub ${AWS::StackName}-EncryptKmsKeyNewId Value: !If [ ShouldDeployKmsKeys, !GetAtt AndiEncryptKmsKeyMmmYYYY.KeyId, 'Dummy EncryptKmsKeyNewId' ] Description: New Encrypt KMS Key ID (rotation in progress) - Update
EncryptKmsAliasto point to the new key
Deploy to Staging, then Production.
Step 2 — Services: Grant Re-Encryption Permissions (MR 2)
In services/andi/application.yml, services/shares/application.yml, and services/reports/application.yml:
In the re-encryption subscriber function of each service, split the 3 KMS policies so the new key has encrypt/decrypt access while the old key retains decrypt access:
- KMSDecryptPolicy: # For data encrypted with old key
KeyId:
Fn::ImportValue: !Sub ${KmsInfrastructureStackName}-<current-export-name>
- KMSEncryptPolicy: # For new data encryption
KeyId:
Fn::ImportValue: !Sub ${KmsInfrastructureStackName}-EncryptKmsKeyNewId
- KMSDecryptPolicy: # For data encrypted with new key (consistency)
KeyId:
Fn::ImportValue: !Sub ${KmsInfrastructureStackName}-EncryptKmsKeyNewIdNormal querier and commands functions are unchanged — they continue using the current key for live traffic.
Deploy to Staging, then Production.
Step 3 — Re-Encryption
Run Staging first, verify all jobs succeed in staging-janitor-key-rotations-table, then repeat for Production.
Retrieve the old and new key IDs from the AWS Console (KMS → Customer managed keys) or CF stack outputs.
Run in this order: ANDi → Bank Details → Shares → Reports
ANDi Data (~5 min)
Publish to {env}-andis-data-rotation-requested:
{
"event": "ANDIS_DATA_ROTATION_REQUESTED",
"payload": {
"id": "DD_MMM_YYYY_ROTATION_ANDI_ANDI",
"old": { "id": "old-key-id", "account": "account-num" },
"new": { "id": "new-key-id", "account": "account-num" },
"actorId": "ANDI_andi-id"
}
}ANDi Bank Details (~5 min)
Publish to {env}-andis-update-bank-details-data-rotation-requested:
{
"event": "ANDIS_UPDATE_BANK_DETAILS_DATA_ROTATION_REQUESTED",
"payload": {
"id": "DD_MMM_YYYY_ROTATION_ANDI_BANK_DETAILS",
"old": { "id": "old-key-id", "account": "account-num" },
"new": { "id": "new-key-id", "account": "account-num" },
"actorId": "ANDI_andi-id"
}
}Shares (~5 min)
Publish to {env}-shares-data-rotation-requested:
{
"event": "SHARES_DATA_ROTATION_REQUESTED",
"payload": {
"id": "DD_MMM_YYYY_ROTATION_ANDI_SHARES",
"old": { "id": "old-key-id", "account": "account-num" },
"new": { "id": "new-key-id", "account": "account-num" },
"actorId": "ANDI_andi-id"
}
}Reports (~4 hours per year — send one message per year since 2021)
Publish to {env}-reports-data-rotation-requested for each year:
{
"event": "REPORTS_DATA_ROTATION_REQUESTED",
"payload": {
"id": "DD_MMM_YYYY_ROTATION_ANDI_REPORTS_yyyy",
"old": { "id": "old-key-id", "account": "account-num" },
"new": { "id": "new-key-id", "account": "account-num" },
"actorId": "ANDI_andi-id",
"options": { "year": "yyyy" }
}
}Where:
DD_MMM_YYYY— date of the rotation e.g.05_MAY_2026old-key-id— the current key IDnew-key-id— the new key IDaccount-num— AWS account number for the environmentandi-id— your ANDi ID (for audit purposes)yyyy— the year to run for e.g.2022
Monitor progress in {env}-janitor-key-rotations-table. Wait for SUCCEEDED before proceeding to Production.
Step 4 — Services: Switch to New Key (MR 3)
After all re-encryption jobs have succeeded on both Staging and Production.
In services/andi/application.yml, services/shares/application.yml, and services/reports/application.yml:
Replace all references to the current export name with EncryptKmsKeyMmmYYYYId across every function — querier KEY_ID, querier IAM, commands KEY_ID, commands IAM, and all 3 re-encryption subscriber policies (collapse them back to a single key reference).
Deploy to Staging, then Production, across all environments.
Step 5 — IaC Cleanup (MR 4)
Once all service stacks across all environments have been redeployed:
In infrastructure/kms/application.yml:
- Remove the previous
EncryptKmsKey<previous date>Idoutput (now unused) - Remove the
EncryptKmsKeyNewIdoutput (temporary rotation export, now unused) - Leave the previous key resource in place —
DeletionPolicy: Retainprotects it. Schedule manual deletion via the AWS Console once you are confident nothing was missed.
Deploy to Staging, then Production.
Troubleshooting
Job status definitions
| Status | Definition | Recommended action |
|---|---|---|
| PENDING | Process has started | Wait |
| PARTIAL | Some jobs have failed | Check job error messages in the rotation record |
| SUCCEEDED | All jobs have succeeded | Proceed |
| FAILED | All jobs have failed | Check input message for errors |
Individual job failures (e.g. KMS rate limiting)
Failed jobs are retried up to 5 times before landing on {env}-andi-deadletter-queue. Rather than replaying the DLQ blind, look up the failed job IDs in {env}-janitor-key-rotations-table and republish them individually to {env}-andi-data-reencryption-requested:
{
"event": "ANDIS_DATA_RE-ENCRYPTION_REQUESTED",
"payload": {
"id": "<job-id-from-janitor-table>",
"entityId": "<entity-id-from-janitor-table>",
"rotation": {
"id": "DD_MMM_YYYY_ROTATION_ANDI_ANDI",
"type": "Rotation",
"status": "PENDING",
"entityDomain": "ANDI",
"key": {
"old": { "id": "old-key-id", "account": "account-num" },
"new": { "id": "new-key-id", "account": "account-num" }
},
"actorId": "ANDI_andi-id",
"createdAt": 0,
"updatedAt": 0
}
}
}