Package: django-aws-secrets-manager
Django AWS SecretsManagerDjango AWS SecretsManageris a package that helps you manage the secret values used by Django through AWS's SecretsManager service.RequirementsPython >= 3.6DjangoRequired settings for the settings moduleAWS_SECRETS_MANAGER_SECRETS_NAME(or AWS_SECRETS_NAME)Secret name of SecretsManager to useAWS_SECRETS_MANAGER_SECRETS_SECTION(or AWS_SECRETS_SECTION)The key that separates JSON objects by colons.ex) In the example below, the "production" item is represented as"sample-project:production".AWS_SECRETS_MANAGER_REGION_NAME(or AWS_REGION_NAME)Regionof the SecretsManager service to useex) ap-northeast-2Secret value setting of AWS SecretsManagerSecretsManager's Secret valueuses JSON format in Plaintext.Here is an example Secret value to use for configuration, and theSecret(Corresponds toAWS_SECRETS_MANAGER_SECRETS_NAMEin the settings module) is namedsample-project-secret{"sample-project(Recommend the name of django project)":{"base(If the settings module is a package, submodule names are recommended)":{"SECRET_KEY":"DjangoSecretKey"},"dev":{"AWS_S3_BUCKET_NAME":"sample-s3-dev"},"production":{"AWS_S3_BUCKET_NAME":"sample-s3-production"}}}Setting up AWS Credentials for Django to useDjango uses two methods to access the SecretsManager on AWS. The first uses a profile of~/.aws/credentialsin your home folder, and the second uses an environment variable.1. Using the AWS Credentials ProfileRecommended for use in development environmentsSet Profile of IAM User withSecretsManagerReadWrite Permissionto~/.aws/credentials. The following example uses the profile namesample-project-secretsmanager[sample-project-secretsmanager]aws_access_key_id=AKI*************aws_secret_access_key=Mlp********************Then enter the profile name inAWS_SECRETS_MANAGER_PROFILE(or AWS_PROFILE) of the settings module.# settings.pyAWS_SECRETS_MANAGER_PROFILE='sample-project-secrets-manager'2. Use environment variablesIt is recommended to use in distribution or CI / CD environment.If you set the following values in the environment variable, the contents are used to use the SecretsManager service.AWS_SECRETS_MANAGER_ACCESS_KEY_ID(or AWS_ACCESS_KEY_ID)AWS_SECRETS_MANAGER_SECRET_ACCESS_KEY(or AWS_SECRET_ACCESS_KEY)Using Secrets in Django's Settings ModuleFirst, import the SECRETS instance of the library.Enter the settings for Django AWS SecretsManagerUse SECRETS as a dictionary to get the secrets you wantFollow the form of the example belowBy separating the settings module into packages, it is assumed that there are base and dev submodules.settings/
    __init__.py
    base.py
    dev.py## settings/base.py# 1. Import the SECRETS instance of the libraryfromaws_secretsimportSECRETS# 2. Enter the settings for Django AWS SecretsManagerAWS_SECRETS_MANAGER_SECRETS_NAME='sample-project-secret'AWS_SECRETS_MANAGER_PROFILE='sample-project-secretsmanager'AWS_SECRETS_MANAGER_SECRETS_SECTION='sample-project:base'AWS_SECRETS_MANAGER_REGION_NAME='ap-northeast-2'# 3. Use SECRETS as a dictionary to get the secrets you wantSECRET_KEY=SECRETS['SECRET_KEY']SECRET_KEY=SECRETS.get('SECRET_KEY')## settings/dev.py# The SECRETS instance is already imported from the base module.from.baseimport*# Use a different secrets sectionAWS_SECRETS_MANAGER_SECRETS_SECTION='sample-project:dev'# Use SECRETS as a dictionary to get the secrets you wantAWS_STORAGE_BUCKET_NAME=SECRETS['AWS_STORAGE_BUCKET_NAME']AWS_STORAGE_BUCKET_NAME=SECRETS.get('AWS_STORAGE_BUCKET_NAME','default')ContributingAs an open source project, we welcome contributions.The code lives onGitHub