Repository navigation
Conversation
…esponse Tasks on ECS Managed Instances cannot reach IMDS, so they have no way to learn which host they run on. The agent already knows both values; expose them in the v4 /task and /taskWithTags responses. Addresses Gap 1 of aws/containers-roadmap#2788.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds two fields to the Task Metadata Endpoint v4 task response (
/taskand/taskWithTags) on EC2 and ECS Managed Instances:EC2InstanceID: the EC2 instance ID of the hostContainerInstanceARN: the ECS container instance ARN of the hostThis addresses Gap 1 of aws/containers-roadmap#2788. Tasks on ECS Managed Instances cannot reach IMDS (hop limit 1, not configurable), so today they have no way to learn which instance they run on. Users work around this with a Lambda that polls
DescribeContainerInstances/DescribeTasks. The same applies on EC2 when IMDS access from tasks is blocked (e.g.ECS_AWSVPC_BLOCK_IMDS=true).The agent already knows both values, so this only exposes them.
Question for maintainers: the roadmap issue asks for both fields, but only
EC2InstanceIDis otherwise unavailable to tasks.ContainerInstanceArncan already be fetched from the introspection endpoint (/v1/metadata). I included it because it's requested, costs one line, and saves a second call. Happy to drop it if you prefer to keep the v4 response minimal.Implementation details
ecs-agent/tmds/handlers/v4/state/response.go(+ vendored copy): newContainerInstanceARNandEC2InstanceIDfields onTaskResponse, bothomitempty, so responses without a value (e.g. ECS Anywhere has no EC2 instance ID) are unchanged.agent/handlers/v4/response.go:NewTaskResponsesets both fields. The container instance ARN was already passed in (used for tag lookup). The instance ID is a new parameter.currentEC2InstanceIDinagent/app/agent.go). It is threaded throughstartAsyncRoutines→ServeTaskHTTPEndpoint→taskServerSetup→NewTMDSAgentState, the same wayvpcID/availabilityZoneIDare.TaskARN,VPCID,AvailabilityZoneID) and the container metadata file (ContainerInstanceARN).ECS_CHECKPOINT=false) the agent does not resolve the instance ID at startup, soEC2InstanceIDis omitted.Testing
go test -tags unit ./agent/handlers/...passes (run in agolang:1.26.9Linux container).v4TaskResponseFromV2) now includes both fields, so all existing/v4/<id>/taskHTTP handler tests assert them.TestNewTaskContainerResponsesasserts them directly onNewTaskResponse.New tests cover the changes: yes
Description for the changelog
Feature - Add EC2InstanceID and ContainerInstanceARN to the task metadata v4 task response
Additional Information
Does this PR include breaking model changes? If so, Have you added transformation functions?
No. Two optional response fields are added. No persisted state changes.
Does this PR include the addition of new environment variables in the README?
No.
Licensing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.