Created
January 28, 2019 17:19
-
-
Save adhorn/100d45f8206d5efa0f002b396b5a0c98 to your computer and use it in GitHub Desktop.
lambda function used to demo ALB to Lambda integration
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
from __future__ import unicode_literals | |
import sys | |
import logging | |
import boto3 | |
import os | |
log = logging.getLogger() | |
log.setLevel(logging.DEBUG) | |
sys.path.insert(0, './vendor') | |
import simplejson as json | |
# simplejson can handle Decimal object while the \ | |
# standard json package can't. | |
region = os.environ["AWS_REGION"] | |
tablename = os.environ["tablename"] | |
dynamodb = boto3.resource('dynamodb', region_name=region) | |
table = dynamodb.Table(tablename) | |
def makehealthcheckcalls(): | |
log.debug("status: {}".format(os.environ["STATUS"])) | |
return int(os.environ["STATUS"]) | |
def get_from_dynamo(event): | |
log.debug("Received in get_from_dynamo: {}".format(json.dumps(event))) | |
item_id = event | |
log.debug("item_id: {}".format(item_id)) | |
item = table.get_item( | |
Key={ | |
'item_id': item_id, | |
} | |
) | |
return item['Item']['item_id'] | |
def get_item(event, context): | |
log.debug("Received event in get_item: {}".format(json.dumps(event))) | |
basepath = event["path"].split('/') | |
print(basepath) | |
if basepath[1] == "get": | |
body = { | |
"item_id": get_from_dynamo(basepath[2]), | |
} | |
response = { | |
"statusCode": 200, | |
"isBase64Encoded": False, | |
"headers": { | |
"Content-Type": "text/html; charset=utf-8" | |
}, | |
"body": "<html><body><h1>Returned {0} from {1}</h1></body></html>".format( | |
json.dumps(body), region | |
) | |
} | |
elif basepath[1] == "health": | |
response = { | |
"statusCode": makehealthcheckcalls(), | |
"isBase64Encoded": False, | |
"headers": { | |
"Content-Type": "text/html; charset=utf-8" | |
}, | |
"body": "<html><body><h1>health: {0} {1} </h1></body></html>".format(makehealthcheckcalls(), region) | |
} | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment