Both AWS and GCP, probably Azure as well, offer the ability to query instance level metadata from within the guest (and container)
Both cloud use the same IP address (169.254.169.254) internally to handle the request.
tl;dr, do a get request against the metadata url and look at the response headers.
curl -vv 169.254.169.254
* Expire in 0 ms for 6 (transfer 0x5557b4810fb0)
* Trying 169.254.169.254...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x5557b4810fb0)
* Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)
> GET / HTTP/1.1
> Host: 169.254.169.254
> User-Agent: curl/7.64.0
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: BaseHTTP/0.3 Python/2.7.13
< Date: Mon, 10 May 2021 16:01:58 GMT
< Content-Type: application/text
< Metadata-Flavor: Google
<
0.1/
computeMetadata/
* Closing connection 0
$ dig metadata.google.internal
; <<>> DiG 9.11.5-P4-5.1+deb10u5-Debian <<>> metadata.google.internal
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39546
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;metadata.google.internal. IN A
;; ANSWER SECTION:
metadata.google.internal. 3600 IN A 169.254.169.254
;; Query time: 0 msec
;; SERVER: 169.254.169.254#53(169.254.169.254)
;; WHEN: Mon May 10 15:59:56 UTC 2021
;; MSG SIZE rcvd: 69
https://cloud.google.com/compute/docs/storing-retrieving-metadata#querying
curl "http://metadata.google.internal/computeMetadata/v1/instance"
root@5f9f3662a783:/# curl -vv "http://metadata.google.internal/computeMetadata/v1/instance"
* Trying 169.254.169.254:80...
* TCP_NODELAY set
* Connected to metadata.google.internal (169.254.169.254) port 80 (#0)
> GET /computeMetadata/v1/instance HTTP/1.1
> Host: metadata.google.internal
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: BaseHTTP/0.3 Python/2.7.13
< Date: Mon, 10 May 2021 15:55:56 GMT
< Content-Type: application/text
< Metadata-Flavor: Google
<
attributes/
cpu-platform
description
disks/
guest-attributes/
hostname
id
image
legacy-endpoint-access/
licenses/machine-typemaintenance-eventnamenetwork-interfaces/preemptedremaining-cpu-timescheduling/
service-accounts/
tags
virtual-clock/
zone
* Closing connection 0
on AWS, https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
Below is an example of querying the AWS Url on GCP. The
root@5f9f3662a783:/# curl -vv http://169.254.169.254/latest/meta-data/
* Trying 169.254.169.254:80...
* TCP_NODELAY set
* Connected to 169.254.169.254 (169.254.169.254) port 80 (#0)
> GET /latest/meta-data/ HTTP/1.1
> Host: 169.254.169.254
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 404 Not Found
< Server: BaseHTTP/0.3 Python/2.7.13
< Date: Mon, 10 May 2021 15:53:25 GMT
< Content-Type: text/html; charset=UTF-8
< Metadata-Flavor: Google
<
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 404 (Not Found)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>404.</b> <ins>That’s an error.</ins>
<p>The requested URL <code>/latest/meta-data/</code> was not found on this server. <ins>That’s all we know.</ins>
* Closing connection 0