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.

52 lines
1.7 KiB

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