Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created August 14, 2019 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brydavis/11078240cdc231b24c53a00739b70c5b to your computer and use it in GitHub Desktop.
Save brydavis/11078240cdc231b24c53a00739b70c5b to your computer and use it in GitHub Desktop.
import json
import boto3
from boto3.dynamodb.conditions import Key
session = boto3.Session()
dynamodb = session.resource('dynamodb', region_name='us-west-2')
def lambda_handler(event, context):
# {
# "product_id": "P000041"
# }
product_id = json.loads(event["body"])["product_id"]
rental = dynamodb.Table("rental").scan(
FilterExpression=Key('product_id').eq(product_id)
)
product = dynamodb.Table("product").get_item(
Key={
'product_id': product_id
}
)
customers = []
for user in rental["Items"]:
customer = dynamodb.Table("customer").get_item(
Key={
"Id": user["user_id"]
}
)
customers.append(customer["Item"])
return {
'statusCode': 200,
'body': json.dumps({
"rental_info": rental["Items"][0],
"product_info": product["Item"],
"customer_info": customers,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment