Articles → AWS → Listtables And Describetable Operations In Dynamodb Using The AWS Lambda Function
Listtables And Describetable Operations In Dynamodb Using The AWS Lambda Function
Dynamodb Table Creation
Create A Lambda Function
Install AWS-SDK
Code To List Tables
// index.mjs
import AWS from 'aws-sdk';
export const handler = async (event) => {
// TODO implement
AWS.config.update({ region: 'us-east-2' });
const dynamodb = new AWS.DynamoDB();
try {
const data = await dynamodb.listTables().promise();
const tables = data.TableNames || [];
const tablesString = JSON.stringify(tables);
const response = {
statusCode: 200,
body: JSON.stringify({
message: tablesString,
tables: tablesString,
}),
};
return response;
} catch (error) {
console.error('Error:', error);
console.error('Stack Trace:', error.stack);
const response = {
statusCode: 500,
body: JSON.stringify({
error: 'Internal Server Error',
errorMessage: error.message, // Include error message in the response
}),
};
return response;
}
};
Upload The Code To The Lambda Function
Permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:ListTables",
"dynamodb:DescribeTable"
],
"Resource": "*"
}
]
}
Output
Describe Table
// index.mjs
import AWS from 'aws-sdk';
export const handler = async (event) => {
// TODO implement
AWS.config.update({ region: 'us-east-2' });
const dynamodb = new AWS.DynamoDB();
const tableName = 'Employee'; // Replace with your actual DynamoDB table name
try {
const tableDescription = await dynamodb.describeTable({ TableName: tableName }).promise();
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Table description:',
tableDescription: tableDescription,
}),
};
return response;
} catch (error) {
console.error('Error:', error);
console.error('Stack Trace:', error.stack);
const response = {
statusCode: 500,
body: JSON.stringify({
error: 'Internal Server Error',
errorMessage: error.message, // Include error message in the response
}),
};
return response;
}
};