AWS Builder Center

Stop Hardcoding Secrets! Secure Your Application with AWS Secrets Manager

Protect API keys, database passwords, and application credentials using AWS Secrets Manager and Python.

1. Introduction

Imagine pushing your Python project to GitHub and accidentally exposing your database password.
One small mistake could give an attacker access to sensitive application data.
Hardcoded passwords, API keys, and access tokens are serious security risks.
Instead of storing credentials inside source code, we can use AWS Secrets Manager to securely manage and retrieve secrets.

2. Architecture

Python Application
|
v
IAM Role
|
v
AWS Secrets Manager
|
v
AWS KMS
|
v
Secure Credentials
|
v
Database / API
Services involved:
  • AWS Secrets Manager: Stores and retrieves credentials.
  • AWS KMS: Encrypts secrets.
  • AWS IAM: Controls access to secrets.
  • AWS Lambda: Supports automated credential rotation.

3. Implementation Using Python

First, create a secret in AWS Secrets Manager containing your database credentials.
Example:
{
"username": "app_user",
"password": "ExamplePassword123!",
"host": "mydb.example.com"
}
Install the AWS SDK:
pip install boto3
Retrieve the secret using Python:
import boto3
import json

client = boto3.client("secretsmanager")

response = client.get_secret_value(
SecretId="production/application/database"
)

credentials = json.loads(response["SecretString"])

print("Database:", credentials["host"])
print("Username:", credentials["username"])
The application retrieves credentials using its IAM role instead of storing passwords in source code.
Never print actual secret values in application logs.

4. Automatic Secret Rotation

Storing secrets securely is not enough. Credentials should also be rotated periodically.
AWS Secrets Manager supports automatic rotation using AWS Lambda.
The rotation process includes four stages:
  1. Create a new credential.
  1. Update the target database.
  1. Test the new credential.
  1. Activate the new version.
This reduces the risks associated with long-lived credentials.

5. Security Best Practices

  • Never hardcode passwords or API keys.
  • Apply least-privilege IAM permissions.
  • Enable automatic rotation where appropriate.
  • Use AWS KMS encryption.
  • Monitor secret access through CloudTrail.
  • Never expose credentials in application logs.

6. Conclusion

Application security starts with protecting sensitive information.
AWS Secrets Manager provides a centralized approach to storing, retrieving, and rotating application credentials.
By combining Secrets Manager, IAM, KMS, and Lambda, developers can build applications that are more secure and easier to maintain.
Remember: Your code should know how to access a secret, not contain the secret itself.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article