How to Manage Secrets in AWS: A Practical Guide Using AWS Secrets Manager
Managing secrets such as database passwords, API keys, and OAuth tokens securely is essential for any cloud application. AWS Secrets Manager provides a fully managed service to store, rotate, and retrieve secrets throughout their lifecycle. This tutorial covers the key steps to manage secrets effectively in AWS.
Hard-coding secrets in code or configuration files is a common security risk. AWS Secrets Manager encrypts secrets at rest using AWS KMS, enforces fine-grained access through IAM policies, and supports automatic rotation to reduce exposure. It also integrates with services like RDS, Redshift, and Lambda.
1. Creating a Secret
Use the AWS Management Console, CLI, or SDK to create a secret. For example, via AWS CLI:
- Run
aws secretsmanager create-secret --name MyDatabaseSecret --secret-string '{"username":"admin","password":"P@ssw0rd"}' - Choose a KMS encryption key (default or custom).
- Enable automatic rotation if desired (requires a rotation Lambda function).
2. Accessing Secrets from Applications
Retrieve secrets at runtime using the AWS SDK. Example in Python:
- Import
boto3and create a Secrets Manager client. - Call
get_secret_value(SecretId='MyDatabaseSecret')to fetch the secret. - Parse the JSON to extract credentials and use them in your connection.
This avoids storing secrets in environment variables or source code.
3. Automating Secret Rotation
Enable rotation to automatically update secrets on a schedule. AWS provides managed Lambda functions for services like RDS, or you can write custom rotation logic. Steps:
- Create a Lambda function with permission to update the secret and service.
- Attach the rotation policy to the secret.
- Set rotation interval (e.g., 30 days).
- Test the rotation without breaking existing applications.
4. Best Practices with IAM Policies
Use least-privilege permissions. Create IAM policies that restrict access to specific secrets and actions. Example:
- Allow only
secretsmanager:GetSecretValueon a named secret. - Deny
secretsmanager:CreateSecretto non-admin users. - Use conditions like
aws:SourceVpceto limit API calls to specific VPC endpoints.
Regularly audit secret usage with AWS CloudTrail and enable secret rotation to minimise risk.
By following these steps, you can securely manage secrets in AWS, reduce exposure, and maintain compliance with security best practices.