Guide to Using AWS Secrets Manager
Introduction
AWS Secrets Manager is a powerful secrets management service from Amazon Web Services, designed to help you protect sensitive information such as database credentials, API keys, and other authentication tokens throughout their lifecycle.
Instead of “hard-coding” sensitive information directly into your source code or application configuration - which carries significant security risks—you can centrally store them on AWS and retrieve them securely through API calls.
Key Advantages
- Automatic Password Rotation: This is the most valuable feature. Secrets Manager can automatically rotate passwords (for example, for RDS) on a schedule without manual intervention or application downtime.
- Maximum Security with KMS: All stored information is encrypted using AWS Key Management Service (KMS), ensuring data remains secure even while at rest.
- Fine-Grained Access Control: Deep integration with AWS IAM allows precise control over which users or services can access specific secrets.
- Monitoring and Auditing: Seamless integration with AWS CloudTrail enables you to track access history - who retrieved a secret, when, and from where.
- Reduced Operational Risk: Eliminates the risk of exposing secrets accidentally when pushing code to public repositories (such as GitHub or GitLab).
How to Use
Continue using AWS CDK and create the file lib/secret-manager-stack.ts:
Explanation:
- secretsmanager.Secret: used to create a secret, where secretName is the name used to access it.
- The configuration in generateSecretString will create a JSON object like:
- The password is randomly generated with a default length of 32 characters; you can customize the length and whether to allow special characters.
- A Lambda function is created to automatically rotate the password. This is a general approach when you want full control over password rotation. For database passwords, Secrets Manager provides ready-made Lambda templates for simpler rotation.
- addRotationSchedule: configures password rotation; the minimum interval is 1 hour.
Next, create the file rotation-key.ts as the Lambda function:
- Password rotation consists of four stages:
- createSecret: Generate a new password and assign it the AWSPENDING version stage.
- setSecret: Synchronize the new password with the target service (for example, call another service’s API to change the password).
- testSecret: Verify that the new password actually works.
- finishSecret: Promote the new password version to AWSCURRENT.
- In this example, setSecret and testSecret are simulated and do not include real processing.
After deploying the stack, a secret will be created.
To use the secret in NestJS, first create the file secret-manager.service.ts:
Just like parameter storage, every call to AWS to retrieve values counts toward request costs ($0.40 / secret / month, $0.05 / 10,000 requests). Therefore, this example caches the secret for 5 minutes before fetching the latest value from AWS again. You can adjust the duration as needed.
Create the file secret-manager.controller.ts:
This is only a demo example, so an API is created to return secret information. In real applications, these are sensitive details and should not be exposed publicly like this.
Add the service and controller to app.module.ts:
Run the application to see the result.
We can use CloudTrail to check the event history and see which actions have been performed on our secrets.
Happy coding!
Comments
Post a Comment