Skip to content

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:

  1. Define a new AuthKmsKeyMmmYYYY resource
  2. Update the AuthKmsAlias to point to the new key
  3. Deploy to Staging and Production

Where MmmYYYY is the Month and Year e.g. Jun2026

Rotating ​

  1. Get the new key's Public Key from the AWS Console — navigate to the Key > Public Key
  2. Update packages/auth/src/public-keys.ts in the luna-auth package with the new public key value
  3. Publish the luna-auth package with a new Major Version
  4. Update luna-middlewares to use the new luna-auth version and publish with a new Major Version
  5. Update all services that use luna-auth and luna-middlewares to the new major versions (~36 services) — doing this across several MRs is easier for GitLab pipelines
  6. Update the auth service to use the new key ID in services/auth/application.yml in the KMSAuthKey mapping
  7. 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:

  1. Add the new AndiEncryptKmsKeyMmmYYYY resource with DeletionPolicy: Retain and UpdateReplacePolicy: Retain
  2. Ensure the existing key resource also has DeletionPolicy: Retain and UpdateReplacePolicy: Retain
  3. 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)
  4. Add a temporary EncryptKmsKeyNewId export pointing to the new key (used by re-encryption subscriber IAM during rotation):
    yaml
    EncryptKmsKeyNewId:
      Export:
        Name: !Sub ${AWS::StackName}-EncryptKmsKeyNewId
      Value: !If [ ShouldDeployKmsKeys, !GetAtt AndiEncryptKmsKeyMmmYYYY.KeyId, 'Dummy EncryptKmsKeyNewId' ]
      Description: New Encrypt KMS Key ID (rotation in progress)
  5. Update EncryptKmsAlias to 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:

yaml
- 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}-EncryptKmsKeyNewId

Normal 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:

json
{
  "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:

json
{
  "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:

json
{
  "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:

json
{
  "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_2026
  • old-key-id — the current key ID
  • new-key-id — the new key ID
  • account-num — AWS account number for the environment
  • andi-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:

  1. Remove the previous EncryptKmsKey<previous date>Id output (now unused)
  2. Remove the EncryptKmsKeyNewId output (temporary rotation export, now unused)
  3. Leave the previous key resource in place — DeletionPolicy: Retain protects it. Schedule manual deletion via the AWS Console once you are confident nothing was missed.

Deploy to Staging, then Production.


Troubleshooting ​

Job status definitions ​

StatusDefinitionRecommended action
PENDINGProcess has startedWait
PARTIALSome jobs have failedCheck job error messages in the rotation record
SUCCEEDEDAll jobs have succeededProceed
FAILEDAll jobs have failedCheck 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:

json
{
  "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
    }
  }
}

Made with ❤️ by the Luna team