Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import boto3
  4. import json
  5.  
  6. print('Loading function')
  7. dynamo = boto3.client('dynamodb')
  8.  
  9.  
  10. def respond(err, res=None):
  11. return {
  12. 'statusCode': '400' if err else '200',
  13. 'body': err.message if err else json.dumps(res),
  14. 'headers': {
  15. 'Content-Type': 'application/json',
  16. },
  17. }
  18.  
  19.  
  20. def lambda_handler(event, context):
  21. '''Demonstrates a simple HTTP endpoint using API Gateway. You have full
  22. access to the request and response payload, including headers and
  23. status code.
  24.  
  25. To scan a DynamoDB table, make a GET request with the TableName as a
  26. query string parameter. To put, update, or delete an item, make a POST,
  27. PUT, or DELETE request respectively, passing in the payload to the
  28. DynamoDB API as a JSON body.
  29. '''
  30. #print("Received event: " + json.dumps(event, indent=2))
  31.  
  32. operations = {
  33. 'DELETE': lambda dynamo, x: dynamo.delete_item(**x),
  34. 'GET': lambda dynamo, x: dynamo.scan(**x),
  35. 'POST': lambda dynamo, x: dynamo.put_item(**x),
  36. 'PUT': lambda dynamo, x: dynamo.update_item(**x),
  37. }
  38.  
  39. operation = event['httpMethod']
  40. if operation in operations:
  41. payload = event['queryStringParameters'] if operation == 'GET' else json.loads(event['body'])
  42. return respond(None, operations[operation](dynamo, payload))
  43. else:
  44. return respond(ValueError('Unsupported method "{}"'.format(operation)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement