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




Picture showing the dynamodb table created in aws



Create A Lambda Function




Picture showing the lambda function created in aws



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




Picture showing the listTables function executed using the lambda function



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;
    }
};


Picture showing the describeTable function executed using the lambda function





Posted By  -  Karan Gupta
 
Posted On  -  Friday, December 8, 2023

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250