python_changetxt.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # lambda_code
  2. import json
  3. import boto3
  4. def lambda_handler(event,context):
  5. client = boto3.client('ssm')
  6. try:
  7. msg =json.loads(event['body'])
  8. sg = msg['secret_code']
  9. print("secret " + sg)
  10. client.put_parameter(Name="Secret",Value=sg,Type='String',Overwrite=True)
  11. except:
  12. print("NO")
  13. return {
  14. "statusCode": 200,
  15. "headers": {
  16. "Content-Type": "application/json",
  17. },
  18. "body": json.dumps(event, indent=4),
  19. }
  20. # -----------------------------
  21. import boto3
  22. client = boto3.client('ssm')
  23. respond = client.get_parameter(Name='Secret')
  24. code_ori = respond['Parameter']['Value']
  25. def alter(file,old_str,new_str):
  26. file_data = ""
  27. with open(file, "r", encoding="utf-8") as f:
  28. for line in f:
  29. if old_str in line:
  30. line = line.replace(line,new_str)
  31. file_data += line
  32. with open(file,"w",encoding="utf-8") as f:
  33. f.write(file_data)
  34. f.close()
  35. code_pro = code_ori.replace("'", '')
  36. code = f'secret_code = "{code_pro}"'
  37. alter("/conf.toml", "secret_code", cod