Skip to content

Instantly share code, notes, and snippets.

@johnaxel
Forked from vagelim/example-lambda.py
Last active November 18, 2019 14:57
Show Gist options
  • Save johnaxel/e01ed73cdaaf6846f204ec4d650aa397 to your computer and use it in GitHub Desktop.
Save johnaxel/e01ed73cdaaf6846f204ec4d650aa397 to your computer and use it in GitHub Desktop.
Example Lambda to illustrate concepts in lambda blog post
import hashlib
import boto3
import time
## CONFIG ##
BUCKET = 'bucket-name'
FILEPATH = 'hashes'
SEP = '\t'
def log(metric_name, metric_type='count', metric_value=1, tags=[]):
# format:
# MONITORING|unix_epoch_timestamp|metric_value|metric_type|my.metric.name|#tag1:value,tag2
print("MONITORING|{}|{}|{}|{}|#{}".format(
int(time.time()), metric_value, metric_type, 'hasher.lambda.' + metric_name, ','.join(tags)
))
def response(statusCode, body):
log(metric_name='responses', tags=['hash-service', 'status:' + statusCode])
return {
'statusCode': statusCode,
'body': body,
'headers': {
'Content-Type': 'application/json'
},
}
def read_s3(bucket=BUCKET, path=FILEPATH):
client = boto3.client('s3')
resp = client.get_object(Bucket=bucket, Key=path)
resp = resp['Body'].read()
log(metric_name='db_reads', tags=['hash-service'])
return resp
def hash_exists(hash, address=False):
function_start = time.time()
resp = read_s3()
resp = resp.split('\n')
r_value = False
# Obviously not an ideal solution as hash db grows
for each in resp:
if each.split(SEP)[0] == hash:
log(metric_name='hashes_found', tags=['hash-service'])
# True if we want to return the hash
if address:
r_value = each.split(SEP)[0]
else:
# Otherwise return the original (unhashed) string
r_value = each.split(SEP)[1]
break
function_duration = time.time() - function_start
log(metric_name='hash_exists.latency', metric_type='histogram',
metric_value=function_duration, tags=['hash-service'])
return r_value
def lambda_handler(event, context):
log(metric_name='requests', tags=['hash-service'])
if 'hash' in event:
hash = event['hash']
# Check if we saw it before
exists = hash_exists(hash)
if exists:
return response('200', exists)
else:
return response('404', "hash does not exist in db")
elif 'string' in event:
message = event['string']
# log distribution of payload sizes
payload_size = len(message)
log(metric_name='payload_size', metric_type='histogram', metric_value=payload_size, tags=['hash-service'])
# Convert the given string to its unique hash
hash = hashlib.sha256(message).hexdigest()
log(metric_name='strings_received', tags=['hash-service'])
# address = true because we want to return the matched hash
exists = hash_exists(hash, address=True)
if exists:
# Search through the hashes to find it
return response('200', exists)
else:
data = read_s3()
data = "{}\n{}{}{}".format(data, hash, SEP, message)
client = boto3.client('s3')
client.put_object(Bucket=BUCKET, Key=FILEPATH, Body=data)
return response('200', hash)
else:
return response('400', "Nothing to see here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment