Skip to content

Instantly share code, notes, and snippets.

@hirochachacha
Created February 8, 2019 08:22
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 hirochachacha/006f26c0d8a5a3a1299ae570c5e847b8 to your computer and use it in GitHub Desktop.
Save hirochachacha/006f26c0d8a5a3a1299ae570c5e847b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import argparse
import os
import boto3
client = boto3.client('ssm')
parser = argparse.ArgumentParser(
description='sync .env with aws ssm parameter store.')
parser.add_argument(
'command', metavar='command', type=str, help='pull, push or rm')
parser.add_argument('path', metavar='path', type=str, help='path')
def pull(path):
with open('.env', 'w') as f:
res = client.get_parameters_by_path(
Path=path,
WithDecryption=True,
)
for param in res['Parameters']:
r = client.get_parameter_history(Name=param['Name'])
p = r['Parameters'][0]
if p.get('Description'):
print(
f"{os.path.basename(param['Name'])}={param['Value']} # {p['Description']}",
file=f,
)
else:
print(
f"{os.path.basename(param['Name'])}={param['Value']}",
file=f,
)
def push(path):
delete(path)
with open('.env', 'r') as f:
for line in f:
keyvalue, comment = line.partition('#')[::2]
name, value = keyvalue.partition('=')[::2]
name, value, comment = name.strip(), value.strip(), comment.strip()
if name:
client.put_parameter(
Name=f'{path}/{name}',
Value=f'{value}',
Description=f'{comment}',
Type='SecureString',
# Overwrite=True,
)
def delete(path):
res = client.get_parameters_by_path(Path=path)
if res['Parameters']:
client.delete_parameters(
Names=[param['Name'] for param in res['Parameters']])
if __name__ == '__main__':
args = parser.parse_args()
if args.command == ['pull', 'get', 'fetch', 'download']:
pull(args.path)
if args.command == ['push', 'set', 'upload']:
push(args.path)
if args.command in ['rm', 'remove', 'delete', 'destroy']:
delete(args.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment