fix(ec2,ssm): explain missing IMDS registration for SSM-only managed instances (#3283)

This commit is contained in:
Matheus André authored and GitHub committed 2026-09-09 00:26:30 -05:00
1 parent f30ba77ac0
commit f77cf4da77
8 files changed
+119 -4

No files matched your search

@@ -143,6 +143,10 @@ public class Ec2MetadataServer {
if (inst != null) {
tokenToInstance.put(token, inst);
}
else {
LOG.debugv("IMDS: token requested from {0}, which is not a registered EC2 container; "
+ "metadata requests with this token will fail", ctx.request().remoteAddress().host());
}
ctx.response()
.setStatusCode(200)
@@ -354,12 +358,30 @@ public class Ec2MetadataServer {
String remoteIp = ctx.request().remoteAddress().host();
Instance inst = containerIpToInstance.get(remoteIp);
if (inst == null) {
LOG.warnv("IMDS: could not identify instance for request from {0}", remoteIp);
ctx.response().setStatusCode(404).end("Instance not found");
String message = unregisteredContainerMessage(remoteIp);
LOG.warnv("IMDS: {0}", message);
ctx.response().setStatusCode(404)
.putHeader("content-type", "text/plain")
.end(message);
}
return inst;
}
/**
* Explains why IMDS has nothing to serve for a request coming from {@code remoteIp}.
*
* <p>IMDS only knows about containers that {@link Ec2ContainerManager} launched through EC2
* {@code RunInstances}. Registering a container's SSM agent as a managed instance
* ({@code UpdateInstanceInformation}) does not create an EC2 instance record, so a container
* that was only registered with SSM ends up here.
*/
static String unregisteredContainerMessage(String remoteIp) {
return "Instance not found: no EC2 instance is registered for source IP " + remoteIp + ". "
+ "IMDS only serves containers launched through EC2 RunInstances; "
+ "registering a container as an SSM managed instance does not register it with IMDS. "
+ "Launch the container with RunInstances first, then register its SSM agent.";
}
// ── Utilities ─────────────────────────────────────────────────────────────
String resolveRoleName(String profileArn) {
@@ -114,12 +114,23 @@ public class SsmCommandService implements Resettable {
info.setComputerName(request.path("Hostname").asText(instanceId));
info.setRegion(region);
if (info.getRegistrationDate() == null) {
boolean firstRegistration = info.getRegistrationDate() == null;
if (firstRegistration) {
info.setRegistrationDate(Instant.now());
}
instanceStore.put(instanceKey(region, instanceId), info);
LOG.infov("SSM agent registered: instanceId={0} platform={1}/{2}", instanceId, info.getPlatformType(), info.getPlatformName());
// Registering with SSM does not create an EC2 instance record. Make it obvious when the
// agent lives in a container Floci did not launch through RunInstances: SendCommand falls
// back to agent polling and IMDS (169.254.169.254) has nothing to serve for that container.
if (firstRegistration && !directCommandExecutor.isContainerBacked(instanceId)) {
LOG.warnv("SSM managed instance {0} is not backed by a running Floci EC2 container. "
+ "SendCommand will use the agent polling flow and IMDS will not answer for it. "
+ "To get both, launch the container with EC2 RunInstances before registering its SSM agent.",
instanceId);
}
}
public List<InstanceInformation> describeInstanceInformation(String region) {
@@ -69,7 +69,15 @@ public class SsmDirectCommandExecutor {
if (!"AWS-RunShellScript".equals(documentName)) {
return false;
}
return isContainerBacked(instanceId);
}
/**
* Whether {@code instanceId} is a Floci EC2 instance whose Docker container is running.
* Only such instances get direct command execution and an IMDS registration; any other
* managed instance is served through the SSM agent polling flow.
*/
public boolean isContainerBacked(String instanceId) {
Instance instance = ec2Service.findInstanceById(instanceId);
if (instance == null || instance.getDockerContainerId() == null || instance.getDockerContainerId().isBlank()) {
return false;