This Python script provides a utility to synchronize environment variables between a local .env file and AWS Secrets Manager. It supports two modes of operation:
PULL: Retrieves the latest variables from AWS Secrets Manager and updates the local.envfile.SYNC: Identifies local environment variables that are not present in AWS Secrets Manager and updates the AWS secret with these new variables.
The script also has the option to alphabetize the .env file, making it easier to manage and review.
- Python 3.x installed on your system.
- Boto3 library installed (
pip install boto3). python-dotenvlibrary installed (pip install python-dotenv).- AWS CLI installed and configured with the necessary permissions to read and write to AWS Secrets Manager.
- An existing
.envfile in the correct format (KEY=VALUEwithout quotes, unless necessary).
Before running the script, configure the following variables at the top of the script:
env_file: The filename of your local.envfile. The default is set to.env.dev.sync_mode: The mode of operation. Set to'PULL'to pull variables from AWS or'SYNC'to sync local variables to AWS.alphabetize_env: Set toTrueto alphabetize the.envfile, orFalseto leave it as is.
-
Ensure your
.envfile is correctly named and placed in the project root directory, or update theenv_filevariable with the correct relative path. -
Set the desired
sync_modeandalphabetize_envin the script. -
Define the
LOCAL_VAR_SECRET_ARNvariable in your.envfile with the ARN of your AWS secret:LOCAL_VAR_SECRET_ARN=arn:aws:secretsmanager:us-east-1:123456789012:secret:mySecret -
Run the script with Python:
python path/to/script.py
The script will either pull the latest variables from AWS Secrets Manager or sync local variables to AWS based on the sync_mode you've set.
The script will print messages indicating the actions being taken and their results, such as whether the .env file was updated or if new local variables were added to AWS Secrets Manager.
This script modifies sensitive data either in AWS Secrets Manager or the local .env file. It is recommended to back up your AWS secrets and .env files before running this script to avoid accidental data loss. Ensure that your AWS credentials are secured and have the appropriate permissions to access AWS Secrets Manager.
This script is provided "as is", without warranty of any kind. Use it at your own risk.