🔧 How to Retrieve All DynamoDB Tables using JavaScript SDK v3 (2023)
Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to
In an engineering team with matured devops practices, it is often required to know all the DynamoDBs running in various accounts. This list of tables could then be used for:
- Performing standard compliance checks on your table names and configuration.
- Build alarm systems that monitors list of tables in a production account to make sure that every single table you need exists.
- Creating backups and restoring data in DynamoDB tables.
- Managing and organizing your DynamoDB tables.
- Monitoring and optimizing performance of your DynamoDB tables.
- Automating and integrating your application with other AWS services that use DynamoDB tables as input or output.
- Auditing and tracking usage of your DynamoDB tables.
In this article, we'll be exploring how to retrieve all the DynamoDB table names using AWS SDK for JavaScript v3.
Before you start
I'd recommend using the latest version of NodeJS supported by Lambda Functions. It's also helpful to have nvm in your machine to easily switch between NodeJS versions.
# Install Node 18 to use the latest code-base
nvm install 18
node --version
# Switch to NodeJS 18
nvm use 18
You will have to install the JavaScript AWS SDKs v3 for DynamoDB. If you are using NPM, use the following commands to install the SDKs.
# If you use NPM
npm install @aws-sdk/client-dynamodb
# Install only if you want to use
# named profiles inside your dev machine
npm install @aws-sdk/credential-providers
If you need to use yarn:
# If you use yarn
yarn add @aws-sdk/client-dynamodb
# Install only if you want to use inside your dev machine
yarn add @aws-sdk/credential-providers
The Source Code
By design, ListTablesCommand can only return a maximum of 100 DynamoDB names (see the code below).
import { DynamoDBClient, ListTablesCommand } from "@aws-sdk/client-dynamodb";
import { fromIni } from "@aws-sdk/credential-providers";
const client = new DynamoDBClient({ region: "ap-southeast-1" });
// Use this code if you need named profiles
// const client = new DynamoDBClient({
// credentials: fromIni({ profile: "my-poc-profile" }),
// region: "ap-southeast-1",
// });
const command = new ListTablesCommand({});
const response = await client.send(command);
console.log(response);
Which would result to:
{
'$metadata': {
httpStatusCode: 200,
requestId: 'LKJEWJOI3290923902302310219',
extendedRequestId: undefined,
cfId: undefined,
attempts: 1,
totalRetryDelay: 0
},
LastEvaluatedTableName: undefined,
TableNames: [
'dynamo-table-a',
'dynamo-table-b',
'dynamo-table-c'
]
}
The page limitation of 100 items per response is probably placed by AWS to gracefully handle accounts with large number of DynamoDB tables in them.
If you really want to retrieve all the tables in an account. You can use the following code instead:
import { DynamoDBClient, ListTablesCommand } from "@aws-sdk/client-dynamodb";
import { fromIni } from "@aws-sdk/credential-providers";
const client = new DynamoDBClient({
credentials: fromIni({ profile: "my-poc-profile" }),
region: "ap-southeast-1",
});
let startTableName = "";
let hasResults = false;
let tableNames = [];
do {
hasResults = false;
const searchInput = {
Limit: 100,
};
if (startTableName) {
searchInput.ExclusiveStartTableName = startTableName;
}
const command = new ListTablesCommand(searchInput);
const response = await client.send(command);
if (response.TableNames && response.TableNames.length > 0) {
startTableName = response.TableNames[response.TableNames.length - 1];
tableNames = [...tableNames, ...response.TableNames];
hasResults = true;
}
} while (hasResults);
console.log(tableNames);
Which would result to:
[ 'dynamo-table-a', 'dynamo-table-b', 'dynamo-table-c' ]
🔧 DynamoDB Basic - Part 1: Introduction DynamoDB
📈 34.83 Punkte
🔧 Programmierung
🔧 using slack api to retrieve data
📈 24.02 Punkte
🔧 Programmierung
🔧 Using JDBC to Retrieve Cursor Data in GBase 8c
📈 24.02 Punkte
🔧 Programmierung