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.

Article illustration

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 boto3 and 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:GetSecretValue on a named secret.
  • Deny secretsmanager:CreateSecret to non-admin users.
  • Use conditions like aws:SourceVpce to 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.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *