You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.4 KiB

  1. # import json
  2. import base64
  3. import boto3
  4. def lambda_handler(event, context):
  5. s3_client = boto3.client('s3')
  6. response = s3_client.get_object(Bucket='cloudcomp-counter', Key='us-east-1')
  7. counter = int(response['Body'].read().decode('utf-8'))
  8. debug = ""
  9. incr = 0
  10. if 'body' in event:
  11. body = str(base64.b64decode(event['body']).decode("utf-8"))
  12. if body.startswith('input'):
  13. incr = int(body.rsplit('=')[1])
  14. elif 'input' in event:
  15. incr = int(event['input'])
  16. if incr is not 0:
  17. counter = counter + incr
  18. response = s3_client.put_object(Bucket='cloudcomp-counter', Key='us-east-1', Body=str(counter))
  19. output = ('<html><head><title>TCPTimeCounter REST Service</title>\n'
  20. '<meta http-equiv="refresh" content="5"/></head><body>\n'
  21. '<h2>HS Fulda - TCPTimeCounter REST Service</h2>\n'
  22. '<p><b>HTML-Output:</b> ' + str(counter) + '</p></body>\n'
  23. '<form method=POST action="">\n'
  24. '<input type="hidden" name="input" value="1">\n'
  25. '<input type="submit" value="Increment"></form>\n'
  26. # '<hr><b>Lambda Event:</b><br>' + repr(event) + '\n'
  27. # '<hr><b>Lambda Context:</b><br>' + repr(context) + '\n'
  28. '</body></html>\n')
  29. return {
  30. 'statusCode': 200,
  31. 'headers': {
  32. 'Content-Type': 'text/html',
  33. },
  34. 'body': output
  35. }