Skip to content

How does it work

Patryk Kalinowski edited this page Feb 4, 2021 · 1 revision

This page describes the internals of Terracost.

Ingestion

Ingestion is a process during which Terracost downloads pricing data from cloud providers and saves it into the MySQL database. Since such data differs between providers, we've abstracted it away by using two entities: Products and Prices.

A Product is a single cloud provider offering, uniquely identified by an SKU number. It is composed of properties that describe it: service to which the Product belongs (e.g. "AmazonEC2"), location (e.g. AWS region), and a number of service-dependent attributes. These describe the product in more details and includes all the information necessary to find a single SKU.

Each Product, however, may have multiple prices. This happens, for example, with separate "on-demand" and "reserved" offers, or if the price changes depending on how much the product is used. This is why a second entity, Price, must be used to properly describe cloud provider pricing.

These two entities are stored in the pricing_products and pricing_product_prices tables respectively.

Estimation

Once the required pricing data is stored in the database, it may be used to estimate a Terraform plan. To do that, a plan is first read into a structure that describes it. Then, for each resource found within it, Terracost performs an SQL query requesting a Product (and associated Price) from the database that matches attributes included in the Terraform plan file. These are stored into two lists - one for the state prior to the plan and one for the planned outcome.

These two lists are stored into a Plan structure with methods that can be used to calculate the total (and partial) costs of either.

Clone this wiki locally