digital-oceandevopsprovisioningdroplet

How do I programmatically get the Digital Ocean ID of the current Droplet from within the Droplet?


I am setting up monitoring and log aggregation on my AWS and Digital Ocean servers, and I want to be able to include a Droplet ID in the meta-data for my log messages from Digital Ocean.

On AWS there is a way to get the Instance ID from inside the instance: How to get the instance id from within an ec2 instance?

I'm looking for something similar in Digital Ocean so I can have a unique ID in my logs to identify a droplet.

I'm not looking for something like the hostname, since hostname can be set to anything in /etc/hostname. I might have multiple web servers behind a load balancer, all answering to the same hostname. IP addresses can change, so I don't want that.

I want a unique ID to help me find my Droplet in the API or web console, and I want to be able to get it from a shell script running on the Droplet.


Solution

  • You can run curl http://169.254.169.254/metadata/v1/id within the droplet, that will get you the ID of the droplet. If you want to use it in a shell script, you can do something like this:

    #!/bin/sh
    droplet_id=$(curl http://169.254.169.254/metadata/v1/id)
    echo "droplet id: $droplet_id"
    
    # use $droplet_id in your script whenever you need to identify the droplet
    
    ...