Skip to content

Instantly share code, notes, and snippets.

@osya
Last active June 2, 2024 04:00
Show Gist options
  • Save osya/f9625a0d3649db30b68e609fa8ad07de to your computer and use it in GitHub Desktop.
Save osya/f9625a0d3649db30b68e609fa8ad07de to your computer and use it in GitHub Desktop.
Python boilerplate for AWS Lambda #Python
from __future__ import print_function
import boto3
import json
print('Loading function')
def handler(event, context):
'''Provide an event that contains the following keys:
- operation: one of the operations in the operations dict below
- tableName: required for operations that interact with DynamoDB
- payload: a parameter to pass to the operation being performed
'''
#print("Received event: " + json.dumps(event, indent=2))
operation = event['operation']
if 'tableName' in event:
dynamo = boto3.resource('dynamodb').Table(event['tableName'])
operations = {
'create': lambda x: dynamo.put_item(**x),
'read': lambda x: dynamo.get_item(**x),
'update': lambda x: dynamo.update_item(**x),
'delete': lambda x: dynamo.delete_item(**x),
'list': lambda x: dynamo.scan(**x),
'echo': lambda x: x,
'ping': lambda x: 'pong'
}
if operation in operations:
return operations[operation](event.get('payload'))
else:
raise ValueError('Unrecognized operation "{}"'.format(operation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment