How to calculate the creation time for Azure VM Instances

Simarpreet Arora Cloudwiry Written by Simarpreet Arora


For organizations, knowing the creation time of an instance is useful in Azure to keep track of the running time of Azure VM instances.

Sounds like a pretty simple task, and a simple Google search should solve it, right? But, when we search for it, on every page, most cloud engineers suggest that there is no easy way to know for sure.

There are a few possible solutions :

  1. Go to Azure log analytics and get the start date of the instance (but Azure log analytics does not offer visibility beyond 90 days).

  2. If the VM uses a managed disk, you can check the creation date of the managed disk.

  3. Get data from OS disk name.

Seems like a simple problem, but the solution is too complicated, and most of us give up on it thinking we can live without the starting date plus, this feels like a hack job.

In comes Azure’s API to get the creation date in one step. I am not sure why it’s so difficult to get to this link on Google:

https://docs.microsoft.com/en-us/rest/api/resources/resources/list

 


from msrestazure.azure_active_directory import ServicePrincipalCredentials


def get_credentials(account):
    return ServicePrincipalCredentials(
        client_id=account['clientId'],
        secret=account['clientSecret'],
        tenant=account['tenantId']
    )


client = ResourceManagementClient(get_credentials(account), account['name'])
for resource in client.resources.list(expand='createdTime,changedTime'):
    #todo use object directly or use a convertor to convert obj to dict
    resource = dict_utils.convert_object_to_dictionary(resource)
    result.append({
        'id': resource['id'],
        'created_time': resource['created_time'],
        'changed_time': resource['changed_time']
    })

return result

Hope this helped cut short your struggle to find a solution for something this trivial!

 

For more strategies on how to optimize your cloud, schedule a call with our cloud experts or drop us an email at hello@cloudwiry.com

GET STARTED
cloudwiryqa
Share
This