Praveen Valtix
Praveen Valtix

Reputation: 43

Retrieve custom data from azure linux VM

I have a question regarding accessing custom data from a Azure Linux VM after boot up. I am currently using the Azure GO SDK to programmatically create VMs which are based on CentOS Linux 7.5. For each VM, I am attaching a unique set of environment variables so that the boot up service scripts can access the environment. The custom data is only a set of environment variables, no actual scripts.

In the OS profile, I fill in the base64 encoded string as follows:

OsProfile: compute.OSProfile{
	ComputerName:  to.StringPtr(p.InstanceName),
	AdminUsername: to.StringPtr(p.UserName),
	LinuxConfiguration: compute.LinuxConfiguration{
		SSH: compute.SSHConfiguration{
			PublicKeys: []compute.SSHPublicKey{
				{
					Path: to.StringPtr(
						fmt.Sprintf("/home/%s/.ssh/authorized_keys",
							p.UserName)),
					KeyData: to.StringPtr(p.SshPublicKey),
				},
			},
		},
	},
	CustomData: to.StringPtr(base64.StdEncoding.EncodeToString([]byte(p.UserData))),
},

Its not clear to me, how to access the custom data from inside the VM.

In AWS case, we use the instance user data and access the data from the EC2 instance as follows:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

http://169.254.169.254/latest/user-data

Upvotes: 2

Views: 5002

Answers (3)

Karishma Tiwari - MSFT
Karishma Tiwari - MSFT

Reputation: 1545

Azure Instance Metadata Service now provides the ability for the VM to have access to its custom data. The binary data must be less than 64KB and is provided to the VM in base64 encoded form. For details on how to create a VM with custom data, see Deploy a Virtual Machine with CustomData.

Retrieving custom data in Virtual Machine Instance Metadata Service provides custom data to the VM in base64 encoded form. The following example decodes the base64 encoded string.

curl -H "Metadata:true" "http://169.254.169.254/metadata/instance/compute/customData?api-version=2019-02-01&&format=text" | base64 --decode

Reference Doc: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service#custom-data

Upvotes: 3

Ken W - Zero Networks
Ken W - Zero Networks

Reputation: 3804

The Azure Instance Metadata Service provides information about running virtual machine instances that can be used to manage and configure your virtual machines. This includes information such as SKU, network configuration, and upcoming maintenance events. For more information on what type of information is available, see metadata categories.

Azure's Instance Metadata Service is a REST Endpoint accessible to IaaS VMs created via the Azure Resource Manager. The endpoint is available at a well-known non-routable IP address (169.254.169.254) that can be accessed only from within the VM.

Upvotes: -1

Praveen Valtix
Praveen Valtix

Reputation: 43

Ok found the answer. Not very well documented.

https://azure.microsoft.com/en-us/blog/custom-data-and-cloud-init-on-windows-azure/

/var/lib/waagent/CustomData

Upvotes: 1

Related Questions