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)?