Voiced by Amazon Polly |
Overview
Managing many instances efficiently in Google Compute Engine (GCE) and track who created it can be complex. However, by leveraging the powerful feature of auto-labeling based on the creator attribute, you can simplify instance management and enhance resource visibility in your cloud environment. This blog will explore auto-labeling in GCE and demonstrate how to automate the labeling process to categorize and track instances based on their creators. Discover the benefits of auto-labeling by the creator, learn implementation techniques, and uncover best practices for designing a logical labeling structure.
Pioneers in Cloud Consulting & Migration Services
- Reduced infrastructural costs
- Accelerated application deployment
Introduction
In the world of cloud computing, managing a large number of instances efficiently is a common challenge.
By implementing auto-labeling based on the creator, you can streamline instance organization, enhance resource visibility, and automate operations effectively. This blog will explore auto-labeling in GCE and demonstrate how to leverage this feature to simplify instance management based on the creator.
Architecture
Steps to setup Auto-Labeler
- Create a service account
- Create a Role with the following permission:
1 2 |
compute.instances.get compute.instances.setLabels |
This role would be later attached to the service account of Cloud Function for labeling the GCE Instances.
- Create a service account with the previously created role assigned.
2. Create a Pub/Sub topic:
- This Pub/Sub topic would be the destination of the Log Router. It will receive every filtered log, such as the instance creation logs.
3. Create a Log Router:
- Create a log Router that will filter the logs, for instance, creation and route it to the Pub/Sub topic created earlier.
- Put the sink details like name, destination, etc.
- In the “Choose logs to include in sink”, fill this. Make sure to put your project ID.
1 2 |
logName="projects/<project_id>/logs/cloudaudit.googleapis.com%2Factivity" protoPayload.methodName: "compute.instances.insert" |
- Then click on “Create Sink”
4. Create Cloud Function:
- This Cloud Function would get triggered whenever there is any log of instance creation.
- The trigger for the Cloud Function should be “Pub/Sub” and have the service account attached to it.
- The codes for Cloud Function are:
package.json
1 2 3 4 5 6 7 8 9 10 11 12 |
{ "name": "auto-labeller-function", "version": "1.0.0", "description": "Cloud Function for auto-labeller", "main": "index.js", "scripts": { "start": "node index.js" }, "dependencies": { "@google-cloud/compute": "^3.9.1" } } |
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const { labelComputeEngineInstance } = require('./gce'); exports.labelResource = async (event, context) => { const logData = JSON.parse(Buffer.from(event.data, 'base64').toString()); console.log(JSON.stringify(logData)); // const logData = event.data; if ( String(logData.protoPayload.methodName).includes('compute.instances.insert') ) { console.log('Labelling Compute Engine Instance...'); await labelComputeEngineInstance(logData); } else { console.log(logData); } }; |
gcs.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
exports.labelComputeEngineInstance = async (log) => { // Start preparing the labels const resourceNameArray = String(log.protoPayload.resourceName).split('/'); const projectId = resourceNameArray[1]; const zone = resourceNameArray[3]; const instanceName = resourceNameArray[5]; const createdBy = String( log.protoPayload.authenticationInfo.principalEmail ).replace(/[^a-z0-9_-]/g, '-'); const labels = { 'created-by': createdBy, }; // End preparing the labels try { // Imports the Compute library const { InstancesClient } = require('@google-cloud/compute').v1; // Instantiates a client const computeClient = new InstancesClient(); // Construct get request const getRequest = { instance: instanceName, project: projectId, zone, }; // Run get request const getResponse = await computeClient.get(getRequest); if (getResponse[0].labels['created-by']) { console.log("Already has 'created-by' label. Exiting..."); return; } // Construct set label request const setLabelRequest = { instance: instanceName, project: projectId, zone, instancesSetLabelsRequestResource: { labelFingerprint: String(getResponse[0].labelFingerprint), labels: labels, }, }; // Run set label request const setLabelResponse = await computeClient.setLabels(setLabelRequest); console.log(JSON.stringify(labels)); console.log( `Labels set for ${instanceName} instance in project ${projectId}` ); } catch (error) { console.log( `Error setting lables to ${instanceName} VM in project ${projectId}.` ); console.log(error); } }; |
5. After the Cloud Function is created, your auto-labeller setup is complete. Try creating any GCE Instance, and the label ‘created-by’ will be applied to the instance with the creator’s email ID value.
Conclusion
Auto-labeling instances based on the creator attribute in Google Compute Engine brings a significant advantage to instance management and resource tracking. By automating the labeling process, you can effortlessly organize your instances, gain better visibility into resource usage, and streamline operations specific to individual creators. Leveraging auto-labeling by the creator empowers you to optimize your cloud infrastructure, improve accountability, and enhance collaboration among teams or individuals responsible for instance creation. With a well-designed labeling structure and automated workflows, you can effectively manage your GCE instances, maximize resource utilization, and achieve operational excellence in your cloud environment. Embrace auto-labeling by the creator in GCE and unlock the potential to streamline instance management, simplify resource tracking, and drive efficiency in your cloud-based applications and services.
Making IT Networks Enterprise-ready – Cloud Management Services
- Accelerated cloud migration
- End-to-end view of the cloud environment
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
1. What is GCE Instance Labelling in Google Cloud Platform (GCP)?
ANS: – GCE Instance Labelling in GCP is a feature that allows users to assign custom metadata labels to their Google Compute Engine (GCE) instances. These labels serve as key-value pairs that provide additional information and context about the instances.
2. Why is labeling GCE instances important for GCP users?
ANS: – Labelling GCE instances is important for GCP users because it enables better organization, management, and categorization of resources. With labels, users can easily identify and group instances based on specific criteria such as project, environment, owner, or purpose.
3. Are there any limitations or considerations when automating instance labeling in GCP?
ANS: – When automating instance labeling in GCP, it’s important to consider a few limitations. For example, label changes may not be immediately reflected in all GCP services or interfaces. It’s also crucial to ensure appropriate permissions and access controls are in place to prevent unauthorized modification of labels. Additionally, careful planning and testing are necessary to avoid unintended consequences or conflicts with existing labeling conventions.

WRITTEN BY Avinash Kumar
Avinash Kumar is a Senior Research Associate at CloudThat, specializing in Cloud Engineering, NodeJS development, and Google Cloud Platform. With his skills, he creates innovative solutions that meet the complex needs of today's digital landscape. He's dedicated to staying at the forefront of emerging cloud technologies.
Comments