0

I am writing a python script to create/update/delete values in AWS SSM parameter store.

My current logic is:

def lambda_handler(event, context):
    logger.info(event)#removing tab
    files = get_local_files() #set of all application.properties files
    count = 0
    for file in files: #iterate in each application.properties file
        remote_key_vals = get_ssm_paramstore_values(file) #remote_key_vals is a set of keys from ssm
        local_key_vals = read_ssm_local_file(file) #local_key_vals is a set of keys from local file
        if(len(remote_key_vals)==0):
            create_ssm_paramstore(file)
            count=count+1
        elif(local_key_vals>remote_key_vals):
            diff = local_key_vals-remote_key_vals
            if(len(diff)>0):
                update_ssm_paramstore(file, diff)
                count=count+1
        elif(remote_key_vals>local_key_vals):
            diff=remote_key_vals - local_key_vals
            if(len(diff)>0):
                delete_ssm_paramstore(file, diff)
                count = count+1 
        else:
            logger.info(file+' already exist')
        logger.info(local_key_vals)
    try:
        req = requests.put(event['ResponseURL'],
                          data=getResponse(event, context,
                                            responseStatus))
        if req.status_code != 200:
            logger.info(req.text)
            raise Exception('Received non 200 response while sending response to CFN.'
                            )
    except requests.exceptions.RequestException, e:
        logger.info(e)
        raise
    return

This code works for 3 conditions only:

  • When the SSM parameter store is empty, the local application.properties file will get created/loaded if the length of the remote_key_vals set is 0 (as in no ssm parameter exists).
  • When there is an extra value in local_key_vals than remote_key_vals. I've calculated the difference of the two sets and updated the difference in parameter store (application.properties file)
  • Delete a value from SSM parameter store if local application.properties file doesn't have that key/value anymore.

I need to handle a condition wherein the size of both sets is the same, a key in local application.properties file has an updated name and remote ssm needs to be updated with this name.

E.g.: Key set for local student/application.properties: set(['APPLICATION_NAME_TEST', 'VERSION', 'S3_BUCKET'])

Key set for SSM parameter store (student/application.properties): set(['APPLICATION_NAME', 'VERSION', 'S3_BUCKET'])

How can I update APPLICATION_NAME key in SSM parameter store to APPLICATION_NAME_TEST key(present in local file)?

nehacharya
  • 925
  • 1
  • 11
  • 31
  • If the dataset is small, and you always want to 'push' local to remote, you might be better off just always doing an overwrite and throwing away all the comparison logic? Also a `set` has no order, and your example shows a set containing a single list - which is it? – match Feb 17 '20 at 09:43
  • The issue is that an overwrite will wipe out any values assigned to keys and these values will have to be manually added in SSM. So to avoid adding values manually for the keys after each deployment, comparison logic is written. It's a set containing a single list and order doesn't matter – nehacharya Feb 17 '20 at 09:58

1 Answers1

0

This solution worked for me.

local_key_set = set(['APPLICATION_NAME_TEST', 'VERSION']);
remote_key_set= set(['APPLICATION_NAME', 'VERSION']);

local_key_list = list(local_key_set);
remote_key_list= list(local_key_set);

for i in range(0,len(local_key_list)):
    if local_key_list[i] != remote_key_list[i]:
        remote_key_list[i] = local_key_list[i]

remote_key_set = set(remote_key_list);
nehacharya
  • 925
  • 1
  • 11
  • 31