The DNS based scheme has the most general utility and could solve for all cases, but does require a third party (so it could vanish or change). The provider metadata route is the most encapsulated, but requires an implementation per provider (assuming that they provide instance metadata... a fairly safe assumption, but an assumption nonetheless)
There are a few techniques that could be used:
3rd party DNS
Instance Metadata
AWS
Digital Ocean
Azure
The DNS based scheme has the most general utility and could solve for all cases, but does require a third party (so it could vanish or change). The provider metadata route is the most encapsulated, but requires an implementation per provider (assuming that they provide instance metadata... a fairly safe assumption, but an assumption nonetheless)