Created
April 22, 2023 03:11
-
-
Save ymulenll/67d7ae1ffb768d5d3deda1747dc4d4c8 to your computer and use it in GitHub Desktop.
lambda put item in dynamodb using nodejs
This file contains hidden or 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
| import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | |
| import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; | |
| import { randomUUID } from "crypto"; | |
| const ddbDocClient = DynamoDBDocumentClient.from(new DynamoDBClient({})); | |
| export const handler = async (event, context) => { | |
| try { | |
| const book = JSON.parse(event.body); | |
| const newBook = { | |
| ...book, | |
| id: randomUUID(), | |
| }; | |
| await ddbDocClient.send(new PutCommand({ | |
| TableName: "books", | |
| Item: newBook, | |
| })); | |
| return { | |
| statusCode: 201, | |
| body: JSON.stringify(newBook), | |
| }; | |
| } | |
| catch (error) { | |
| console.error(error); | |
| return { | |
| statusCode: 500, | |
| body: JSON.stringify({ message: error.message }), | |
| }; | |
| } | |
| }; |
Muchas gracias por el código :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gracias, muy util para aprender, necesitaba implementar una aplicacion lambda lo mas pronto posible sin saber nada