Skip to main content

Overview

The CustomerManagedKey resource provides AWS KMS encryption keys for protecting sensitive data at rest. It creates an alias automatically and is retained by default to prevent accidental data loss.

Resource Class

Basic Usage

Configuration Options

KMS keys cannot be deleted immediately. Setting removalPolicy to RemovalPolicy.DESTROY schedules the key for deletion with a 14-day pending window, during which the key is disabled but recoverable. Deleting a KMS key makes every value it encrypted permanently unrecoverable, so keep RemovalPolicy.RETAIN for production data.

Default Features

Every CustomerManagedKey includes:
  • Removal policy: RemovalPolicy.RETAIN (prevents accidental deletion). Set removalPolicy: RemovalPolicy.DESTROY to schedule deletion with a 14-day pending window instead.
  • Automatic alias: Created with the specified or default name
  • CloudFormation outputs: Both key and alias ARNs exported

Key Components

The KMS Key

The Key Alias

Common Patterns

Database Encryption

S3 Bucket Encryption

Secrets Manager Integration

EBS Volume Encryption

Key Policies

Default Key Policy

The default policy allows:
  • Root account full access
  • Key administrators to manage
  • Key users to encrypt/decrypt

Custom Key Policy

Cross-Account Access

Key Rotation

Enable Automatic Rotation

Removal Policy

Keys are retained when the stack is destroyed by default. Pass removalPolicy: RemovalPolicy.DESTROY only for throwaway environments where the encrypted data is disposable.
When removalPolicy is RemovalPolicy.DESTROY, the construct sets a 14-day pending window. AWS disables the key immediately, then deletes it after the window elapses. You can cancel the deletion during that window to recover the key and any data it protects.

Outputs

The construct automatically creates these CloudFormation outputs:

Grant Permissions

Encryption/Decryption

Key Management

Service Integration

With RDS

With Lambda

With CloudWatch Logs

Cost Considerations

Pricing

  • $1.00 per month per key
  • $0.03 per 10,000 requests

Cost Optimisation

  1. Share keys across resources when appropriate
  1. Use bucket keys for S3
  1. Cache decrypted values in Lambda

Complete Example

Best Practices

  1. Use aliases for easier key management
  2. Enable key rotation for long-lived keys
  3. Separate keys by purpose (data, secrets, logs)
  4. Grant least privilege access
  5. Monitor key usage with CloudTrail
  6. Use key policies for fine-grained control
  7. Tag keys for cost allocation

Security Considerations

Key Deletion Protection

Compliance

Troubleshooting

Common Issues

  1. Access denied: Check key policy and IAM permissions
  2. Key not found: Verify alias or key ID
  3. Throttling: Implement exponential backoff
  4. Invalid grant: Check principal permissions

Debug Commands

Next Steps

Secrets Manager

Store and rotate secrets encrypted with a customer-managed key.

S3 Bucket

Encrypt object storage at rest with your KMS key.

RDS Aurora

Provision an Aurora cluster with KMS storage and backup encryption.

IAM Role

Grant roles least-privilege encrypt and decrypt permissions.