diff --git a/docs/services/ec2.md b/docs/services/ec2.md index 9cfa564502..a93aa39d0d 100644 --- a/docs/services/ec2.md +++ b/docs/services/ec2.md @@ -286,7 +286,7 @@ naming different VPCs find nothing. | RebootInstances | Reboots instances through the local EC2 service model. | | DescribeInstanceStatus | Returns status records for stored instances. | | DescribeInstanceCreditSpecifications | Returns the CPU credit option of burstable performance instances. A named instance id reports the option the instance acquired at launch, either the explicit `CreditSpecification.CpuCredits` or the family default, `standard` for t2 and `unlimited` for t3, t3a and t4g. An id that is not a burstable performance instance reports `standard`, and only an unknown id is an error. Naming no id returns the instances on the unlimited option, including one that kept `unlimited` after a resize onto a non-burstable type. `Filter.N` supports the modeled `instance-id` and narrows whichever set the request selected. `MaxResults` accepts 5 through 1000 and cannot be combined with instance ids. `DryRun=true` returns `DryRunOperation`. | -| DescribeInstanceAttribute | Returns a supported attribute for an instance. | +| DescribeInstanceAttribute | Returns a supported attribute for an instance. `ebsOptimized` reports what `RunInstances` (or its launch template) set; instances launched by CloudFormation, Auto Scaling or Fleet report `false`. | | ModifyInstanceAttribute | Updates supported mutable attributes for an instance. `UserData.Value` (base64) needs the instance stopped, as on AWS, and answers `IncorrectInstanceState` otherwise. | | ModifyInstanceMetadataOptions | Updates an instance's IMDS options, changing only the fields the request names. An EKS cluster node is refused with `OperationNotPermitted`. | diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java index cc191d05d8..c5b10a9e25 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2QueryHandler.java @@ -940,6 +940,20 @@ public class Ec2QueryHandler { p.getFirst("Placement.HostId"), p.getFirst("Placement.Tenancy"), blockDeviceMappings, "spot".equals(marketType), privateIpAddress, capacitySpecification); + // The request's EbsOptimized wins over a launch template's. Applied after the launch, on this + // API path only: CloudFormation, Auto Scaling and Fleet launches still read false. Passing it + // into Ec2Service.runInstances would cover all of them. + Boolean ebsOptimized = boolParam(p, "EbsOptimized"); + if (ebsOptimized == null && launchTemplateData != null) { + ebsOptimized = launchTemplateData.getEbsOptimized(); + } + if (ebsOptimized != null) { + for (Instance inst : res.getInstances()) { + service.modifyInstanceAttribute(region, inst.getInstanceId(), "ebsOptimized", ebsOptimized.toString()); + inst.setEbsOptimized(ebsOptimized); + } + } + if (!networkInterfaceTags.isEmpty()) { List eniIds = new ArrayList<>(); for (Instance inst : res.getInstances()) { diff --git a/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesEbsOptimizedIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesEbsOptimizedIntegrationTest.java new file mode 100644 index 0000000000..004f0df550 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ec2/Ec2RunInstancesEbsOptimizedIntegrationTest.java @@ -0,0 +1,97 @@ +package io.github.hectorvent.floci.services.ec2; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.specification.RequestSpecification; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.equalTo; + +/** RunInstances dropped EbsOptimized, so an instance launched with EbsOptimized=true read back false. */ +@QuarkusTest +class Ec2RunInstancesEbsOptimizedIntegrationTest { + private static final String AUTH = "AWS4-HMAC-SHA256 Credential=test/20260205/us-east-1/ec2/aws4_request"; + + @Test + void requestValueIsReadBack() { + String id = runInstances(launch().formParam("EbsOptimized", "true")); + try { + assertEbsOptimized(id, "true"); + } finally { + terminate(id); + } + } + + @Test + void absentReadsBackFalse() { + String id = runInstances(launch()); + try { + assertEbsOptimized(id, "false"); + } finally { + terminate(id); + } + } + + @Test + void launchTemplateValueAppliesAndTheRequestOverridesIt() { + String templateId = given().header("Authorization", AUTH).formParam("Action", "CreateLaunchTemplate") + .formParam("LaunchTemplateName", "ebs-optimized-" + UUID.randomUUID()) + .formParam("LaunchTemplateData.ImageId", "ami-0abcdef1234567890") + .formParam("LaunchTemplateData.InstanceType", "t3.micro") + .formParam("LaunchTemplateData.EbsOptimized", "true") + .post("/").then().statusCode(200).extract() + .xmlPath().getString("CreateLaunchTemplateResponse.launchTemplate.launchTemplateId"); + try { + String fromTemplate = runInstances(fromTemplate(templateId)); + try { + assertEbsOptimized(fromTemplate, "true"); + String overridden = runInstances(fromTemplate(templateId).formParam("EbsOptimized", "false")); + try { + assertEbsOptimized(overridden, "false"); + } finally { + terminate(overridden); + } + } finally { + terminate(fromTemplate); + } + } finally { + given().header("Authorization", AUTH).formParam("Action", "DeleteLaunchTemplate") + .formParam("LaunchTemplateId", templateId).post("/").then().statusCode(200); + } + } + + private static RequestSpecification launch() { + return given().header("Authorization", AUTH).formParam("Action", "RunInstances") + .formParam("ImageId", "ami-0abcdef1234567890").formParam("InstanceType", "t3.micro") + .formParam("MinCount", "1").formParam("MaxCount", "1"); + } + + private static RequestSpecification fromTemplate(String templateId) { + return given().header("Authorization", AUTH).formParam("Action", "RunInstances") + .formParam("LaunchTemplate.LaunchTemplateId", templateId) + .formParam("MinCount", "1").formParam("MaxCount", "1"); + } + + private static String runInstances(RequestSpecification request) { + return request.post("/").then().statusCode(200) + .extract().path("RunInstancesResponse.instancesSet.item.instanceId"); + } + + private static void assertEbsOptimized(String id, String expected) { + given().header("Authorization", AUTH).formParam("Action", "DescribeInstanceAttribute") + .formParam("InstanceId", id).formParam("Attribute", "ebsOptimized") + .post("/").then().statusCode(200) + .body("DescribeInstanceAttributeResponse.ebsOptimized.value.text()", equalTo(expected)); + given().header("Authorization", AUTH).formParam("Action", "DescribeInstances") + .formParam("InstanceId.1", id).post("/").then().statusCode(200) + .body("DescribeInstancesResponse.reservationSet.item.instancesSet.item.ebsOptimized.text()", + equalTo(expected)); + } + + private static void terminate(String id) { + given().header("Authorization", AUTH).formParam("Action", "TerminateInstances") + .formParam("InstanceId.1", id).post("/").then().statusCode(200); + } +}