liveBook · Manning

EC2 metadata service

As explained in Amazon Web Services in Action, Third Edition: An in-depth guide to AWS

The EC2 Instance Metadata Service is an instance-local HTTP service that provides metadata and planned-action information to an EC2 instance.

The EC2 Instance Metadata Service (IMDS) is an instance-local HTTP service. An EC2 instance can query it for metadata and planned-action information without reaching an external endpoint. section 3.8.2

The service matters in two practical situations:

  • Spot Instance interruption handling: query the planned-action endpoint to learn whether AWS has scheduled termination within two minutes. section 3.8.2
  • Security: an attacker who reaches an EC2 machine can communicate with IMDS and authenticate as an AWS resource. section 5.3

Check for a planned Spot Instance action

From the instance, use Session Manager to run curl against http://169.254.169.254/latest/meta-data/spot/instance-action. A 404 response indicates that AWS has not marked the instance for termination; a response such as {"action": "stop", "time": "2022-04-08T12:12:00Z"} indicates a planned action. section 3.8.2

$ curl http://169.254.169.254/latest/meta-data/spot/instance-action
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www .w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www .w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
  <title>404 - Not Found</title>
 </head>
 <body>
  <h1>404 - Not Found</h1>
 </body>
</html>
sh-4.2$

Inspect the endpoint command and its 404 response. section 3.8.2

How Amazon Web Services in Action, Third Edition: An in-depth guide to AWS covers it

How deep it goes
Explains the service’s local access, interruption check, response meanings, and security significance without detailing broader metadata categories.
Examples
Demonstrates curl against the spot instance-action endpoint, a 404 response, and a JSON stop action.
What it assumes
Assumes access to an EC2 instance through Session Manager and basic familiarity with Spot Instance operation.
What you can do after
Diagnose impending Spot Instance termination by querying and interpreting the planned-action response.
How it connects
Connects EC2 metadata service with EC2, Spot Instances, Session Manager, and authentication.

Also explained in

Compare how 2 books explain EC2 metadata service →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Amazon Web Services in Action, Third Edition: An in-depth guide to AWS. The section links point to the parts of the book it draws on.