How To Get All AWS Resources Via API

Services

28/11/2022


AWS provides an SDK called @aws-sdk/client-config-service, which allows you to query all AWS resources using an SQL command.

To start off, install the SDK with a package manager of your choice.

BASH
$ npm install @aws-sdk/client-config-service
$ yarn add @aws-sdk/client-config-service

Then initialize the Config Service Client class which takes several arguments, with your account credentials and region being the essential ones.

TYPESCRIPT
const configClient = new ConfigServiceClient({
region,
credentials,
})

The next, and most important step, is to initialize SelectResourceConfigCommand by passing an SQL SELECT query into the class' constructor.

TYPESCRIPT
const params: SelectResourceConfigCommandInput = {
Expression: "SELECT *", // Get ALL resources
}
const command = new SelectResourceConfigCommand(params)

In the AWS Config page under Advanced queries (located in the sidebar), you may play around with SELECT queries to figure out how to query the resources you need.

AWS Config page

Click the "New query" button to start a query

Last, but not least, send your query to AWS and receive a response with your requested data of type SelectResourceConfigCommandOutput.

TYPESCRIPT
const response = await client.send(command)

WRITTEN BY

Code and stuff