Skip to content

Instantly share code, notes, and snippets.

@peacing
Last active July 28, 2020 00:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save peacing/c0b05a6bce181eaf772ef235225c99fe to your computer and use it in GitHub Desktop.
example lambda function
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