Overview
The Secret resource stores sensitive values such as database passwords, API keys, and other credentials. Each secret is encrypted with a Customer Managed Key (CMK) and accepts simple strings or JSON objects.Resource Class
Basic Usage
Simple Secret
Generated Password
JSON Secret
Configuration Options
Core Properties
Secret Value Options (use one)
Default Features
Every secret includes:- KMS encryption with Customer Managed Key
- Automatic key creation with alias
cmk/${id} - Key retention on stack deletion (RETAIN policy)
- Import/export capabilities
Generated Passwords
Basic Password Generation
Database Password with Username
API Key Generation
Import/Export Pattern
Export for Cross-Stack Usage
Import Specific Field
Integration with Resources
With RDS
With ECS
With Lambda
KMS Encryption
Default CMK Creation
Access the CMK
Secret Rotation
Enable Rotation
Factory Pattern
Using StackBuilder
IAM Permissions
Grant Read Access
Custom Permissions
Outputs
The nestedCustomerManagedKey construct creates the KMS key outputs. Export
names key off the CMK construct id (${id}CustomerManagedKey), run through
toPascalCase, so the value below is name = toPascalCase($CustomerManagedKey):
Complete Example
Best Practices
- Never hardcode secrets. Store them in Secrets Manager.
- Use generated passwords for databases.
- Rotate secrets every 30 to 90 days.
- Apply least-privilege IAM policies.
- Separate secrets by purpose. Do not mix unrelated secrets.
- Tag secrets for cost allocation and compliance.
- Monitor access with CloudTrail.
Cost Optimisation
Pricing
- $0.40 per secret per month
- $0.05 per 10,000 API calls
Cost Saving Tips
- Combine related secrets in JSON objects
- Cache secrets in Lambda functions
- Use Parameter Store for non-sensitive config
Security Considerations
Secret Value Security
Access Patterns
Troubleshooting
Next Steps
KMS Key
Configure the customer-managed key that encrypts each secret.
IAM Role
Grant roles least-privilege read access to secrets.
RDS Instance
Manage database credentials with generated secrets.
ECS Cluster
Inject secrets into container tasks at runtime.