/terraform-resource_counter

Count the number of terraform resources of a type in a TFE org

Primary LanguageShell

Terraform Enterprise Resource Counter

Overview

This script is meant to answer either of the questions below:

  • How many $type am I currently running in my TFC/TFE org?
  • How many resources of any type do I currently have provisioned in my TFC/TFE org?

Setup

You'll need two environment variables to get started. There are some optional ones as well.

  • TFH_org: The name of the TFE organization to be targeted
  • TFH_token: A token which has access to pull workspaces and statefiles
  • TFH_hostname (optional): DNS name to TFE server
    Default: app.terraform.io
  • TFE_API_VERSION (optional): Version of the TFE api to use
    Default: v2

Example

To count by a specific resource type:

export TFH_org=WhatsARanjit
export TFH_token=XXXXXXXXXXXXXXX
./tfe_count.sh azurerm_resource_group
Workspace terraform-azure-devvm-aks has 0 azurerm_resource_group
Workspace terraform-azure-ssh has 0 azurerm_resource_group
Workspace neil-test-aks-privileged has 0 azurerm_resource_group
Workspace terraform-azure-resource-groups has 2 azurerm_resource_group
Workspace terraform-azure-aks-winnodepool has 1 azurerm_resource_group
Workspace gitlab-terraform-cloud-aks has 0 azurerm_resource_group
Workspace my-gcp-gitlab-pipeline has 0 azurerm_resource_group
Workspace vault-azure-demo has 0 azurerm_resource_group
Organization GitlabCI-demos has 3 azurerm_resource_group resources

To count by any resource type:

export TFH_org=WhatsARanjit
export TFH_token=XXXXXXXXXXXXXXX
./tfe_count.sh
INFO: Did not specify resource type, will check for all resource count
Workspace terraform-azure-devvm-aks has 0 resources
Workspace terraform-azure-ssh has 0 resources
Workspace neil-test-aks-privileged has 2 resources
Workspace terraform-azure-resource-groups has 2 resources
Workspace terraform-azure-aks-winnodepool has 10 resources
Workspace gitlab-terraform-cloud-aks has 0 resources
Workspace my-gcp-gitlab-pipeline has 0 resources
Workspace vault-azure-demo has 0 resources
Organization GitlabCI-demos has 14 resources