DEV Community

Cover image for Behind The Scenes: Integrating Infracost for Cost Visibility
Argonaut
Argonaut

Posted on • Originally published at argonaut.dev

Behind The Scenes: Integrating Infracost for Cost Visibility

This article is written with massive help from Prajjwal Dimri 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 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 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, CLI commands, or through one of its integrations. It can also be self-hosted and used with the Infracost Cloud subscription.

In July 2022, 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

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

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

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 are shared by the user, and the return value collected is the price in USD.

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

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

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
    2. Service
    3. Product Family
    4. Region
    5. Attributes
  2. This request is forwarded to the costly microservice.
  3. costly microservice processes the logic to separate fixed and variable costs based on the resource type.
  4. 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

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.
  2. The backend receives the request and provisions the necessary Terraform (TF) files and resources.
  3. Then, initiates the cost calculations using Infracost for all resources in the TF file.
  4. Costs are forwarded to costly along with the resource_id, which is then stored securely by costly. costly also calculates total costs.
  5. 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
  2. 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.
  2. 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.
  3. 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.
  4. Argonaut updates its infra and resource costs weekly with a custom cost update service,
  5. Make cost estimates as part of your infra workflow, achieve more savings, and improve your cloud resource utilization.
  6. Supported resources - Argonaut currently provides cost information for these AWS and GCP resources, respectively
    1. AWS
      1. Aurora
      2. DocumentDB
      3. EKS
      4. OpenSearch (Elasticsearch)
      5. Elasticache
      6. MSK
      7. RDS
    2. GCP
      1. CloudSQL
      2. CloudComposer v2
      3. 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 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 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 associated with cloud operations. More Kubernetes cost optimization strategies can be found here.

Huge shoutout to the Infracost team for building an amazing open-source tool to help us keep track of cloud costs across thousands of different services, products, regions, and configurations.

Top comments (0)