Building an EC2 Cloud Inventory Across All Regions and Accounts

EC2 instances often account for the largest portion of your AWS bill. Yet, it’s notoriously difficult to get a simple list of all EC2 instances across all regions and accounts, as threads on StackOverflow and

List EC2 instances from all accounts in all regions
by
u/kilmanjaro in
aws

show.

You also then want to use that list to ask questions about your inventory, such as:

How many total instances are there?

Which instances are running?

Which instances are missing tags?

Which resources have an expiration date?

In this post, I’ll describe how to use Resoto to build an EC2 cloud asset inventory. The baseline inventory is a list with all EC2 instances, which you then can use to create more narrow and detailed views.

Getting Started with Resoto​

Install Resoto.

Start Resoto Shell.

Configure Resoto to collect AWS resources and trigger resource collection. During each “collect run,” Resoto extracts and transforms data from cloud APIs, and stores snapshots of your infrastructure in its graph database.

Creating Your First EC2 Inventory​

Once Resoto has completed collecting your AWS resources, run the following search, which I’ll call our “baseline inventory”:

search is(aws_ec2_instance) | list --markdown id,name,instance_type,ctime as create_time, age,/ancestors.region.reported.name as region,/ancestors.account.reported.name as account,instance_status,tags.owner,tags.expiration

The above command returns a list of EC2 instances formatted as a Markdown table:

And voilà, you have your EC2 asset inventory, listing all instances across your regions and accounts.

Re-running the search will produce identical results unless Resoto has taken a new snapshot and your inventory has changed.

Export Your EC2 Inventory in CSV Format​

Let’s export this list to a CSV file so that we can import the data into Excel or Google Sheets:

search is(aws_ec2_instance) | list --csv id,name,instance_type,ctime as create_time, age,/ancestors.region.reported.name as region,/ancestors.account.reported.name as account,instance_status,tags.owner,tags.expiration | write ec2_inventory-@TODAY@.csv

The --csv flag for the list command tells Resoto to output the results in CSV format.

The write command sends the CSV output to the designated file.

The pipes (|) passes the result of the preceeding command as input for the following command.

@TODAY@ is a special placeholder string that we use to add the current date to the filename.

Executing the above command writes your EC2 inventory to a file on your computer.

Customizing Your EC2 Inventory​

Now that you have a baseline inventory, let’s customize it to include custom resource properties.

The search we used up until this point is:

search is(aws_ec2_instance) | list

This returns a list of EC2 instances with properties from a predefined list.

To add specific properties of interest, we can specify them as a parameter to the list command.

Available Resource Properties in Resoto​

The available properties of a resource kind are defined in the resource data model. In our case, the relevant resource kind is aws_ec2_instance.

You can list the available properties of aws_ec2_instance in your terminal with:

kind aws_ec2_instance

Including Custom Resource Properties in Your EC2 Inventory​

To keep it simple, we’ll building the inventory with ten properties:

ID

Name

Instance type

Creation time

Age

Region

Account

Instance status

Owner tag

Expiration tag

These properties provide a good overview, and include instance identifiers, how long ago instances were created, whether or not instances are currently running, and if so, where (region, account).

A best practice and policy we recommend is to tag all resources with an owner and expiration. By including these two properties into the search, the inventory clearly shows if developers are following that policy.

The full search syntax to build my inventory (for now, without formatting and writing the results to a file) reads as:

search is(aws_ec2_instance) | list id,name,instance_type,ctime as create_time, age,/ancestors.region.reported.name as region,/ancestors.account.reported.name as account,instance_status,tags.owner,tags.expiration

You can see how there are two different ways to fetch the properties.

Directly from the resource: ID, name, instance type, create time, age, and instance status are direct properties of the aws_ec2_instance kind.

Indirectly from a related resource: Region name and account name belong to the kinds aws_region and aws_account.

Capturing Indirect Properties with Graph Traversal​

After each complete data extraction, Resoto transforms the data extracted from the cloud APIs into a directed graph. Each resource becomes a node in this graph.

Each resource node in the graph has “ancestors” and “descendants,” depending on their location in the hierarchy relative to related resources.

Ancestors are resources that are higher up in the graph. The immediate ancestors of a resource are called “predecessors.”

If you think about the hierarchy of an EC2 instance, each instance is part of an account and a region. These are ancestors.

Descendants resources are resources that are further down in the graph. The immediate descendants of a resource are called “successors.”

Every EC2 instance will also have additional relationships to other resources, like EBS volumes or ENI network interfaces. Such resources are descendants.

Graph traversal allows me to “walk” the graph and select properties from related resources.

For our baseline inventory, we’re only interested in the region and account our resources run in. Both are higher up in the graph. We can retrieve the two properties by adding /ancestors.region.reported.name as region and /ancestors.account.reported.name as account to our search.

The search will traverse up the graph and search for nodes of kind region and account, and display the reported name with labels region and account. The benefit of this syntax is that we don’t have to specify exactly where in the graph that information is available—just that it is available somewhere.

Formatting and Exporting the EC2 Inventory​

We’ve already seen that Resoto offers two ways to format the results of a search, Markdown and CSV.

Markdown provides a nice visual in the CLI, whereas CSV format allows for further analysis of data in a spreadsheet or cloud warehouse.

Adding back in the formatting and writing to a file, the full command to build the inventory now reads as:

search is(aws_ec2_instance) | list --csv id,name,instance_type,ctime as create_time, age,/ancestors.region.reported.name as region,/ancestors.account.reported.name as account,instance_status,tags.owner,tags.expiration | write ec2_inventory-@TODAY@.csv

I can now take the resulting CSV file and upload it to Google Sheets for further analysis:

Exporting the inventory to a spreadsheet makes the data available to a wider audience, without the need for specific AWS skills like using the AWS Console.

Building on Your Baseline EC2 Inventory​

With a baseline inventory in place, you can now narrow down or expand the search to include properties like instance prices or aggregate data by instance type.

We’re always curious to hear what you’re building and how we can help. Join us on Discord and let us know!

Can’t you just use AWS Config aggregator?

Yes? It’s not very hard and I haven’t had to work on it in years, which counts for something even though it’s definitely less capable.

Yeah, it works fine. Advanced queries also give you most of anything you need. If you have unique requirements you can create Athena tables for the Config data and query it that way.

Cloud query is really nice for this

I had the founder on our (video) podcast → https://some.engineering/podcasts/2023/03/03/elt-for-cloud-infrastructure-data

The difference with Resoto is that we have both the analytics and the governance / remediation layer. You take the data from the inventory, and pipe it into a command.

The concept here is that we want to write code that uses infrastructure data as an input.

This use-case is totally possible with native AWS features with Config advanced query. We’ve around 300 accounts and it works just fine.

yes, AWS Config is of course a native solution and works just fine.

It’s an option for people who are looking for an open source alternative, and maybe have use cases that the native solution doesn’t address today.

A few points where we think we’re different:

- the inventory is “only” the first step, we also look to include other data points - from your cloud bill, for example.

- remediation / updates - use the data as an input to perform automatic updates with code

- if you do run multi-cloud infrastructure, then the same approach to building an inventory works across your clouds.

If you’re fully happy with “just” the data that AWS Config provides, then it would be hard to make a case to invest time into installing Resoto.

But if you’re looking to enrich your inventory with more data, and write code that reacts to changes in your infrastructure - then I think you might be delighted if you give it a spin.

[edited for more detail]

Yes, you can 100% use Config aggregator for what’s shown in the blog post! Disclaimer: I’m the original author of Resoto, so likely biased.

Resoto shines when dealing with more complex inventory requirements. For example, it’s quite simple to incorporate pricing data, which I was missing in Config.

It also enables you to perform intricate listings across resources such as Route53 records, ALBs/ELBs, storage volumes, etc. Plus allows for integration with any Kubernetes clusters in your infrastructure, whether EKS or self-managed.

At that point your inventory becomes not only a list of all your compute instances but also what and how much storage each of them is using, what they cost, whether they act as a backend to a load balancer and what domain names are pointing to that balancer. And if they are used as a K8S node you can list which version of a container is deployed on them.

Additionally if multi-cloud is relevant in your organisation, Resoto works across clouds. So if you have resources in GCP or DigitalOcean, you can obtain a comprehensive overview of your entire cloud infrastructure without having to export and merge data from both AWS Config and GCP Cloud Asset inventory.

As always, I would just use the best tool for the job. If you’re already using AWS Config and it meets your requirements then there is no reason to switch. But if your use-case is more complex maybe Resoto can be useful. It’s free and open source.

we want to write code that uses infrastructure data as an input

that’s the same guiding pricinple I’ve been using since I started working with AWS back in ~2011 or so. Nice idea

I would love to learn more about how you accomplish that, and what you’ve put in place! Ok if I shoot you a DM?

sure