
book_create

import boto3
import json
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table("Books-WS78")
def lambda_handler(event, context):
try:
body = event.get("body")
# Handle both API Gateway (string) and test event (dict)
if isinstance(body, str):
book_item = json.loads(body)
elif isinstance(body, dict):
book_item = body
else:
raise ValueError("Invalid request body")
table.put_item(Item=book_item)
return {
"statusCode": 200,
"body": json.dumps({
"message": "Data successfully written to DynamoDB!"
}),
"headers": {
"Content-Type": "application/json"
}
}
except Exception as error:
print("Error:", str(error))
return {
"statusCode": 400,
"body": json.dumps({
"message": "Failed to write data to DynamoDB.",
"error": str(error)
}),
"headers": {
"Content-Type": "application/json"
}
}
Note: Change the DynamoDB table name in the code to match your DynamoDB table name.






Test_1{
"body": {
"id": "1",
"name": "Java Programming",
"author": "Alex Smith",
"category": "Technology",
"price": "10.89",
"description": "Hướng dẫn lập trình Java cơ bản",
"image": "https://book-image-resize-stores-tranvix.s3.ap-southeast-1.amazonaws.com/Notification.png"
}
}




