Skip to content

Add EC2InstanceID and ContainerInstanceARN to task metadata v4 task response - #5176

Open
lvthillo wants to merge 1 commit into
aws:masterfrom
lvthillo:tmds-v4-ec2-instance-id
Open

lvthillo wants to merge 1 commit into
aws:masterfrom
lvthillo:tmds-v4-ec2-instance-id

Conversation

@lvthillo

Copy link
Copy Markdown

Summary

Adds two fields to the Task Metadata Endpoint v4 task response (/task and /taskWithTags) on EC2 and ECS Managed Instances:

  • EC2InstanceID: the EC2 instance ID of the host
  • ContainerInstanceARN: the ECS container instance ARN of the host

This 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 EC2InstanceID is otherwise unavailable to tasks. ContainerInstanceArn can 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): new ContainerInstanceARN and EC2InstanceID fields on TaskResponse, both omitempty, so responses without a value (e.g. ECS Anywhere has no EC2 instance ID) are unchanged.
  • agent/handlers/v4/response.go: NewTaskResponse sets both fields. The container instance ARN was already passed in (used for tag lookup). The instance ID is a new parameter.
  • The instance ID is the value the agent already resolves at startup (currentEC2InstanceID in agent/app/agent.go). It is threaded through startAsyncRoutines → ServeTaskHTTPEndpoint → taskServerSetup → NewTMDSAgentState, the same way vpcID/availabilityZoneID are.
  • v2/v3 responses are unchanged. Field names follow the existing v4 style (TaskARN, VPCID, AvailabilityZoneID) and the container metadata file (ContainerInstanceARN).
  • Known limitation: with checkpointing disabled (ECS_CHECKPOINT=false) the agent does not resolve the instance ID at startup, so EC2InstanceID is omitted.

Testing

  • go test -tags unit ./agent/handlers/... passes (run in a golang:1.26.9 Linux container).
  • Unit and integration test packages compile for linux and windows.
  • The shared expected v4 task response (v4TaskResponseFromV2) now includes both fields, so all existing /v4/<id>/task HTTP handler tests assert them. TestNewTaskContainerResponses asserts them directly on NewTaskResponse.

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.

…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.
@lvthillo
lvthillo requested a review from a team as a code owner October 20, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant