{"id":371167,"date":"2021-11-23T15:00:10","date_gmt":"2021-11-23T12:00:10","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-access-and-use-digitalocean-droplet-metadata-cloudsavvy-it\/"},"modified":"2021-11-23T15:00:10","modified_gmt":"2021-11-23T12:00:10","slug":"how-to-access-and-use-digitalocean-droplet-metadata-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-access-and-use-digitalocean-droplet-metadata-cloudsavvy-it\/","title":{"rendered":"#How to Access and Use DigitalOcean Droplet Metadata \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6aee4bdde4554\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6aee4bdde4554\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-access-and-use-digitalocean-droplet-metadata-cloudsavvy-it\/#User_Data\" >User Data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-access-and-use-digitalocean-droplet-metadata-cloudsavvy-it\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Access and Use DigitalOcean Droplet Metadata \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage aligncenter size-full wp-image-8934\" srcset=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/83680385.jpeg?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/83680385.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/83680385.jpeg?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"DigitalOcean logo\" width=\"1600\" height=\"900\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>DigitalOcean droplets possess metadata that reveal information about the compute instance and its environment. You can supply your own arbitrary data to make custom values accessible from inside your droplet.<\/p>\n<p>The default metadata available on every droplet lets you query key information such as the DigitalOcean datacentre region, network interface addresses, and tags assigned in the control panel or via the API.<\/p>\n<p>A droplet can retrieve its own metadata by calling the special network address, <code>169.254.169.254<\/code>. Connect to your droplet over SSH and use the <code>curl<\/code> command to experiment with the available API endpoints.<\/p>\n<p>Here\u2019s a basic example that provides a list of supported metadata fields:<\/p>\n<pre>curl http:\/\/169.254.169.254\/metadata\/v1\/<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-14897\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/11\/750c2144.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"867\" height=\"348\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>You can access any of the fields included in this endpoint\u2019s output by <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ending the field name to the metadata API\u2019s base URL:<\/p>\n<pre>curl http:\/\/169.254.169.254\/metadata\/v1\/id<\/pre>\n<p>This example will provide the droplet\u2019s IP address in its response.<\/p>\n<p>The following metadata fields are currently supported:<\/p>\n<ul>\n<li><code>id<\/code> \u2013 The droplet\u2019s public IP address.<\/li>\n<li><code>hostname<\/code> \u2013 The droplet\u2019s hostname, matching the name supplied when creating the droplet. It may not reflect the name in <code>\/etc\/hostname<\/code> if you manually change it later.<\/li>\n<li><code>user-data<\/code> \u2013 Arbitrary user-supplied data (see the later section in this guide).<\/li>\n<li><code>vendor-data<\/code> \u2013 DigitalOcean-issued data used to configure the droplet.<\/li>\n<li><code>public-keys<\/code> \u2013 SSH public keys that were added to the droplet when it was created via the control panel or API. These keys are added to the <code>root<\/code> account automatically.<\/li>\n<li><code>region<\/code> \u2013 The DigitalOcean datacentre that the droplet resides in, such as <code>nyc1<\/code> or <code>lon1<\/code>.<\/li>\n<li><code>interfaces<\/code> \u2013 Provides details of the droplet\u2019s network interfaces. The response will contain <code>private<\/code> and <code>public<\/code> fields, each an array of objects that describe configured network interfaces. The details include <code>ipv4<\/code>, to obtain the IP address, and <code>mac<\/code>, the droplet\u2019s reported MAC address on that network.<\/li>\n<li><code>floating_ip<\/code> \u2013 When a DigitalOcean Floating IP is assigned to the droplet, this field exposes its details.<\/li>\n<li><code>dns<\/code> \u2013 Contains a sub-field <code>nameservers<\/code> that lists the nameservers used by the droplet when performing DNS lookups.<\/li>\n<li><code>tags<\/code> \u2013 Provides a list of all the user-created tags assigned to the droplet.<\/li>\n<li><code>features<\/code> \u2013 An object describing DigitalOcean features that have been activated for the droplet.<\/li>\n<\/ul>\n<p>Fields that contain nested data or are arrays of objects can be traversed using API endpoints. Here\u2019s an example that provides the IP address of the droplet\u2019s first private network interface:<\/p>\n<pre>curl http:\/\/169.254.169.254\/metadata\/v1\/interfaces\/private\/0\/ipv4\/address<\/pre>\n<p>This makes it easy to extract specific values for use in your scripts. You don\u2019t always need to manually parse the JSON response bodies.<\/p>\n<p>Once you\u2019ve obtained your droplet\u2019s ID, your scripts could defer to the regular DigitalOcean API to gain more information or perform droplet actions. As an example, you might want to <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.digitalocean.com\/reference\/doctl\/reference\/compute\/droplet-action\/snapshot\">create a snapshot<\/a> before running a destructive sequence of commands. Your script could use the metadata API to discover the droplet ID it\u2019s running on, then pass the ID to the snapshots REST API to create a new snapshot. The script would still be portable across different droplets as you\u2019re not hardcoding the ID.<\/p>\n<p>You can get all the metadata associated with a droplet by appending <code>.json<\/code> to the base API endpoint:<\/p>\n<pre>curl http:\/\/169.254.169.254\/metadata\/v1.json<\/pre>\n<p>This will provide a JSON object containing all the fields described above. Fields with a <code>-<\/code> (hyphen) character will be converted to use <code>_<\/code> (underscore) instead in the JSON.<\/p>\n<h2 id=\"user-data\"><span class=\"ez-toc-section\" id=\"User_Data\"><\/span>User Data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>DigitalOcean lets you provide user data when you create a new droplet. It can be supplied either via the form in the control panel or with the <code>--user-data<\/code> flag when you\u2019re using <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.digitalocean.com\/reference\/doctl\/reference\/compute\/droplet\/create\"><code>doctl compute droplet create<\/code><\/a>. User data is exposed by the <code>user-data<\/code> field in the metadata API.<\/p>\n<p>Like other forms of metadata, user data is immutable and can\u2019t be modified after the droplet\u2019s creation. It\u2019s normally used to configure first-run package installations and config changes but you could also add arbitrary data fields that make sense to your organization.<\/p>\n<p>User data gets passed to <code>cloud-init<\/code>. This program runs on a droplet\u2019s first boot and acts as a mechanism to automate initial setup. To use <code>cloud-init<\/code>, add the contents of a <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/an-introduction-to-cloud-config-scripting\"><code>cloud-config<\/code>-compatible YAML<\/a> file to your droplet\u2019s <code>user-data<\/code>. This example updates all existing packages, writes a file, and creates a new user account:<\/p>\n<pre class=\"yaml\">users:&#13;\n  name: example&#13;\n  groups: sudo&#13;\n  shell: \/bin\/bash&#13;\nwrite_files:&#13;\n  - path: \/example.txt&#13;\n    content: \"Arbitrary data here\"&#13;\npackage_update: true<\/pre>\n<p>The metadata API is a service that\u2019s only available from within a droplet. You interact with it using <code>curl<\/code>, alternative terminal HTTP clients, or your programming language\u2019s HTTP library within scripts. DigitalOcean provides an official client library <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/digitalocean\/go-metadata\">for Go<\/a> which acts as a basic wrapper around the API.<\/p>\n<p>You can\u2019t directly query a droplet\u2019s metadata from the outside. Neither the Doctl CLI nor the main <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.digitalocean.com\/reference\/api\/api-reference\">DigitalOcean REST API<\/a> support retrieving the exact data structure exposed by the in-droplet metadata service.<\/p>\n<p>If you want to access droplet details such as networking interfaces, tags, and system configuration externally, you\u2019ll need to use other features of the API and Doctl. The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/docs.digitalocean.com\/reference\/doctl\/reference\/compute\/droplet\/get\"><code>doctl compute droplet get<\/code> command<\/a> is a good starting point which provides much of the same information as the Metadata service, as well as some extra details including hardware resource limits.<\/p>\n<h2 id=\"summary\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The DigitalOcean Metadata service is a special API that you can access from <em>within<\/em> your droplets. It helps you build scripts that need to reference key attributes of your droplet, such as its ID, datacentre region, or IP address.<\/p>\n<p>You can add your own information to a droplet\u2019s metadata during the creation process. The metadata API will expose it as the <code>user-data<\/code> field. This means you can use metadata as a basic config store for your scripts and bootstrap routines, lessening the need to copy files or set environment variables each time you create a new droplet.<\/p>\n<p>Metadata is always specific to an individual droplet. The API exists to provide a droplet introspection mechanism. Use the regular REST API, either directly or via Doctl, if you want to query your DigitalOcean account to find other droplets with similar attributes.\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14894\/how-to-access-and-use-digitalocean-droplet-metadata\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Access and Use DigitalOcean Droplet Metadata \u2013 CloudSavvy IT&#8221; DigitalOcean droplets possess metadata that reveal information about the compute instance and its environment. You can supply your own arbitrary data to make custom values accessible from inside your droplet. The default metadata available on every droplet lets you query key information such as&#8230;<\/p>\n","protected":false},"author":1,"featured_media":371168,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/01\/83680385.jpeg","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-371167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/371167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=371167"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/371167\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/371168"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=371167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=371167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=371167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}