Add IMDS fallback with DescribeInstances. #1139
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the issue
Related to #1101.
Instance Metadata Service (IMDS) provides key information about the local EC2 instance including region and instance ID. The CloudWatch Agent relies on IMDS to retrieve those fields, which it uses as metric dimensions. As a security best practice, EKS recommends that the hop limit on worker nodes is set to 1. This effectively disables IMDS within containers as any response for a call made within a container will require at least 2 hops.
In the cases where IMDS is not available or disabled, the agent needs an additional fallback mechanism to retrieve the metadata without IMDS. For EC2 instances, the default AWS hostname follows a fixed format and will be prefixed with either the private IPv4 address (
ip-10-24-34-0.ec2.internal
) or the instance ID (i-0123456789abcdef.us-west-2.compute.internal
) based on the hostname type configured on the instance. In these cases,DescribeInstances
can be used to get the instance metadata instead.Description of changes
Adds a fallback metadata provider that parses the hostname to determine an EC2 filter and makes a call to
DescribeInstances
with that filter. Consolidates the metadata provider functions into a single package.License
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Tests
Added unit tests and built/installed the agent on an EC2 host with IMDS disabled. Configured
common-config.toml
to point to shared credential file.fmt.Print
to stdout isn't being logged for some reason. The IMDS logging is from the AWS SDK.Without DescribeInstances permissions
With DescribeInstances permissions
Requirements
Before commit the code, please do the following steps.
make fmt
andmake fmt-sh
make lint