Misleading 404 logs in IMDS mode when querying EC2 Metadata without a public IP by xabinapal · Pull Request #1058 · aws/aws-node-termination-handler
Description of changes:
While running NTH in IMDS mode on instances without an assigned public IP, I noticed that requests to certain EC2 Metadata endpoints, specifically public-hostname and public-ipv4, return a 404 status code. This is the expected behavior, as documented here. Although NTH does not fail and works as expected in this scenario, some log messages about these 404 status codes are displayed:
2024/08/29 13:42:34 INF Metadata response status code: 404. Body: <?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>
These messages don't clarify the reason for the 404 codes, which can mislead users (like myself!) into thinking something is wrong. To address this, I've added an allowMissing parameter to the GetMetadataInfo function. When this flag is enabled, no message will be logged for 404 responses, and no error will be returned.
While this might not be the most elegant solution, I felt like any other alternative, such as implementing the options pattern, would be overkill and too complex for the limited scope of the function.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.