fix(ec2): record EbsOptimized from RunInstances (#6875)

* fix(ec2): record EbsOptimized from RunInstances

RunInstances dropped EbsOptimized, so an aws_instance with
ebs_optimized = true read back false and Terraform replaced it on every
plan. The request's value, else the launch template's, is now recorded on
each launched instance.

It is applied after the launch on the RunInstances API path only;
instances launched by CloudFormation, Auto Scaling or Fleet still report
false (documented).

* test(ec2): clean up the launch template and each instance if a launch fails

The launch-template case launched both instances before its cleanup scope
opened, so a failed launch leaked the template and, on the second launch,
the first instance.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-28 21:05:31 -04:00
1 parent e820414f22
commit 3f36c4e99e
3 files changed
+112 -1

No files matched your search

+1 -1
View File
@@ -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`. |
@@ -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<String> eniIds = new ArrayList<>();
for (Instance inst : res.getInstances()) {
@@ -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);
}
}