Skip to content

Instantly share code, notes, and snippets.

@vishnuhd
Created April 18, 2020 10:10
Show Gist options
  • Save vishnuhd/670aabdcb60a8c22044241161d3f971f to your computer and use it in GitHub Desktop.
Save vishnuhd/670aabdcb60a8c22044241161d3f971f to your computer and use it in GitHub Desktop.
Python script to create encrypted secrets in AWS secret manager using KMS Key
import boto3
import os
import json
filename = 'data.json'
kms_id = ''
secretdata= {"username":"chris","password":"BnQw!XDWgaEeT9XGTT29"}
secret_data={}
for k,v in secretdata.items():
encrypt_string = os.popen(("aws kms encrypt --key-id %s --plaintext '%s' --query CiphertextBlob --output text" % (kms_id, v))).read()
secret_data[k] = encrypt_string
json_data = json.dumps(secret_data)
with open(filename,'w') as f:
f.write(json_data)
with open('data.json', 'r') as content_file:
secret_content = content_file.read()
print(secret_content)
client = boto3.client('secretsmanager')
response = client.create_secret(
Description='My test secret',
Name='MyTestt',
SecretString=secret_content,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment