Your first priority should be to label projects with a cost-center. Resources inside those projects will inherit the project label for cost allocation.
You can then apply labels to resources when you need more specific cost attribution.
In this article we review methods for applying labels to both projects and resources.
There are many ways to apply and maintain project labels. We’ll review three methods.
The method you use will depend on what your team is comfortable with, and how large your environment is. Pick the method that is easiest to put in and maintain in your environment.
Our goal is for 100% cost-center label coverage for all projects. The billing reports we create will apply project labels to resources that do not have a cost-center label. This inheritance enables us to not concern ourselves with 100% labeling of individual resources. This inheritance is illustrated here:
In a future articles we will discuss monitoring to ensure coverage of project cost-center labeling is maintained. Another article will detail report creation that benefits from project label inheritance.
Applying Labels via Cloud Console
One way to apply labels to existing projects is using the Cloud Resource Manager in the Google Cloud Console. You can select 1 or more project and use the ‘side panel’ to apply a label to those projects.
Applying Labels via gcloud CLI
Another option you may use is the Cloud SDK CLI.
Updating the label3 on an existing project:
% gcloud projects update project-id-12345 --update-labels=cost-center=waffleiron
When creating a new project4:
% gcloud projects create new-project-id-12345 --labels=cost-center=waffleiron
Applying Labels via Terraform
If you are creating projects via code with Terraform and the Project Factory Module you can code the label assignment. The following is an example of applying a label to the project:
Verifying Project Labels with CloudCostMgmt Toolkit
The CloudCostMgmt Toolkit5 includes a script that will output a list of projects. You can use the output to plan your label assignment, and then for verification once complete.
cloudcostmgmt % python3 gcp/project-list.py projectName,projectId,projectNumber,createTime,lifecycleState,projectCostCenter Project Example A,project-a-1234,123456789012,2020-05-26T18:55:12.187Z,ACTIVE,waffleiron Project Example B,project-b-1234,234567890123,2020-05-26T18:54:53.805Z,ACTIVE,shirtpress Project Example C,project-c-1234,345678901234,2020-05-20T13:21:46.480Z,ACTIVE,shirtwaffle-shared Project Example D,project-d-1234,456789012345,2020-05-15T19:59:07.830Z,DELETE_REQUESTED,shirtpress
Labeling Resources Within Projects
Labeling only at the project level might not give you the cost break-down you need. You might need to provide more granular assignment of costs within a project. You do this by labeling individual resources.
Unfortunately, there is no standard way to label all resource types. Some don’t even support labeling. Some can be labeled when created via CLI or API but not from the web console. Some support labeling only during creation. Begin with the resource type you seek to label and then find methods to label them.
Ideally all resources are labeled when provisioned. However, you likely have existing resources that you want to apply labels to. Most, but not all, support having their labels updated. Persistent disks, for example, can have their labels modified via the web console or the gcloud CLI command:
gcloud compute disk update DISK_NAME --zone=us-central1-a --update-labels=cost-center=waffleiron
To apply labels upon provisioning you need to find and modify all processes that are creating resources. This might require modification of scripts, code, pipelines, and definitions throughout your environment.
An additional challenge is that not all resources are created directly by you or your provisioning processes. Sometimes a resource is created by another resource. An example of this is a Load Balancer created by a Google Kubernetes Engine (GKE) cluster. If you need to label this, you will have to instruct GKE to label it for you, or apply the label after creation.
There are a number of challenges you may face in applying labels to resources. Key recommendations are as follows:
- Drive resource labeling based on need, especially in existing deployments. Apply labels where more break-down is needed. Don’t waste effort on label a resource if project labeling is sufficient.
- Start with the resource type you want to label. If this is a new or frequently re-created resource, look at the documentation for your provisioning tool on how to label. If it is an existing resource with a long lifespan, look at the platform tools (CLI, Web Console, APIs) to see if they support label updating for this resource type.
- Define and communicate your labeling strategy2 broadly within your organization. This enables everyone who provisions resources to know what to do and why.
Labeling 100% of projects with a cost-center label should be the first priority. This can be accomplished even in large environments. Further break-down of costs can be achieved over time and as needed by applying labels to individual resources.