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
Default Features
Every CustomerManagedKey includes:- Removal policy:
RemovalPolicy.RETAIN(prevents accidental deletion). SetremovalPolicy: RemovalPolicy.DESTROYto 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. PassremovalPolicy: RemovalPolicy.DESTROY only for throwaway environments where the encrypted data is disposable.
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
- Share keys across resources when appropriate
- Use bucket keys for S3
- Cache decrypted values in Lambda
Complete Example
Best Practices
- Use aliases for easier key management
- Enable key rotation for long-lived keys
- Separate keys by purpose (data, secrets, logs)
- Grant least privilege access
- Monitor key usage with CloudTrail
- Use key policies for fine-grained control
- Tag keys for cost allocation
Security Considerations
Key Deletion Protection
Compliance
Troubleshooting
Common Issues
- Access denied: Check key policy and IAM permissions
- Key not found: Verify alias or key ID
- Throttling: Implement exponential backoff
- 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.