|
| 1 | +import argparse |
| 2 | + |
| 3 | +import boto3 |
| 4 | + |
| 5 | + |
| 6 | +def _get_params(client, path, next_token, debug): |
| 7 | + params = [] |
| 8 | + p = dict( |
| 9 | + Path=path, |
| 10 | + Recursive=True, |
| 11 | + ParameterFilters=[], |
| 12 | + WithDecryption=not debug |
| 13 | + ) |
| 14 | + if next_token: |
| 15 | + p['NextToken'] = next_token |
| 16 | + response = client.get_parameters_by_path(**p) |
| 17 | + next_token = response.get('NextToken') |
| 18 | + params.extend(response['Parameters']) |
| 19 | + if next_token: |
| 20 | + params.extend(_get_params(client, path, next_token, debug)) |
| 21 | + return params |
| 22 | + |
| 23 | + |
| 24 | +def run(env, app_name, debug=False): |
| 25 | + all_path = [ |
| 26 | + '/common/', |
| 27 | + '/{env}/common/'.format(env=env), |
| 28 | + '/{env}/{app_name}/'.format(env=env, app_name=app_name)] |
| 29 | + |
| 30 | + # client = boto3.client('ssm') |
| 31 | + client = boto3.client('ssm', region_name='us-east-1') |
| 32 | + |
| 33 | + for path in all_path: |
| 34 | + params = _get_params(client, path, None, debug) |
| 35 | + |
| 36 | + for param in params: |
| 37 | + env_var = param['Name'].split(path)[1] |
| 38 | + if debug: |
| 39 | + if param['Type'] == 'SecureString': |
| 40 | + param['Value'] = '****' |
| 41 | + print(param) |
| 42 | + else: |
| 43 | + print("export {}='{}'".format(env_var, param['Value'])) |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + parser = argparse.ArgumentParser(description='Print varialbes from EC2 Parameter Store') |
| 48 | + parser.add_argument('-d', '--debug', required=False, action='store_true', |
| 49 | + help='Debug variables if True or print export commands otherwise') |
| 50 | + parser.add_argument('-e', '--env', required=True, |
| 51 | + help='Environment') |
| 52 | + parser.add_argument('-a', '--app', required=True, |
| 53 | + help='Application name') |
| 54 | + |
| 55 | + args = parser.parse_args() |
| 56 | + run(args.env, args.app, args.debug) |
0 commit comments