Last week I published a blog on how you can store AWS VM Instance metadata as tags for Azure Arc enabled servers, this week I want to share with you how you can do this with Google Cloud Platform (GCP) VM Instances. If you want to onboard a Google Cloud Platform (GCP) Compute Engine VM instance (virtual machine) to Azure using Azure Arc for multicloud management, you might want to store some of the GCP VM instances metadata as Azure tags for your Azure Arc enabled server. One of the easiest ways is to set these tags during the onboarding process of an Azure Arc-enabled server from GCP. GCP VM instances offer a service to retrieve instance metadata directly from the running instance, so you do not need to use the Google Cloud Platform console or the GCP CLI (This is similar to the Azure Instance Metadata Service on Azure VMs).
On GCP VM instances running Windows you can run the following PowerShell command to see the metadata available:
Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri "http://metadata.google.internal/computeMetadata/v1/instance/"
Now in my example, I want to store the GCP Zone as well as the GCP instance Id as an Azure tag for my Azure Arc enabled server. For onboarding the GCP VM instance, I use the Azure arc onboarding method by using a service principal. This is the simple script you can use, don’t forget to replace the variables for subscription ID, tenant ID, resource group, and more.
# Get GCP VM Instance data
$GCPZone = Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri "http://metadata.google.internal/computeMetadata/v1/instance/zone"
$GCPInstanceId = Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri "http://metadata.google.internal/computeMetadata/v1/instance/id"
$GCPAttirbute1 = Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri "http://metadata.google.internal/computeMetadata/v1/instance/attributes/MyGCPAttribute1"
# Create Tags
$tags = "Datacenter=GCP,CountryOrRegion=Germany,GCPZone=$GCPZone,GCPInstanceId=$GCPInstanceId"
# Add the service principal application ID and secret here
$servicePrincipalClientId="<service principal id>"
$servicePrincipalSecret="<service principal secret>"
# Download the installation package
Invoke-WebRequest -Uri "https://aka.ms/azcmagent-windows" -TimeoutSec 30 -OutFile install_windows_azcmagent.ps1
# Install the hybrid agent
& ".\install_windows_azcmagent.ps1"
if($LASTEXITCODE -ne 0) {
throw "Failed to install the hybrid agent"
}
# Run connect command
& "$env:ProgramW6432\AzureConnectedMachineAgent\azcmagent.exe" connect --service-principal-id "$servicePrincipalClientId" --service-principal-secret "$servicePrincipalSecret" --resource-group "<resourcegroup>" --tenant-id "<tenantid>" --location "<azureregion>" --subscription-id "<subscriptionid>" --cloud "AzureCloud" --tags "$tags" --correlation-id "<correlationid>"
if($LastExitCode -eq 0){Write-Host -ForegroundColor yellow "To view your onboarded server(s), navigate to https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.HybridCompute%2Fmachines"}
If you are onboarding a GCP VM instance running Linux to Azure Arc, you can use the following command to automatically add tags during the onboarding process.
# Get GCP VM Instance data
gcpzone="$(curl "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google")"
gcpinstanceid="$(curl "http://metadata.google.internal/computeMetadata/v1/instance/id" -H "Metadata-Flavor: Google")"
gcpattribute1="$(curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/MyGCPAttribute1" -H "Metadata-Flavor: Google")"
# Create Tags
tags="Datacenter=GCP,CountryOrRegion=Germany,GCPZone=$GCPZone,GCPInstanceId=$GCPInstanceId"
# Add the service principal application ID and secret here
$servicePrincipalClientId="<service principal id>"
$servicePrincipalSecret="<service principal secret>"
# Download the installation package
wget https://aka.ms/azcmagent -O ~/install_linux_azcmagent.sh
# Install the hybrid agent
bash ~/install_linux_azcmagent.sh
# Run connect command
azcmagent connect --service-principal-id "$servicePrincipalClientId" --service-principal-secret "$servicePrincipalSecret" --resource-group "<resourcegroup>" --tenant-id "<tenantid>" --location "<azureregion>" --subscription-id "<subscriptionid>" --cloud "AzureCloud" --tags "$tags" --correlation-id "<correlationid>"
if [ $? = 0 ]; then echo "\033[33mTo view your onboarded server(s), navigate to https://portal.azure.com/#blade/HubsExtension/BrowseResource/resourceType/Microsoft.HybridCompute%2Fmachines\033[m"; fi
Of course, there is way more metadata you can get from the machine itself. Maybe not everything you need, but at least you have access to some of the data.
I hope this article was helpful and showed you how you can store some of the GCP VM instances metadata as Azure tags for your Azure Arc enabled server. If you have any questions feel free to leave a comment.
Tags: Azure, Azure Arc, Cloud, GCP, Google, Linux, metadata, Microsoft, Microsoft Azure, multicloud, PowerShell, Tags, Virtual Machine, Windows Server Last modified: October 28, 2021