This example demonstrates how to use resource labels to 1) organize your VMs, and 2) monitor better your GCP spending. It creates an environment with VMs that have the labels: team, cost-center, env, and app. As a result, you can use the Cloud Billing and Data Studio to explore your GCP spending data grouping them by labels according to your business.
Pre-req:
- Create a new project and select it
- Open Cloud Shell and clone this repo into the Cloud Shell VM
git clone https://github.com/sylvioneto/terraform_gcp.git
- Ensure the var is set, otherwise set it with
gcloud config set project
command
echo $GOOGLE_CLOUD_PROJECT
- Create a bucket to store your project's Terraform state
gsutil mb gs://$GOOGLE_CLOUD_PROJECT-tf-state
- Enable the necessary APIs
gcloud services enable cloudbuild.googleapis.com \
compute.googleapis.com \
container.googleapis.com \
cloudresourcemanager.googleapis.com
-
Go to IAM and add
Editor
andSecurity Admin
role to the Cloud Build's service account<PROJECT_NUMBER>@cloudbuild.gserviceaccount.com
. -
Execute Terraform using Cloud Build
cd ./terraform_gcp/cost-management
gcloud builds submit . --config cloudbuild.yaml
To see your VMs with labels, please mark the Labels checkbox in the displayed columns.
Wait at least 24h before checking your billing data with Data Studio or Cloud Billing since the Billing Export needs time to register the usage.
- Execute Terraform using Cloud Build
cd ./terraform_gcp/cost-management
gcloud builds submit . --config cloudbuild_destroy.yaml