fix(cloudformation): report a failed resource in the stack's rollback reason the way AWS does (#5904)

* fix(cloudformation): report a failed resource in the stack's rollback reason the way AWS does

The stack's ROLLBACK_IN_PROGRESS / UPDATE_ROLLBACK_IN_PROGRESS reason and
event now name the failed resource, "The following resource(s) failed to
create: [X]. Rollback requested by user." on a create and "failed to
update: [X]." on an update, and the resource's own message stays on its
event. A nested stack's parent resource and a stack set instance still
report that message, read from the stack's events.

* fix(cloudformation): tell a stack's own events from a resource that shares its name

failureCause matched the stack's own events by logical ID alone, so a
resource whose logical ID equals the stack name ended the backward walk
and the nested stack parent and stack set instance got the rollback
summary instead of the resource's message. Match the stack ID as well.

* fix(cloudformation): give a nested stack's parent the child's own reason

AWS puts the child stack's reason on the parent's nested stack resource,
"Embedded stack <arn> was not successfully created: The following
resource(s) failed to create: [X].", so the parent reads the child's
status reason again instead of the failed resource's message. A stack
set instance keeps the resource's message.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-10-18 01:10:38 -04:00
1 parent 30d2bf03d9
commit c68cc7694f
16 files changed
+265 -32

No files matched your search

@@ -2360,7 +2360,7 @@ public class CloudFormationService implements ResourceProvider {
Set<String> attemptedResourceIds) {
LOG.warnv("Stack {0} resource {1} failed: {2}", stack.getStackName(),
failedResource.getLogicalId(), failedResource.getStatusReason());
rollbackFailedExecution(stack, region, isCreate, failedResource.getStatusReason(), previousState,
rollbackFailedExecution(stack, region, isCreate, rollbackReason(isCreate, failedResource), previousState,
attemptedResourceIds);
}
@@ -2403,6 +2403,57 @@ public class CloudFormationService implements ResourceProvider {
persistStack(stack);
}
/**
* The stack's reason for rolling back: a summary naming the failed resource, whose own message
* stays on its own event. A resource an update adds failed to create, and only a create
* rollback says the rollback was requested, the default OnFailure counting as the request.
*/
private static String rollbackReason(boolean isCreate, StackResource failedResource) {
String operation = "UPDATE_FAILED".equals(failedResource.getStatus()) ? "update" : "create";
String summary = "The following resource(s) failed to " + operation + ": ["
+ failedResource.getLogicalId() + "].";
return isCreate ? summary + " Rollback requested by user." : summary;
}
/**
* The stack's reason, or, while that is still the rollback summary, the failed resource's own
* message, which is what a stack set instance reports as the cause.
*/
static String failureCause(Stack stack) {
String reason = stack.getStatusReason();
List<StackEvent> events = stack.eventsSnapshot();
int rollbackStart = events.size() - 1;
while (rollbackStart >= 0 && !isStackEvent(stack, events.get(rollbackStart), ROLLBACK_STARTS)) {
if (isStackEvent(stack, events.get(rollbackStart), OPERATION_STARTS)) {
return reason;
}
rollbackStart--;
}
if (rollbackStart < 0 || !Objects.equals(reason, events.get(rollbackStart).getResourceStatusReason())) {
return reason;
}
for (int i = rollbackStart - 1; i >= 0; i--) {
StackEvent event = events.get(i);
if (isOwnEvent(stack, event)) {
return reason;
}
if ("CREATE_FAILED".equals(event.getResourceStatus()) || "UPDATE_FAILED".equals(event.getResourceStatus())) {
return event.getResourceStatusReason();
}
}
return reason;
}
private static boolean isStackEvent(Stack stack, StackEvent event, Set<String> statuses) {
return isOwnEvent(stack, event) && statuses.contains(event.getResourceStatus());
}
/** The stack's own event: a resource may share the stack's name, but never its stack ID. */
private static boolean isOwnEvent(Stack stack, StackEvent event) {
return stack.getStackName().equals(event.getLogicalResourceId())
&& Objects.equals(stack.getStackId(), event.getPhysicalResourceId());
}
private List<UpdateCleanupFailure> finishCommittedResourceCleanup(Stack stack, String region) {
return finishCommittedResourceCleanup(stack, region, false);
}
@@ -3612,6 +3663,10 @@ public class CloudFormationService implements ResourceProvider {
&& normalizedHost.endsWith("." + normalizedSuffix);
}
private static final Set<String> ROLLBACK_STARTS = Set.of("ROLLBACK_IN_PROGRESS", "UPDATE_ROLLBACK_IN_PROGRESS");
private static final Set<String> OPERATION_STARTS = Set.of("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS");
/**
* Terminal statuses under which {@link #executeTemplate} actually finished creating or
* updating a stack's resources and computed its Outputs, including the two "committed but
@@ -678,7 +678,7 @@ public class StackSetService {
} else if (!"CREATE_COMPLETE".equals(stackStatus) && !"UPDATE_COMPLETE".equals(stackStatus)) {
inst.setStatus("INOPERABLE");
inst.setDetailedStatus("FAILED");
inst.setStatusReason(stacks.isEmpty() ? null : stacks.get(0).getStatusReason());
inst.setStatusReason(stacks.isEmpty() ? null : CloudFormationService.failureCause(stacks.get(0)));
} else {
inst.setDetailedStatus("SUCCEEDED");
}