Last active
July 28, 2020 00:50
-
-
Save peacing/c0b05a6bce181eaf772ef235225c99fe to your computer and use it in GitHub Desktop.
example lambda function
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 | |
import base64 | |
from datetime import datetime | |
import requests | |
def lambda_handler(event, context): | |
for record in event['Records']: | |
decoded_data = json.loads(base64.b64decode(record['kinesis']['data'])) | |
user_id = decoded_data['user_id'] | |
amount = decoded_data['amount'] | |
dynamo_client = boto3.client('dynamodb') | |
put_response = dynamo_client.put_item( | |
TableName='user_spending', | |
Item={ | |
'user_id': {'S': user_id}, | |
'amount': {'N': f'{amount}'}, | |
'updated_at': {'S': datetime.utcnow().isoformat()} | |
} | |
) | |
if put_response['ResponseMetadata']['HTTPStatusCode'] == 200: | |
message = {'success': True, 'msg': f'success processing event: user_id: {user_id} amount: {amount}'} | |
else: | |
raise RuntimeError(f'failure processing event: user_id: {user_id} amount: {amount}') | |
return message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment