Behind The Scenes: Integrating Infracost for Cost Visibility
Tags: infracost, cloud, finops, observability
Published: 2023-03-02 09:32:02

_This article is written with massive help from [Prajjwal Dimri](https://prajjwal.me/) who built the infracost integration._

Argonaut enables app deployment and management in Kubernetes on AWS and GCP. Moreover, cloud infra like RDS, s3, CloudSQL, GKE, EKS, etc. can also be provisioned and managed alongside your apps in one place -- across all environments. We aim to provide more visibility into your cloud setup. This article goes through Argonaut’s integration with Infracost for giving users instant cost estimates for their infra resources.

To achieve more visibility into cloud resources, one of the main challenges is getting a sense of cloud costs. [FinOps](https://finops.org/) is a fast-growing cross-functional discipline covering tools and best practices to improve cost spending on the cloud. Argonaut also provides its users with real-time cloud cost estimates; this is done with reliable and updated resource pricing from Infracost.

[Infracost.io](https://infracost.io) is one of the leading open-source projects for cloud cost, with over 3 million cloud resources pricing across AWS, GCP, and Azure. It can be used by invoking the [Cloud pricing API](https://www.infracost.io/docs/cloud_pricing_api/overview/), [CLI commands](https://www.infracost.io/docs/features/cli_commands/), or through one of [its integrations](https://www.infracost.io/docs/integrations/github_app/). It can also be self-hosted and used with the [Infracost Cloud](https://www.infracost.io/pricing/) subscription.

In [July 2022](https://www.argonaut.dev/blog/july-2022-release-notes), Argonaut launched the cost estimate feature by adopting Infracost to fetch the resource pricing of infra components shipped using Argonaut. We show infra cost for most of the infra components. Few resources having usage-based pricing like S3 are not supported.

## Cost visibility in Argonaut

Users can see the cost of infrastructure resources in two places. First, pre-creation, while you create/update a new resource. Second, post-creation, for existing resources with generated terraform files.

Price pre-creation estimates shown before creating an object can be seen below. (Infra create page). This is a quick estimate based on the values you have entered by you. This is not as accurate as the post-creation cost.

![Infra create page quick cost estimation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/imjf13w3shxdqdeclcme.png)

The post-creation cost can be seen below. (Infra list page). This version is more accurate as it uses the terraform config files to estimate the cost.

![Infra List page final cost estimation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7w3vxztmq6mo7idz5ci.png)

## Implementation

To achieve this, we built out a microservice, creatively named `costly`, that interacts with Argonaut's backend and the Infracost API to provide us with cost estimates. We have two different workflows that use Infracost and `costly` in different ways.

> Note: We strongly profess using a monolith architecture for our backend. We have a single backend that handles all the requests from the UI. This is a conscious decision to keep the backend simple and easy to maintain.
>
> We chose to build a separate microservice to interact with Infracost because we use REST APIs internally and Infracost is a GraphQL API.

### `costly` microservice

`costly` is a microservice created by our team to facilitate easy cost visibility at various stages of infra operation. `costly`’s job is to aggregate the pricing. (Base price + Storage + compute cost). It also understands the resource type and calculates the total cost by including other factors like region, number of node groups, and attributes based on the service type (e.g., purchase options for Kubernetes clusters).

The `costly` microservice processes the logic to separate fixed and variable costs based on the resource type. This cost is returned to the user in the Argonaut UI. This cost estimate is a rough estimate based on the values you have entered by you. By hovering over the `?` you can see the breakdown between Fixed and Variable costs.

![Costly microservice, cost breakdown - Fixed and Variable costs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lh8m8jktetq1k5iagrc6.png)

Here is an example of a graphQL query we run in the `costly` microservice. This shows the processing of requests for an AWS DocumentDB resource. The [Product properties](https://www.infracost.io/docs/cloud_pricing_api/overview/) are shared by the user, and the return value collected is the price in USD.

```graphQL
query ($region: String!, $instanceType: String!) {
  products(
    filter: {
      vendorName: "aws"
      region: $region
      service: "AmazonDocDB"
      attributeFilters: [{ key: "instanceType", value: $instanceType }]
    }
  ) {
    prices {
      USD
    }
  }
```

### Cost estimation workflows

Argonaut uses two workflows to get you the costs of resources. They are described below:

**Quick cost estimates (pre-creation)**

![Pre-creation cost estimate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67kto69j58hwlcjh6llj.png)

This is shown to the user when creating a new infra resource on Argonaut. They appear in the step where you create new infra resources or update an existing infra resource. Perform the following actions to get a quick cost estimate.
`Infra` > `Resource +` > Add resource details > See cost estimate

We get the above estimate by using Infracost’s Cloud Pricing API.

This process happens in this sequence.

1. A request is sent from the UI to the Argonaut backend containing the infra resource parameters. Parameters such as
   1. Vendor
   1. Service
   1. Product Family
   1. Region
   1. Attributes
1. This request is forwarded to the `costly` microservice.
1. `costly` microservice processes the logic to separate fixed and variable costs based on the resource type.
1. The Infracost cloud pricing API uses this information and returns the correct cost values based on the pricing charts available for the requested resource

**Infra provisioning along with cost estimate (post-creation)**  
![Post-creation cost estimate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/od8f0cd3l8n1rs15h39v.png)

Actual costs refer to the cost of resources shown once they are created and active. These costs are usually more accurate than the initial quick estimates provided. They have a slightly different flow, as in this step, we use Terraform config files to estimate your infra resources costs.

`costly` has an additional step where the returned cost is stored securely. A time-series clickhouse database is used here, and each entry is mapped to your account and resource ID. This will be part of upcoming features that provide more ways to save costs.
This process happens in this sequence:

1. User specifies resource attributes and clicks `create resource`.
1. The backend receives the request and provisions the necessary Terraform (TF) files and resources.
1. Then, initiates the cost calculations using Infracost for all resources in the TF file.
1. Costs are forwarded to `costly` along with the `resource_id`, which is then stored securely by `costly`. `costly` also calculates total costs.
1. This cost is then displayed to the user.

> Note: There are two additional scenarios where the post-creation workflow is triggered. These don’t require any user interaction and are automatically handled by Argonaut.
>
> 1. A `weekly cron job` updates all infra costs based on the latest Terraform files
> 1. A safeguard method that can initiate the cost estimation process if no value is found for the `resource_id` in the storage

## Benefits to Argonaut users

Argonaut provides several benefits by integrating infra cost visibility right into the resource CRUD workflows.

1. It saves the user time and effort compared to visiting other tools such as AWS cost calculator to obtain the relevant costs.
1. The decision-making process is faster. By viewing the costs in-line, you can decide whether the resource fits within your budget for that service.
1. The process is automated and based on TF modules (GitOps single source of truth principles), leaving less room for human errors than cost estimates using third-party software.
1. Argonaut updates its infra and resource costs weekly with a custom cost update service,
1. Make cost estimates as part of your infra workflow, achieve more savings, and improve your cloud resource utilization.
1. Supported resources - Argonaut currently provides cost information for these AWS and GCP resources, respectively
   1. AWS
      1. Aurora
      1. DocumentDB
      1. EKS
      1. OpenSearch (Elasticsearch)
      1. Elasticache
      1. MSK
      1. RDS
   1. GCP
      1. CloudSQL
      1. CloudComposer v2
      1. GKE

## Challenges faced during implementation

Here are a few challenges we faced and things we learned integrating `costly` and infracost. If you’re looking to add infracost to your workflows, do keep these in mind.

- Figuring out queries for cloud pricing APIs can be tricky.
- The documentation for some of the [attributes and values](https://www.infracost.io/blog/cloud-pricing-api/#concepts) is work in progress.
- For Google’s CloudSQL queries, the API returned text-based responses having various attributes. Regex matching was challenging and required additional custom functions to parse the responses.

However, despite these small hurdles, the infracost solution was easy to use, and their active community helped us resolve the [issue](https://github.com/infracost/infracost/issues/1728) we had raised on GitHub in a timely manner.

## Conclusion

Having visibility into your cloud costs is essential as you set up your cloud resources. Argonaut’s current cost visibility setup with Infracost is just the first step towards providing you with steps to save on cloud costs. Lots more features, such as historical cost insights, tips for cost savings, are planned for the next few months.

Do note that there are other [hidden costs](https://www.argonaut.dev/blog/hidden-cloud-costs) associated with cloud operations. More Kubernetes cost optimization strategies can be [found here](https://www.argonaut.dev/blog/k8s-cost-optimization-strategies).

Huge shoutout to the [Infracost](https://www.infracost.io/) team for building an amazing [open-source tool](https://github.com/infracost/infracost) to help us keep track of cloud costs across thousands of different services, products, regions, and configurations.