Skip to content

Instantly share code, notes, and snippets.

@ymulenll
Created April 22, 2023 03:11
Show Gist options
  • Select an option

  • Save ymulenll/67d7ae1ffb768d5d3deda1747dc4d4c8 to your computer and use it in GitHub Desktop.

Select an option

Save ymulenll/67d7ae1ffb768d5d3deda1747dc4d4c8 to your computer and use it in GitHub Desktop.
lambda put item in dynamodb using nodejs
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 }),
};
}
};
@luisbrice-dev
Copy link

gracias, muy util para aprender, necesitaba implementar una aplicacion lambda lo mas pronto posible sin saber nada

@Daniee19
Copy link

Muchas gracias por el código :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment