feat(ec2): add instance metadata catalog and architecture parity (#1596)

* fix(ec2): expose instance processor architectures

* feat(ec2): add instance type metadata catalog

* fix(ec2): enforce instance architecture parity

* fix(ec2): tighten instance metadata parity
This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-05 09:26:38 -05:00
1 parent 8893d2c51e
commit c233eb4891
10 files changed
+212 -11

No files matched your search

@@ -120,6 +120,7 @@ public class Ec2InstanceTypeCatalog {
public String instanceType;
public int vcpu;
public int memoryMib;
public int localStorageGiB;
public List<String> supportedArchitectures = List.of();
public Boolean currentGeneration;
@@ -128,6 +129,8 @@ public class Ec2InstanceTypeCatalog {
type.put("instanceType", instanceType);
type.put("vcpu", vcpu);
type.put("memoryMib", memoryMib);
type.put("instanceStorageSupported", localStorageGiB > 0);
type.put("localStorageGiB", localStorageGiB);
type.put("supportedArchitectures", List.copyOf(supportedArchitectures));
type.put("currentGeneration", currentGeneration == null || currentGeneration);
return type;
@@ -310,9 +310,19 @@ public class Ec2MetadataServer {
if (inst == null) {
return;
}
String body = instanceIdentityDocument(inst, config.defaultAccountId());
ctx.response().setStatusCode(200)
.putHeader("content-type", "application/json")
.end(body);
}
static String instanceIdentityDocument(Instance inst, String accountId) {
String az = inst.getPlacement() != null ? inst.getPlacement().getAvailabilityZone() : "us-east-1a";
String body = "{\"accountId\":\"" + config.defaultAccountId() + "\","
+ "\"architecture\":\"x86_64\","
String architecture = inst.getArchitecture() == null || inst.getArchitecture().isBlank()
? "x86_64"
: inst.getArchitecture();
String body = "{\"accountId\":\"" + accountId + "\","
+ "\"architecture\":\"" + architecture + "\","
+ "\"availabilityZone\":\"" + az + "\","
+ "\"imageId\":\"" + inst.getImageId() + "\","
+ "\"instanceId\":\"" + inst.getInstanceId() + "\","
@@ -320,9 +330,7 @@ public class Ec2MetadataServer {
+ "\"privateIp\":\"" + nvl(inst.getPrivateIpAddress()) + "\","
+ "\"region\":\"" + inst.getRegion() + "\","
+ "\"version\":\"2017-09-30\"}";
ctx.response().setStatusCode(200)
.putHeader("content-type", "application/json")
.end(body);
return body;
}
// ── Instance resolution ───────────────────────────────────────────────────
@@ -1601,11 +1601,18 @@ public class Ec2QueryHandler {
.start("memoryInfo")
.elem("sizeInMiB", String.valueOf(t.get("memoryMib")))
.end("memoryInfo")
.elem("instanceStorageSupported", String.valueOf(t.get("instanceStorageSupported")));
if (Boolean.TRUE.equals(t.get("instanceStorageSupported"))) {
xml.start("instanceStorageInfo")
.elem("totalSizeInGB", String.valueOf(t.get("localStorageGiB")))
.end("instanceStorageInfo");
}
xml.start("processorInfo")
.start("supportedArchitectures");
for (String arch : (List<String>) t.get("supportedArchitectures")) {
xml.start("item").elem("item", arch).end("item");
xml.elem("item", arch);
}
xml.end("supportedArchitectures").end("item");
xml.end("supportedArchitectures").end("processorInfo").end("item");
}
xml.end("instanceTypeSet").end("DescribeInstanceTypesResponse");
return xmlResponse(xml.build());
@@ -569,7 +569,10 @@ public class Ec2Service {
reservation.setReservationId(reservationId);
reservation.setOwnerId(accountId);
String effectiveInstanceType = instanceType != null ? instanceType : "t2.micro";
validateArchitectureCompatibility(imageId, effectiveInstanceType);
int count = Math.min(maxCount, Math.max(minCount, 1));
String architecture = architectureFor(imageId, effectiveInstanceType);
for (int i = 0; i < count; i++) {
String instanceId = "i-" + randomHex(17);
String privateIp = assignPrivateIp(region, finalSubnetId);
@@ -578,7 +581,7 @@ public class Ec2Service {
inst.setInstanceId(instanceId);
inst.setImageId(imageId);
inst.setState(InstanceState.pending());
inst.setInstanceType(instanceType != null ? instanceType : "t2.micro");
inst.setInstanceType(effectiveInstanceType);
inst.setPlacement(new Placement(az));
inst.setSubnetId(finalSubnetId);
inst.setVpcId(vpcId);
@@ -586,7 +589,7 @@ public class Ec2Service {
inst.setPrivateDnsName("ip-" + privateIp.replace('.', '-') + ".ec2.internal");
inst.setKeyName(keyName);
inst.setSecurityGroups(new ArrayList<>(sgIdentifiers));
inst.setArchitecture("x86_64");
inst.setArchitecture(architecture);
inst.setLaunchTime(Instant.now());
inst.setAmiLaunchIndex(i);
inst.setClientToken(clientToken);
@@ -700,6 +703,36 @@ public class Ec2Service {
}
}
private void validateArchitectureCompatibility(String imageId, String instanceType) {
Optional<String> imageArchitecture = imageCatalog.findByIdOrAlias(imageId)
.map(image -> image.architecture)
.filter(value -> !value.isBlank());
if (imageArchitecture.isEmpty()) {
return;
}
instanceTypeCatalog.find(instanceType)
.filter(type -> type.supportedArchitectures.stream()
.noneMatch(imageArchitecture.get()::equals))
.ifPresent(type -> {
throw new AwsException("InvalidParameterValue",
"The architecture '" + imageArchitecture.get()
+ "' of the specified image does not match the architecture supported by instance type '"
+ instanceType + "'.",
400);
});
}
private String architectureFor(String imageId, String instanceType) {
Optional<Ec2ImageCatalog.CatalogImage> image = imageCatalog.findByIdOrAlias(imageId);
return image.map(catalogImage -> catalogImage.architecture)
.filter(value -> !value.isBlank())
.or(() -> instanceTypeCatalog.find(instanceType)
.flatMap(type -> type.supportedArchitectures.stream()
.filter(value -> value != null && !value.isBlank())
.findFirst()))
.orElse("x86_64");
}
public Subnet requireSubnet(String region, String subnetId) {
ensureDefaultResources(region);
Subnet subnet = subnets.get(key(region, subnetId)).orElse(null);