Skip to content

Instantly share code, notes, and snippets.

@monkut
Created November 25, 2019 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkut/c0d1827ec6792579815c84bbcc2c7d9e to your computer and use it in GitHub Desktop.
Save monkut/c0d1827ec6792579815c84bbcc2c7d9e to your computer and use it in GitHub Desktop.
import json
import boto3
secretsmgr_endpoint_url = 'http://localhost:4584'
SM_CLIENT = boto3.client('secretsmanager', endpoint_url=secretsmgr_endpoint_url)
# Set Secret
sample_secret_id = 'some:id:2'
secret_data = json.dumps({'key1': 'value1', 'key2': 'value2'})
SM_CLIENT.put_secret_value(
SecretId=sample_secret_id,
SecretString=secret_data
)
#{'ARN': 'arn:aws:secretsmanager:ap-northeast-1:1234567890:secret:some:id:2-ZAZus',
# 'Name': 'some:id:2',
# 'VersionId': 'c44b3daa-78e1-4ef2-bb73-7c8e3bd144f8',
# 'VersionStages': ['AWSCURRENT'],
# 'ResponseMetadata': {'HTTPStatusCode': 200,
# 'HTTPHeaders': {'server': 'amazon.com',
# 'content-type': 'text/html; charset=utf-8',
# 'content-length': '189',
# 'date': 'Mon, 25 Nov 2019 00:38:43 GMT'},
# 'RetryAttempts': 0}}
# Get Secret
response = SM_CLIENT.get_secret_value(
SecretId=sample_secret_id,
)
# response:
#{
# 'ARN': 'arn:aws:secretsmanager:ap-northeast-1:1234567890:secret:some:id:2-BQWnH',
# 'Name': 'some:id:2',
# 'VersionId': 'c44b3daa-78e1-4ef2-bb73-7c8e3bd144f8',
# 'SecretString': '{"key1": "value1", "key2": "value2"}',
# 'VersionStages': ['AWSCURRENT'],
# 'CreatedDate': datetime.datetime(2019, 11, 25, 9, 38, 43, tzinfo=tzlocal()),
# 'ResponseMetadata': {
# 'HTTPStatusCode': 200,
# 'HTTPHeaders': {'server': 'amazon.com', 'content-type': 'text/html; charset=utf-8', 'content-length': '280', 'date': 'Mon, 25 Nov 2019 00:40:37 GMT'},
# 'RetryAttempts': 0}
#}
decoded = json.loads(response['SecretString'])
# decoded:
# {'key1': 'value1', 'key2': 'value2'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment