Automating CloudHealth Perspectives with chtools

As a CloudHealth partner BlueChipTek enables its customers to get the most out of the platform. We leverage automation to help our customers tackle many of their challenges including cost management.

To this end, BlueChipTek has created chtools, a CLI utility and python package that can be used to automate several common tasks performed within CloudHealth. We have open sourced chtools to allow other CloudHealth customers to easily automated these tasks and help drive the adoption of our partner’s platform. In this blog post Joe Keegan, BlueChipTek Lead Cloud Services Architect, will show how chtools CLI utility can be used to automate the creation and management of CloudHealth Perspectives.

chtools overview

chtools is written in python3 and is easily installed via:

pip3 install chtools

chtools is modeled loosely on how the AWS CLI works, utilizing subcommands to specify features and actions you wish to perform within CloudHealth. Examples of chtools in action to create an AWS Account or get the JSON schema of a perspective would be as follows.

chtools aws-account create --name new-account --assume-role-arn arn:aws:iam::123456789012:role/CloudHealth
chtools perspective get-schema --name my-perspective

You will need an API Key to use chtools and can specify the key with either the --api-key argument or setting it in a CH_API_KEY environment variable. Instructions on how to create you API Key can be found here.

While chtools is not specifically intended to only support AWS related config within CloudHealth, we are an AWS Solutions Provider, so we have only tested it for our AWS use cases.

Perspectives and chtools

Perspectives are a powerful core feature of CloudHealth. They are a flexible way to group assets and their associated costs. Perspectives allow users to understand their cloud costs and relate those cost to their business. There were several challenges around perspectives we wanted to tackle with automation.

  1. We wanted to be able to take an “Infrastructure as Code” approach to managing perspectives. This included being able to store them in a source control system, version them, allow for peer review and deploy them using a pipeline.
  2. We wanted a way to backup Perspective configuration. There is no "undo" in CloudHealth and we wanted a way to be able to undo accidental or unwanted changes.
  3. We wanted a way to easily provide several standard perspectives to our customers to minimize the time to value with CloudHealth.

With chtools we can do all of this.

The CloudHealth API represents a Perspective as a JSON object. Working directly with the schema of this JSON object is complex and not at all human friendly. To simplify things, we came up with the concept of a Perspective spec, conceptually like a Kubernetes manifest. The Perspective spec is a YAML file that has some “syntactical sugar” enhancements to make working with the Perspective schema JSON much easier. These “syntactical sugar” enhancements can be viewed in the chtools README.md file.

Let’s look at how we can use chtools to make a change to an existing Perspective.  In this example we have a Perspective that is intended to give us insight on cost of assets that are part of our Production environments deployed into AWS. To this end we have Perspective groups for Prod assets and we allocate assets to the Prod group based on the value of tags.

Looking in the web GUI we can see the Prod Perspective group, and the various rules use to allocate assets to the Prod group.


Now let’s get the YAML spec for this perspective so that we can update it.


$ chtools perspective get-spec --name Environments > scratch/Environments.yaml
$ cat scratch/Environments.yaml 
include_in_reports: 'true'
name: Environments
rules:
- asset: AwsAsset
  condition:
    clauses:
    - op: '='
      tag_field: Env
      val: Prod
  to: Prod
  type: filter
- asset: AwsAsset
  condition:
    clauses:
    - op: '='
      tag_field: Env
      val: Release
  to: Prod
  type: filter
- asset: AwsAsset
  condition:
    clauses:
    - op: '='
      tag_field: Environment
      val: Prod
  to: Prod
  type: filter

We can see the spec contains rules that correspond to the rules we saw in the Web GUI. We will add a new rule that will include any AwsAsset that has the tag Environment with the value of Live. The rule would look like the following YAML mapping added to rules list.

- asset: AwsAsset
  condition:
    clauses:
    - op: '='
      tag_field: Environment
      val: Live
  to: Prod
  type: filter

Once the rule has been added then the updated YAML Spec it can be applied using the following command.

$ chtools perspective update --spec-file scratch/Environments.yaml
Updated Perspective Environments (https://apps.cloudhealthtech.com/perspectives/2954937502961)

If we look in the Web GUI we can see the new rule has been added

This is obviously a very simple example, but chtools gives us the capabilities to tackle the Perspectives problems we set out to address. You can learn more about chtools by visiting our cloudhealth-tools Github repo.

 

If you think a tool like chtools is useful, contact us and let us show you what we else can do to get your cloud cost management to the next level.