Created
October 15, 2020 02:06
-
-
Save yeisoncruz16/d2d287192da81cf9f36c43c15946e024 to your computer and use it in GitHub Desktop.
Simple Python lambda function to store & list information from DynamoDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
from datetime import datetime | |
def lambda_handler(event, context): | |
dynamodb = boto3.resource('dynamodb') | |
client = boto3.client('dynamodb') | |
queryStringParameters = event.get("queryStringParameters", {}) | |
list_items = queryStringParameters.get("list_items", "false") | |
tableTemperature = dynamodb.Table('my-dynamo-table') | |
if list_items == 'true': | |
response = tableTemperature.scan() | |
print(response['Items']) | |
return { | |
'statusCode': 200, | |
'body': json.dumps(response['Items']) | |
} | |
else: | |
params = json.loads(event.get('body')) | |
eventDateTime = (datetime.now()).strftime("%Y-%m-%d %H:%M:%S") | |
my_field = params['my_field'] | |
try: | |
tableTemperature.put_item( | |
Item={ | |
'date': eventDateTime, | |
'status': 'pending', | |
'my_field' : my_field | |
} | |
) | |
return { | |
'statusCode': 200, | |
'body': json.dumps('Succesfully inserted!') | |
} | |
except: | |
print('Closing lambda function') | |
return { | |
'statusCode': 400, | |
'body': json.dumps('Error saving') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment