Spaces:
Configuration error
Configuration error
// Create clients and set shared const values outside of the handler. | |
// Create a DocumentClient that represents the query to add an item | |
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; | |
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb'; | |
const client = new DynamoDBClient({}); | |
const ddbDocClient = DynamoDBDocumentClient.from(client); | |
// Get the DynamoDB table name from environment variables | |
const tableName = process.env.SAMPLE_TABLE; | |
/** | |
* A simple example includes a HTTP get method to get one item by id from a DynamoDB table. | |
*/ | |
export const getByIdHandler = async (event) => { | |
if (event.httpMethod !== 'GET') { | |
throw new Error(`getMethod only accept GET method, you tried: ${event.httpMethod}`); | |
} | |
// All log statements are written to CloudWatch | |
console.info('received:', event); | |
// Get id from pathParameters from APIGateway because of `/{id}` at template.yaml | |
const id = event.pathParameters.id; | |
// Get the item from the table | |
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property | |
var params = { | |
TableName : tableName, | |
Key: { id: id }, | |
}; | |
try { | |
const data = await ddbDocClient.send(new GetCommand(params)); | |
var item = data.Item; | |
} catch (err) { | |
console.log("Error", err); | |
} | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify(item) | |
}; | |
// All log statements are written to CloudWatch | |
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`); | |
return response; | |
} | |